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

时间:2019-01-03 13:59:30

标签: symfony symfony-3.4 vichuploaderbundle

我完全绝望了!!我寻求您的帮助!现在我已经快两周了,所以我几乎不睡觉,-(

上下文:

Symfony 3.4 vich-uploder“ ^ 1.4”

我收到此异常:

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

我解释了我的问题:

-我在2个实体(NoteFrais和Justificatif)之间具有OneToOne关系。
-每个NoteFrais都有一个Justificatif。
-Justificatif是一个vichUplodable文件。
-一切都可以在我的本地环境中完美运行。
-仅在服务器上生产的我的版本中出现问题。 在同一项目中,我已经具有用于其他实体之间其他关系的其他映射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

在本地环境和产品服务器中,前两个映射(徽标和导入)都没有问题。

这是Justificatif实体(@ 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,因为它通常可以正常工作,所以问题并不出在那儿...

这是NoteFrais表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
    //->add('circuit')
  //======Autres champs======//

    ->add('justificatif', JustificatifType::class, array(
        'required' => false));
}

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

有关信息:

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

另一方面:

我注意到我将Justificatif实体指向另一个现有映射,一切工作正常???太棒了...但这不是我想要的...我想保留3个不同的匹配项,并且我想了解为什么忽略此第3个映射。

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

2 个答案:

答案 0 :(得分:0)

在关系中,您必须在表单构建器中将另一个表单添加为EntityType。如果将OneToOne作为EntityType,将OneToMany作为CollectionType。

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

您还可以在Justificatif实体中为属性$ documentName添加nullable true,以避免插入空值的问题。

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

已编辑 实际上,该字段不是持久性的,必须在属性实体中将其注释为File。您可以使用Vich \ UploaderBundle \ Entity \ Fileemabledable将文件信息存储在ORM实体中

在您的课程Justificatif

/**
     * 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

答案 1 :(得分:0)

我终于解决了这个问题!实际上,这非常愚蠢……问题仅涉及产品环境,这意味着所使用的配置必须从config_prod.yml文件中注册,而我的情况并非如此!大多数时候,我都处于开发环境中,所以我最终忽略了该文件的存在……感谢所有尝试帮助我的人!