Symfony2上传程序完整性约束错误



我收到的错误在这里…

执行INSERT INTO users (image_name, updated_at, email, first_name, last_name, start_weight) VALUES (?, ?, ?, ?, ?, ?)' params[零、"2015-08-13 04:52:18"、"wei23849@aldkfj.com","瑞克"、"梅森","200"):

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image_name' cannot be null

This is the Entity.

<?php
namespace AppBundleEntity;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;
/**
 * @ORMEntity
 * @ORMTable(name="users")
 * @UniqueEntity("email", message="That email is already in use")
 * @VichUploadable
 */
class User
{
    /**
     * @ORMColumn(type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @VichUploadableField(mapping="profile_image", fileNameProperty="imageName")
     * @var File
     */
    private $imageFile;
    /**
     * @ORMColumn(type="string", length=255)
     *  
     * @var string
     */
    private $imageName;
    /**
     * @ORMColumn(type="datetime")
     *
     * @var DateTime
     */
    private $updatedAt;
    /**
     * @AssertNotBlank()
     * @AssertEmail(message="Must be a valid email.")
     * @ORMColumn(type="string", length=50)
     */
    private $email;
    /**
     * @AssertNotBlank()
     * @AssertRegex("/^[a-zA-Z -']+$/")
     * @ORMColumn(type="string", length=50)
     */
    private $first_name;
    /**
     * @AssertNotBlank()
     * @AssertRegex(pattern = "/^[a-zA-Z -']+$/", message="Name must be only letters.")
     * @ORMColumn(type="string", length=50, unique=true)
     */
    private $last_name;
    /**
     * @AssertNotBlank()
     * @AssertType(type="numeric")
     * @AssertGreaterThan(value=70)
     * @ORMColumn(type="decimal")
     */
    public $start_weight;
    /**
     * @param File|SymfonyComponentHttpFoundationFileUploadedFile $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 AppBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
/**
* 
*/
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),所以我知道我没有权限问题。

一个可能的原因是VichUploaderBundle的监听器没有被触发。你清空缓存了吗?

我知道这是一个古老的问题,但这显示在谷歌搜索,似乎是最相关的。我想我也许可以帮助任何遇到这个问题的人。我本想加一条评论,但我没有这样的声望。

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

实体映射:

* @VichUploadableField(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'

如果您遵循文档说明,您可能已经将config.yml中的映射设置为product_image而不是profile_image。如果您决定为您的实体更改映射,则很容易忘记更新映射。

相关内容

  • 没有找到相关文章