我想弄清楚如何在Zend中使用註釋以及Doctrine hydrators和builders,並且似乎無法找出事情出錯的地方。我有兩個實體,一個叫做'Lists',一個叫'Listtypes'。 Listtypes定義列表的種類(允許我在類別中創建列表)。我最初嘗試使用'ComposedObject'註釋,但決定我寧願使用Listype對象中的註釋來添加/編輯這些項目,而不是作爲獲取列表表單的選擇列表作爲字段集的手段。所以我嘗試過使用fieldset,現在我只是在註釋中排除ListtypeId(referencedColumnName)和listtype(來自ManyToOne關係的對象),並在Doctrine FormBuilder創建表單後手動添加選擇器其他領域:不能得到教義形式工作在Zend 2
$list = new \Application\Entity\Lists();
$builder = new \DoctrineORMModule\Form\Annotation\AnnotationBuilder($this->getEntityManager());
$form = $builder->createForm($list);
$form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($this->getEntityManager(),'Application\Entity\Lists'));
$form->add(
array(
'name' => 'ListtypeId',
'type' => 'DoctrineORMModule\Form\Element\DoctrineEntity',
'options' => array(
'label' => 'Select List Type',
'object_manager' => $this->getEntityManager(),
'target_class' => 'Application\Entity\Listtypes',
'property' => 'ListtypeId',
'label_generator' => function($targetEntity) {
return $targetEntity->getListtypeId() . ' - ' . $targetEntity->getListtypeName();
},
'display_empty_item' => true,
'empty_item_label' => '---',
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array(),
'orderBy' => array('ListtypeId' => 'ASC'),
),
),
'attributes' => array(
'required' => 'required'
)
),
)
);
我有這一切工作很好,並認爲我明白它在做什麼。它試圖堅持現在讓我適應的結果。我有這個選擇返回ListtypeId明顯,所以我結束了兩個相關的參數回來,LISTNAME和ListtypeId:
$form->bind($list);
if ($this->request->isPost()) {
$data['ListName'] = $this->request->getPost('ListName');
$data['ListtypeId'] = intval($this->request->getPost('ListtypeId'));
$listId = $this->request->getPost('ListId');
if((intval($listId) > 0) && ($listId != $list->getId())) {
$data['ListId'] = $this->request->getPost('ListId');
}
$form->setData($data);
//$this->getDefMapper()->setListtype($list,$Listtype['ListtypeId']);
if ($form->isValid()) {
$this->getDefMapper()->getEntityManager()->persist($list);
$this->getDefMapper()->getEntityManager()->flush();
}
}
這樣做的結果是一個數組,其中包括我輸入的名稱,從ID選擇器。一切看起來不錯。所以我做了一個$ form-> setData($ data); - 沒有錯誤,看着$ list對象,Name被設置,ListtypeId被設置,但hydrator沒有爲$ listtype屬性創建一個對象(我認爲它應該這樣做 - 我還必須設置一個關鍵字爲'listtype'或以其他方式設置它自己,例如註釋掉的代碼行?)
現在,讓我撓頭的事情是,正如我所說的,ListtypeId的值爲'2'當我在持久化之前檢查xdebug中的代碼時,$ list實例。但是,當持續的火災,我回來的是:
An error occurred
An error occurred during execution; please try again later.
Additional information:
Doctrine\DBAL\Exception\NotNullConstraintViolationException
File:
R:\TheWild\ZF2Skel\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php:112
Message:
An exception occurred while executing 'INSERT INTO Lists (ListtypeId, ListName) VALUES (?, ?)' with params [null, "hoodoo"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ListtypeId' cannot be null
我想知道的是,爲什麼?看起來是在持續調用時設置的。
隨着更多的遊戲,我能夠得到它的工作,但我仍然不明白所涉及的'whys'。我將ListtypeId中的字段重命名爲「listtype」,以匹配對象句柄屬性名稱而不是Id字段。結果是水合器使用適當的Listtypes記錄的實例填充$ list中的listitem對象句柄。但是ListtypeId仍然是空白的。然後persist()調用工作。那麼教義不會在你堅持之前填充引用? – Scott 2015-04-06 01:00:08