Something strange with empty fields in symfony2

when I submit a form with an empty field, I get an error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null. the only way to fix this that I found is to make a default value in the entity file:

 * @ORM\Column(type="string", length=100)
 */
protected $image="";

and change the customizer as follows:

public function setImage($image){
 if(!isset($image)) {
//its really empty but it works only in this way     
}
     else {
    $this->image = $image;
    }  

I think this is very strict ... Is there any explanation for this? And is there any other way to do this? }

+3
source share
2 answers

If the field is imagenot required, you can set it as nullable, so Doctrine will recognize it and set the column to NULL.

, , . Doctrine, nullable = true ORM\Column :

@ORM\Column(type="string", length=100, nullable=true)

nullable=false, .

,
Matt

+7

:

Symfony2 nulls

, , Symfony $image null $entity->setImage(null), $image.

public function setImage($image){
    if(!isset($image)) {
        // $image is null, symfony was trying to set $this->image to null, prevent it
    } else {
        $this->image = $image;
    }
}

( ?). , $this->image ( , )

public function setImage($image){
    if(isset($image)) {
        // $image not null, go ahead and use it
        $this->image = $image;
    }
}

$this->image, null.

+2

All Articles