我不能有超过 2 个 vichuploader 映射 |交响乐 3.4

I can't have more than 2 vichuploader mappings | symfony 3.4

我彻底绝望了!!我请求你的帮助!我阻止这一点已经快两周了,我几乎晚上不睡觉:-(

上下文:

Symfony 3.4 vich-上传者“^ 1.4”

我得到这个异常:

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

我解释一下我的问题:

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

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

这里是 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 个映射。

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

在关系中,您必须在表单生成器中将其他表单添加为 EntityType。如果 OneToOne 作为 EntityType,OneToMany 作为 CollectionType。

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

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

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

已编辑 的确,该字段并没有被持久化,它必须在属性实体中被注释为File。您可以使用 Vich\UploaderBundle\Entity\File embeddable 在您的 ORM 实体中存储文件信息

在你的class理由中

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

我终于解决了这个问题!其实很蠢。。。问题只涉及prod环境,也就是说使用的配置必须从config_prod.yml文件注册,我不是这样的!大多数时候,我在开发环境中,所以我最终忽略了这个文件的存在......感谢所有试图帮助我的人!