Symfony2 Vich Uploader INTEGRITY CONSTRAINT错误

时间:2015-08-13 09:17:02

标签: php symfony vichuploaderbundle

我收到的错误是......

  

执行' INSERT INTO用户(image_name,updated_at,email,first_name,last_name,start_weight)VALUES(?,?,?,?,?,?)'与params [null," 2015-08-13 04:52:18"," wei23849@aldkfj.com" ;," rick"," mason& #34;," 200"]:

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

这是实体..

<?php
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @UniqueEntity("email", message="That email is already in use")
 * @Vich\Uploadable
 */
class User
{

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Vich\UploadableField(mapping="profile_image", fileNameProperty="imageName")
     * @var File
     */
    private $imageFile;

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

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    private $updatedAt;

    /**
     * @Assert\NotBlank()
     * @Assert\Email(message="Must be a valid email.")
     * @ORM\Column(type="string", length=50)
     */
    private $email;

    /**
     * @Assert\NotBlank()
     * @Assert\Regex("/^[a-zA-Z -']+$/")
     * @ORM\Column(type="string", length=50)
     */
    private $first_name;

    /**
     * @Assert\NotBlank()
     * @Assert\Regex(pattern = "/^[a-zA-Z -']+$/", message="Name must be only letters.")
     * @ORM\Column(type="string", length=50, unique=true)
     */
    private $last_name;

    /**
     * @Assert\NotBlank()
     * @Assert\Type(type="numeric")
     * @Assert\GreaterThan(value=70)
     * @ORM\Column(type="decimal")
     */
    public $start_weight;

    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     */
    public function setImageFile(File $image = null) {
        $this->imageFile = $image;
        if ($image) {
            $this->updatedAt = new \DateTime('now');
        }
    }

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

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

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

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set first_name
     *
     * @param string $firstName
     * @return User
     */
    public function setFirstName($firstName)
    {
        $this->first_name = $firstName;
        return $this;
    }

    /**
     * Get first_name
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->first_name;
    }

    /**
     * Set last_name
     *
     * @param string $lastName
     * @return User
     */
    public function setLastName($lastName)
    {
        $this->last_name = $lastName;
        return $this;
    }

    /**
     * Get last_name
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->last_name;
    }

    /**
     * Set start_weight
     *
     * @param string $startWeight
     * @return User
     */
    public function setStartWeight($startWeight)
    {
        $this->start_weight = $startWeight;

        return $this;
    }

    /**
     * Get start_weight
     *
     * @return string 
     */
    public function getStartWeight()
    {
        return $this->start_weight;
    }

    public function __toString() {
        return $this->start_weight;
    }


}

这是表格类型

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

/**
* 
*/
class UserType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('email', 'email', array('label' => "Your Email", 
                                            'attr' => array(
                                                    'placeholder' => 'Enter Email',
                                                    'class' => 'form-control')))
            ->add('first_name', 'text', array('label'=>'First Name',
                                                'attr' => array(
                                                    'placeholder' => "Enter First Name",
                                                    'class' => 'form-control')))
            ->add('last_name', 'text', array('label'=>'Last Name',
                                                'attr'=> array(
                                                    'placeholder' => "Your Last Name",
                                                    'class' => 'form-control')))
            ->add('start_weight', 'text', array('label' => "Your Current Weight",
                                                'attr' => array(
                                                    'class' => "form-control",
                                                    'placeholder' => "Enter Weight")))
            ->add('imageFile', 'file')
            ->add('submit', 'submit', array('label' => "Sign Up!",
                                                'attr' => array(
                                                    'class' => 'btn btn-md btn-primary')));
}

public function getName() {
    return 'user';
}
}

有趣的是,我有两个独立的项目。代码明智的两者都是完全相同的。代码在一个项目中工作,而在另一个项目中不起作用。我在操作中有转储器并在_FILES上运行并获取错误代码0,所以没有错误。上传实际上正在进行中。在内核中,我在两个实例中都有umask(0000)所以我知道我没有权限问题。

我找不到发生这种情况的原因。

2 个答案:

答案 0 :(得分:0)

可能的原因可能是VichUploaderBundle的听众没有被解雇。您是否尝试清除缓存?

答案 1 :(得分:0)

我意识到这是一个老问题,但这显示在谷歌搜索中,似乎是最相关的。我想我也许可以帮助那些遇到这个问题的人。我会添加一条评论,但没有这样的声誉。

原始问题不包含config.yml的详细信息,检查配置是解决问题的关键。

实体映射:

* @Vich\UploadableField(mapping="profile_image", fileNameProperty="imageName")

需要在config.yml

中有相应的映射
vich_uploader:
    db_driver: orm
    mappings:
        profile_image:
            uri_prefix: /profile/images
            upload_destination: '%kernel.root_dir%/../web/profile/images'

如果您按照documentation说明操作,则可能已将config.yml中的映射设置为product_image而不是profile_image。如果您决定为实体更改映射,则很容易忘记更新映射。