Vichuploader无法在生产服务器上运行

时间:2018-12-27 17:12:49

标签: symfony image-uploading symfony-3.4 vichuploaderbundle

2天以来我有问题!我尝试在symfony 3.4项目上使用vichuploaderBundle上传文件。

我已经做过很多次了。但是这次...这是行不通的,我也不明白为什么。在我的本地版本上,它可以正常工作,但是在我的生产服务器上,它不起作用。

这是错误消息: SQLSTATE [23000]:违反完整性约束:1048列“ image_name”不能为空

文件实体被保留(具有ID和创​​建的日期,但是图像名称为空???),就像vichuploader映射不起作用一样。

我有一个实体(NoteFrais),每个NoteFrais与另一个实体都有一个关系(JustificatifDefraiement)。

这是我的JustificatifDefraiement实体:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * JustificatifDefraiement
 *
 * @ORM\Table(name="justificatif_defraiement")
 * @ORM\Entity(repositoryClass="MKG\MystiBundle\Repository  \JustificatifDefraiementRepository")
 * @vich\Uploadable
 */
class JustificatifDefraiement
{
/**
 * @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="justificatif", fileNameProperty="imageName")
 *
 * @var File
 */
private $imageFile;

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


/**
 * @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 $justificatifDefraiement
 * @return JustificatifDefraiement
 */
public function setImageFile(File $justificatifDefraiement = null)
{
    $this->imageFile = $justificatifDefraiement;

    if ($justificatifDefraiement) {
        $this->updatedAt =  new \DateTime();
    }

    return $this;

}

/**
 * @return File|null
 */
public function getImageFile()
{
    return $this->imageFile;
}



/**
 *
 * @param $imageName
 *
 * @return $this
 */
public function setImageName($imageName)
{
    $this->imageName = $imageName;

    return $this;
}

/**
 * @return string|null
 */
public function getImageName()
{
    return $this->imageName;
}

/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 *
 * @return JustificatifDefraiement
 */
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;
}
}

我的表单:

class JustificatifDefraiementType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('imageFile', 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"]',
        )
    ));
}
/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MKG\MystiBundle\Entity\JustificatifDefraiement'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'mkg_mystibundle_justificatifDefraiement';
}
}

配置:

parameters:
locale: fr
app.path.logos: /uploads/logos
app.path.imports: /uploads/imports
app.path.justificatifs: /uploads/justificatifs

我与另一个实体有这种关系:

class NoteFrais
{
//.......//
/**
 * @ORM\OneToOne(targetEntity="MKG\MystiBundle\Entity\JustificatifDefraiement", cascade={"persist"})
 * @ORM\JoinColumn(name="justificatif_defraiement_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
 */
private $justificatifDefraiement;
//.......//
}

和noteFraisType:

class NoteFraisType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        //.......//
        ->add('justificatifDefraiement', JustificatifDefraiementType::class, array(
            'required' => false));
}
/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MKG\MystiBundle\Entity\NoteFrais'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'mkg_mystibundle_notefrais';
}

}

请帮助我!

1 个答案:

答案 0 :(得分:0)

使用VichUploader时,必须在FormType中使用VichFileType

然后您的buildForm ...

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('imageFile', VichFileType::class, array(
        //Options ...
    ));
}