0 レビュー
1 回答
php-ZF2フォーム/ドクトリンManyToMany自己参照関係
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,
],
]);
}
}
およびフィールドセット
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
レビュー
答え :
解決策:
検索の日の後、問題を解決しました。主なことは、フォームにデータを入力するために配列を生成する必要があるということです。
次の構造で配列を作成します
$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(),
],
],
],
]);
結果は私が期待したものです
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。