Nikitas
Nikitas

Reputation: 1003

Form does not bind all data

UPDATE

When I use:

public function setUrl_key($value) { $this->url_key = $value; }
public function getUrl_key() { return $this->url_key; }

Instead of:

public function setUrlKey($value) { $this->url_key = $value; }
public function getUrlKey() { return $this->url_key; }

Works fine. Why?


Using ZF2 with Doctrine 2. On edit action of my form only the fields title and email are displayed in their textbox. The other textboxes are empty as if there was no value in the database. But there is.

But if I put url_key for example into email setter/getter like below it works.

public function setEmail($value) { $this->url_key = $value; }
public function getEmail() { return $this->url_key; }

Works through email getter... I guess that something's wrong with my binding or doctrine 2 hydration?


Here is some of my code:

Controller

    $link = $this->getObjectManager()->getRepository('Schema\Entity\Link')->find($this->params('id'));
    $form = new AdminLinkForm($this->getObjectManager());
    $form->setHydrator(new DoctrineEntity($this->getObjectManager(),'Schema\Entity\Link'));
    $form->bind($link);
    $request = $this->getRequest();
    if ($request->isPost()) {

Entity (setters & getters)

.....

/** @ORM\Column(type="string", name="title", length=255, nullable=false) */
protected $title;

/** @ORM\Column(type="string", length=255, nullable=false) */
protected $short_description;

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

/** @ORM\Column(type="text", nullable=true) */
protected $sample_title;

/** @ORM\Column(type="text", nullable=true) */
protected $sample_description;

/** @ORM\Column(type="text", nullable=true) */
protected $sample_keys;

/** @ORM\Column(type="string", name="webpage_url", length=255, nullable=false) */
protected $webpage_url;

/** @ORM\Column(type="string", length=255, nullable=true) */
protected $email;
......

public function setId($value) { $this->link_id = (int)$value; }
public function getId() { return $this->link_id; }

public function setTitle($value) { $this->title = $value; }
public function getTitle() { return $this->title; }

public function setShortDesc($value) { $this->short_description = $value; }
public function getShortDesc() { return $this->short_description; }

public function setUrlKey($value) { $this->url_key = $value; }
public function getUrlKey() { return $this->url_key; }

public function setEmail($value) { $this->email = $value; }
public function getEmail() { return $this->email; }

Upvotes: 0

Views: 168

Answers (1)

jkavalik
jkavalik

Reputation: 1316

It's your entity fields/setters mismatch as you noted in update. Doctrine finds protected $short_description; and tries to find corresponding getter/setter, but setShortDesc() doesn't match.

You should use something like protected $shortDesc; getShortDesc(); setShortDesc(); as doctrine reads entity fields and then tries to find getters/setters matching the same name and prepending method before. It is not possible to match getShortDesc() with short_description when it is linked only by code inside getter.

In ZF2 you are adviced to use camelCase so even in entity it seems to be good practice to get rid of underscores. Otherwise getters would look out of place and it is not good to mix two styles in the same code.

If in your tables you want or need use underscores, you can tell doctrine like this:

/** @Column(name="field_name") */

private $fieldName;

Upvotes: 1

Related Questions