首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Vichuploader不在生产服务器上工作

Vichuploader不在生产服务器上工作
EN

Stack Overflow用户
提问于 2018-12-27 17:12:49
回答 1查看 319关注 0票数 0

从两天开始我就有问题了!我试图在一个Symfony3.4项目中使用vichuploaderBundle上传文件。

我已经做过很多次了。但是这个time...It不起作用,我也不明白为什么。在我的本地版本中,它工作得很好,但是在我的生产服务器上却不能工作。

以下是错误消息:SQLSTATE23000:完整性约束违反: 1048列'image_name‘不能为空

文件实体是持久化的(有一个id和一个创建的日期,但是图像名是空的?)--就像vichuploader映射不工作一样?

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

这是我的JustificatifDefraiement实体:

代码语言:javascript
运行
复制
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;
}
}

我的表格:

代码语言:javascript
运行
复制
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';
}
}

配置:

代码语言:javascript
运行
复制
parameters:
locale: fr
app.path.logos: /uploads/logos
app.path.imports: /uploads/imports
app.path.justificatifs: /uploads/justificatifs

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

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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';
}

}

请救救我!

EN

回答 1

Stack Overflow用户

发布于 2018-12-28 11:22:49

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

那你的buildForm ..。

代码语言:javascript
运行
复制
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('imageFile', VichFileType::class, array(
        //Options ...
    ));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53948557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档