表单不绑定所有数据

时间:2014-01-24 14:48:10

标签: orm doctrine-orm zend-framework2

更新

当我使用时:

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

而不是:

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

工作正常。为什么呢?


将ZF2与Doctrine一起使用2.在我的表单的编辑操作中,只有字段titleemail显示在其文本框中。其他文本框为空,就好像数据库中没有值一样。但是有。

但是,如果我将url_key放到email setter / getter中,就像下面那样有效。

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

通过电子邮件getter工作...我想我的绑定或学说2水合作用有问题吗?


以下是我的一些代码:

控制器

    $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()) {

实体(setter& 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; }

1 个答案:

答案 0 :(得分:1)

正如您在更新中所述,这是您的实体字段/设置器不匹配。 Doctrine找到protected $short_description;并尝试查找相应的getter / setter,但setShortDesc()不匹配。

你应该使用像protected $shortDesc; getShortDesc(); setShortDesc();这样的东西作为教义读取实体字段,然后尝试找到匹配相同名称和前置方法的getter / setter。当getShortDesc()仅与getter中的代码链接时,无法将short_description/** @Column(name="field_name") */匹配。

在ZF2中,你被建议使用camelCase,所以即使在实体中,摆脱下划线似乎也是一种好习惯。否则getter看起来不合适,并且在同一代码中混合两种样式是不好的。

如果你想要或需要使用下划线,你可以告诉你这样的学说:

private $fieldName;

{{1}}

相关问题