当我尝试使用Doctrine自引用关系创建ZF2表单时,我得到了Doctrine错误Method "Status::getName" is not callable
在我的实体的YAML配置下面:
Status:
type: entity
table: status
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
options:
unsigned: true
name:
type: string
length: 255
manyToMany:
workflow:
targetEntity: Status
joinTable:
name: status_workflow
joinColumns:
statusId:
referencedColumnName: id
inverseJoinColumns:
nextStatusId:
referencedColumnName: id
和表格
class WorkflowForm extends Form
{
public function init()
{
$this->setName('workflow');
$this->add([
'name' => 'workflow',
'type' => WorkflowFieldset::class,
'options' => [
'use_as_base_fieldset' => true,
],
]);
}
}
和fieldset
class WorkflowFieldset extends Fieldset ObjectManagerAwareInterface
{
use ProvidesObjectManager;
public function init()
{
$this->setName('workflow');
$this->add([
'name' => 'id',
'options' => [
'label' => 'Status name'
],
]);
$this->add([
'name' => 'workflow',
'type' => ObjectSelect::class,
'attributes' => [
'multiple' => true,
],
'options' => [
'object_manager' => $this->getObjectManager(),
'target_class' => Status::class,
'property' => 'name',
],
]);
}
}
和行动
public function workflowEditAction()
{
$sm = $this->getServiceLocator();
$fm = $sm->get('FormElementManager');
$om = $sm->get('Doctrine\ORM\EntityManager');
$form = $fm->get(WorkflowForm::class);
//$workflow = $om->getRepository(Status::class)->getStatusesByEntityId($route->getParam('id'));
//$form->bind($workflow);
return new ViewModel([
'form' => $form,
]);
}
最后我想得到这样的东西
很抱歉代码太多了,我没有展示Hidrator,Factory和模板。
提前感谢大家的帮助。
答案 0 :(得分:0)
经过一天的搜索,我解决了我的问题。主要的是需要生成数组来填充表单。
我用下一个结构创建数组
$values = array(4) {
[0] => array(9) {
["id"] => int(99)
["name"] => string(6) "active"
["entityId"] => int(30)
["workflow"] => array(2) {
[0] => int(100)
[1] => int(101)
}
}
[1] => array(9) {
["id"] => int(100)
["name"] => string(8) "inactive"
["entityId"] => int(30)
["workflow"] => array(0) {
}
}
[2] => array(9) {
["id"] => int(101)
["name"] => string(6) "paused"
["entityId"] => int(30)
["workflow"] => array(1) {
[0] => int(99)
}
}
}
填写$form->populateValues(['statuses' => $values]);
接下来需要在存储库中创建自定义方法
public function getWorkflowByModule($module) {
$moduleAlias = 'entity';
$workflowAlias = 'workflow';
$qb = $this->createQueryBuilder($this->_alias)
->select($this->_alias)
->leftJoin($this->_alias . '.workflow', $workflowAlias)
->leftJoin($this->_alias . '.entity', $moduleAlias);
$qb->where($qb->expr()->eq($this->_alias . '.' . 'entity', $module->getId()));
return $qb->getQuery()->getResult();
}
更改字段集中的选择
$this->add([
'name' => 'workflow',
'type' => ObjectSelect::class,
'attributes' => [
'multiple' => true,
],
'options' => [
'object_manager' => $this->getObjectManager(),
'target_class' => Status::class,
'property' => 'name',
'is_method' => true,
'find_method' => [
'name' => 'getWorkflowByModule',
'params' => [
'module' => $this->getModule(),
],
],
],
]);
结果是我的预期