0
votes

I am getting PDOException in edit album "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'artist' cannot be null". I debugged the code and found that after edit form action runs all the column(id,title,artist) values change to the null value in the insert statement, whereas it should be POST values of the edit form. I am using the same code as of ZF2 tutorial. $request->getPost() has correct edited values but $form->getData() returns empty form post values for (id,title,artist).

can anybody please help.

My code is:

public function editAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);
    if (!$id) {
        return $this->redirect()->toRoute('album', array(
            'action' => 'add'
        ));
    }
    $album = $this->getAlbumTable()->getAlbum($id);

    $form  = new AlbumForm();
    $form->bind($album);
    $form->get('submit')->setAttribute('value', 'Edit');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setInputFilter($album->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $this->getAlbumTable()->saveAlbum($form->getData());

            // Redirect to list of albums
            return $this->redirect()->toRoute('album');
        }
    }

    return array(
        'id' => $id,
        'form' => $form,
    );
}
2
Did you just try debugging the code? What's in $form->getData()? - Dennis D.
He said that he is getting empty form with $form->getData() - 125369
of course he did. sorry. :\ - Dennis D.

2 Answers

2
votes

According to the ZF2 example, shouldn't it be

$this->getAlbumTable()->saveAlbum($album);

instead of

$this->getAlbumTable()->saveAlbum($form->getData());

Because you have already bind the $album which attaches the model to the form. This basically does two things

  1. Displays the initial values fetched from that Album with unique ID
  2. After validation of the form the data is put back into the model.

Just try what I have suggested

2
votes

Perhaps you were experiencing the same issue as I did when creating an Entity (Model object) that was to be bound to the Form object.

The mistake I made was that I was providing always a new instance of the InputFilter from the entity's method

getInputFilter();

And after calling $form->isValid(), Zend Form was actually looking to see if there is an entity object bound to it...if so, then it would call the $entity->getInputFilter(), inside the form's $this->bindValues() method and after receiving the filter object the code would return $filter->getValues() to populate the bound model. Since the entity was always returning new InputFilter instance, naturally the values were empty/null.

For me, the mistake was writing something like this in the entity(Model):

    public function getInputFilter()
    {
        return new SomeInputFilter();
    }

But actually, I needed to write the method like this:

    public function getInputFilter()
    {
        if(empty($this->inputFilter)){
            $this->inputFilter = new SomeInputFilter();
        }

        return $this->inputFilter;
    }

As you can see, the solution was to set a protected property $inputFilter, and populate it with a new instance of the InputFilter object only if it's empty. Didn't pay attention to the docs thoroughly while I was coding, and was having the same issue as you did (empty data in the bound model), while trying to insert a record.

Hopefully, you'll find this useful, if not however, I'm sorry to waste your time reading this. :)

P.S.: Thank you for reading my answer and I know I am a little late with the response to the topic, but I've recently started working with Zend 2 Framework, and I've experienced a similar issue, so I tried to share my 2 cents in the hope of helping somehow if possible.