代码之家  ›  专栏  ›  技术社区  ›  Moh

我不能有超过2个vichuploader映射symfony 3.4

  •  1
  • Moh  · 技术社区  · 6 年前

    我完全绝望了!!我请求你的帮助!我在这一点上已经将近两个星期了,几乎晚上都不睡觉:-(

    语境:

    交响乐3.4 维克上位机“^1.4”

    我得到这个例外:

    sqlstate[23000]:违反完整性约束:1048列'document_name'不能为空

    我解释我的问题:

    -我在两个实体之间有一对一的关系(notefrais和justicatif)。
    -每个注释都有一个理由。
    -justificatif是一个可vichuploable文件。
    -在我当地的环境中一切都很好。
    -问题只在服务器上生产的版本中出现。 在同一个项目中,我已经有了其他映射vich_uploader来处理其他实体之间的其他关系。对他们来说一切都很好。

    以下是我的配置:

    parameters:
    locale: fr
    app.path.logos: /uploads/logos
    app.path.imports: /uploads/imports
    app.path.documents: /uploads/documents
    
    vich_uploader:
    db_driver: orm
    mappings:
        logo:
            uri_prefix: '%app.path.logos%'
            upload_destination: '%kernel.root_dir%/../web%app.path.logos%'
            namer: vich_uploader.namer_uniqid
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
        import:
            uri_prefix: '%app.path.imports%'
            upload_destination: '%kernel.root_dir%/../web%app.path.imports%'
            namer: vich_uploader.namer_uniqid
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
        document:
            uri_prefix: '%app.path.documents%'
            upload_destination: '%kernel.root_dir%/../web%app.path.documents%'
            namer: vich_uploader.namer_uniqid
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
    

    我对本地环境和prod服务器中的前2个映射(徽标和导入)没有问题。

    以下是证明实体(@vich/uploadable)

    **
    * Justificatif
    *
    * @ORM\Table(name="justificatif")
    * @ORM\Entity(repositoryClass="MKG\MystiBundle\Repository\JustificatifRepository")
    * @vich\Uploadable
     */
    class Justificatif
    {
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    
    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     *
     * @Vich\UploadableField(mapping="document", fileNameProperty="documentName")
     *
     * @var File
     */
    private $justificatifFile;
    
    /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string
     */
    private $documentName;
    
    
    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    private $updatedAt;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->updatedAt = new \DateTime();
    }
    
    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|UploadedFile $justificatif
     */
    public function setJustificatifFile(File $justificatif = null)
    {
        $this->justificatifFile = $justificatif;
    
        if ($justificatif) {
            $this->updatedAt =  new \DateTime('now');
        }
    }
    
    /**
     * @return File|null
     */
    public function getJustificatifFile()
    {
        return $this->justificatifFile;
    }
    
    
    
    /**
     *
     * @param $documentName
     *
     * @return $this
     */
    public function setDocumentName($documentName)
    {
        $this->documentName = $documentName;
    
        return $this;
    }
    
    /**
     * @return string|null
     */
    public function getDocumentName()
    {
        return $this->documentName;
    }
    
    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return Justificatif
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;
    
        return $this;
    }
    
    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
    
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    }
    

    这里是notefrais实体(有关系):

    /**
     * NoteFrais
     *
     * @ORM\Table(name="note_frais")
     * @ORM\Entity(repositoryClass="MKG\MystiBundle\Repository\NoteFraisRepository")
     * @Vich\Uploadable
     */
     class NoteFrais
    {
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    
    /**
     * @ORM\ManyToOne(targetEntity="MKG\MystiBundle\Entity\Mission", cascade={"persist"})
     * @ORM\JoinColumn(name="mission_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
    private $mission;
    
    /**
     * @ORM\ManyToOne(targetEntity="MKG\MystiBundle\Entity\CodeComptable", cascade={"persist"})
     * @ORM\JoinColumn(name="compte_comptable_id", referencedColumnName="id", onDelete="SET NULL", nullable=true)
     */
    private $compteComptable;
    
    /**
     * @var string
     *
     * @ORM\Column(name="montant_defraiement_max", type="string", length=255, nullable=false)
     */
    private $montantDefraiementMax;
    
    /**
     * @var string
     *
     * @ORM\Column(name="refacturation_client", type="string", length=255, nullable=true)
     */
    private $refacturationClient;
    
    /**
     * @var string
     *
     * @ORM\Column(name="total_defraiement", type="string", length=255, nullable=true)
     */
    private $totalDefraiement;
    
    /**
     * @var string
     *
     * @ORM\Column(name="total_refacturation", type="string", length=255, nullable=true)
     */
    private $totalRefacturation;
    
    /**
     * @var string
     *
     * @ORM\Column(name="compte_avances_et_acomptes", type="string", length=255, nullable=true)
     */
    private $compteAvancesEtAcomptes;
    
    /**
     * @var string
     *
     * @ORM\Column(name="admin_current_user", type="string", length=255, nullable=true)
     */
    private $currentUser;
    
    /**
     * @var string
     *
     * @ORM\Column(name="code_affaire", type="string", length=255, nullable=true)
     */
    private $codeAffaire;
    
    /**
     * @var string
     *
     * @ORM\Column(name="etat", type="string", length=255, nullable=true)
     */
    private $etat;
    
    /**
     * @ORM\OneToOne(targetEntity="Justificatif", cascade={"persist"})
     * @ORM\JoinColumn(name="justificatif_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
    private $justificatif;
    
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dateCreation", type="datetime")
     */
    private $dateCreation;
    
    
    public function __construct() {
    
        $this->dateCreation = new \DateTime;
        $this->etat = "0";
    }
    
    
    
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    
    //======== Getters et Setters ========//
    
    /**
     * Set justificatif
     *
     * @param \MKG\MystiBundle\Entity\Justificatif $justificatif
     *
     * @return NoteFrais
     */
    public function setJustificatif(\MKG\MystiBundle\Entity\Justificatif $justificatif = null)
    {
        $this->justificatif = $justificatif;
    
        return $this;
    }
    
    /**
     * @return \MKG\MystiBundle\Entity\Justificatif
     */
    public function getJustificatif()
    {
        return $this->justificatif;
    }
    
    //======== Getters et Setters ========//
    
    }
    

    对正形式:

    public function buildForm(FormBuilderInterface $builder, array $options)
       {
           $builder->add('justificatifFile', FileType::class, array(
           //'data_class' => null,
           'label' => false,
           'required' => true,
           'attr' => array(
               'class' => 'NoteFraisBootstrapFileInput',
               'type' => 'file',
               'placeholder' => 'Selectionner un justificatif (jpeg, png, jpg, pdf)',
               'data-preview-file-type' => 'text',
               'data-allowed-file-extensions' => '["jpeg", "png", "jpg", "pdf"]',
           )
       ));
       }
    

    我使用filetype而不是vichtype,因为它通常工作得很好,所以问题不在这里。

    以下是备注表格:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        //->add('circuit')
      //======Autres champs======//
    
        ->add('justificatif', JustificatifType::class, array(
            'required' => false));
    }
    

    我尝试了很多事情,修改了我的代码,阅读了论坛的页面…

    有关信息:

    -我对服务器上的目标文件夹具有权限。
    -我已经尝试过多次更改有问题的映射的名称…
    -我清理了太多的缓存(获得,订购…)

    另一方面:

    我注意到我将我的正当实体指向另一个现有的映射,一切都能完美地工作吗?你说什么?令人惊叹的。。。但这不是我想要的…我想保持3个不同的匹配,我想理解为什么忽略第3个映射。

    感谢那些给我时间的人。-)

    2 回复  |  直到 6 年前
        1
  •  0
  •   hoover_D    6 年前

    在关系中,您必须将另一个表单作为EntityType添加到表单生成器中。如果一对一为EntityType,一对多为CollectionType。

    ->add('justificatif', EntityType::class, array(
        'class' => 'YourBundle:Justificatif',
        'required' => false
    ));
    

    您还可以将nullable true添加到属性$documentname的justification实体中,以避免插入空值时出现问题。

     * @ORM\Column(type="string", length=255, nullable=true)
     *
     * @var string
     */
    private $documentName;
    

    编辑 实际上,字段不是持久的,它必须在属性实体中作为文件进行注释。可以使用vich\uploaderbundle\entity\file embedded将文件信息存储在ORM实体中。

    在你的课堂上证明

    /**
         * NOTE: This is not a mapped field of entity metadata, just a simple property.
         * 
         * @Vich\UploadableField(mapping="document", fileNameProperty="document.name")
         * 
         * @var File
         */
        private $justificatifFile;
    
        /**
         * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
         *
         * @var EmbeddedFile
         */
        private $fileJustificatif;
    
     /**
         * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
         * of 'UploadedFile' is injected into this setter to trigger the  update. If this
         * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
         * must be able to accept an instance of 'File' as the bundle will inject one here
         * during Doctrine hydration.
         *
         * @param File|UploadedFile $justificatifFile
         */
        public function setjustificatifFile(?File $justificatifFile = null)
        {
            $this->justificatifFile = $justificatifFile;
    
            if (null !== $justificatifFile) {
                // It is required that at least one field changes if you are using doctrine
                // otherwise the event listeners won't be called and the file is lost
                $this->updatedAt = new \DateTimeImmutable();
            }
        }
    
        public function getjustificatifFile(): ?File
        {
            return $this->justificatifFile;
        }
    

    从文档中 https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md

        2
  •  0
  •   Moh    6 年前

    我终于解决了这个问题!事实上,这很愚蠢…这个问题只涉及prod环境,这意味着使用的配置必须从config_prod.yml文件中注册,而不是我的情况!大多数时候,我在开发环境中,所以我最终忽略了这个文件的存在…感谢所有帮助我的人!