简体   繁体   中英

Symfony2 Vich Uploader INTEGRITY CONSTRAINT error

The error I receive is here...

An exception occurred while executing 'INSERT INTO users (image_name, updated_at, email, first_name, last_name, start_weight) VALUES (?, ?, ?, ?, ?, ?)' with params [null, "2015-08-13 04:52:18", "wei23849@aldkfj.com", "rick", "mason", "200"]:

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

This is the Entity..

<?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;
    }


}

This is the Form Type

<?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';
}
}

The interesting thing about this is I have two separate projects. Both of them code wise are the EXACT SAME. The code works in one project and doesn't in the other. I have the dumper in the action and run it on _FILES and getting error code 0, so nothing wrong there. The upload is in fact taking place. In the kernel I have umask(0000) in both instances so I know that I don't have permission issues.

I can't find anything as to why this is occurring.

One possible cause for this could be that VichUploaderBundle's listeners are not firing. Did you try to clear the cache?

I realise this is an old question but this shows up in google searches and seems to be the most relevant. I thought I might be able to help anyone who runs into this problem too. I would have added a comment but don't have the reputation to do so.

The original question does not include details of the config.yml , checking the configuration was key to solving the problem for me.

The Entity mapping:

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

Needs to have a corresponding mapping in config.yml

vich_uploader:
    db_driver: orm
    mappings:
        profile_image:
            uri_prefix: /profile/images
            upload_destination: '%kernel.root_dir%/../web/profile/images'

If you followed the documentation instructions you might have set up the mapping in config.yml as product_image rather than profile_image . It is easy to forget to update your mapping if you decide to change it for your entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM