P
P
Pavel Osipov2012-08-13 15:04:38
symfony
Pavel Osipov, 2012-08-13 15:04:38

Text field doesn't want to be null?

Good afternoon
I understand with Symfony2, everything seems to be fine, but one hitch does not want to let me go any further. Please help.
There is an Entity Offer, which must have an OfferDescription in all languages ​​of the site. Here are their descriptions:

class Offer<br>
{<br>
    /**<br>
     * @var integer $id<br>
     *<br>
     * @ORM\Column(name="id", type="integer", nullable=false)<br>
     * @ORM\Id<br>
     * @ORM\GeneratedValue(strategy="IDENTITY")<br>
     */<br>
    private $id;<br><br>
    /**<br>
     * @ORM\OneToMany(targetEntity="OfferDescr", mappedBy="offer", cascade={"all"}, orphanRemoval=true  )<br>
     */<br>
    protected $descriptions;<br><br>
    public function __construct()<br>
    {<br>
        $this->descriptions = new ArrayCollection();<br>
    }<br>
}<br><br>
class OfferDescr<br>
{<br>
    /**<br>
     * @var integer $id<br>
     *<br>
     * @ORM\Column(name="id", type="integer", nullable=false)<br>
     * @ORM\Id<br>
     * @ORM\GeneratedValue(strategy="IDENTITY")<br>
     */<br>
    private $id;<br><br>
    /**<br>
     * @var text $h1<br>
     *<br>
     * @ORM\Column(name="h1", type="text", nullable=true)<br>
     */<br>
    private $h1 = '';<br><br>
    /**<br>
     *<br>
     * @ORM\ManyToOne(targetEntity="Offer", inversedBy="descriptions")<br>
     * @ORM\JoinColumn(name="offer_id", referencedColumnName="id")<br>
     */<br>
    protected $offer;<br>
}<br><br>

The edit form is created like this:
class OfferDescrEditForm extends AbstractType<br>
{<br>
    public function buildForm(FormBuilder $builder, array $options)<br>
    {<br>
        $builder->add('lng','hidden');<br>
        $builder->add('h1');<br>
    }<br>
}<br><br>
class OfferEditForm extends AbstractType<br>
{<br>
    public function buildForm(FormBuilder $builder, array $options)<br>
    {<br>
        $builder->add('descriptions', 'collection', array('type' => new OfferDescrEditForm()));<br>
    }<br>
}<br>

The Twig template for editing and adding a new Offer is the same.
Method receiving data from it:
if ( $id ) {<br>
//                Update offer action<br>
                $offer = $this->getOfferTotalInfoArr( $id );<br>
                $this->_updOfferAction( $offer );<br>
            }else{<br>
//                Add new ofer action<br>
                $offer = $this -> _initNewOffer();<br>
                $this->_updOfferAction( $offer );<br>
            }<br>

where,
private function _initNewOffer( ) {<br>
        $langs = $this->getLanguages();<br>
        $offer = new \PROJ\MainBundle\Entity\Offer();<br><br>
        foreach ( $langs as $lng_key => $lng_info ) {<br>
            $descr = new \PROJ\MainBundle\Entity\OfferDescr();<br>
            $descr->setLng( $lng_key );<br>
            $descr->setOffer( $offer );<br><br>
            $offer->addOfferDescr( $descr );<br>
        }<br>
        return $offer;<br>
    }<br>

And in the end, the problem is, editing an existing Offer works OK, but when adding a new one, I get an error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'h1' cannot be null

if h1 for the first language is empty, if you add text to it, then
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'h2' cannot be null
and so on through the empty fields.
sudo php app/console cache:clear --env=devdoesn't change anything.
I don't understand what is the reason. Somewhere there is a notnull requirement for the field, but where?
Thanks in advance for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
balloon, 2012-08-13
@Pavel_Osipov

MySQL throws errors. Check that the table does not stand NOT NULLfor these fields.

A
Andrey Osty, 2012-08-13
@2king2

Here is PHP: empty - Manual

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question