2017-01-28 6 views
0

나는 nodes 테이블과 nodes_nodes 테이블을 가지고 있습니다.cakephp 3 자기 참조하기 belongsToMany

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('nodes'); 
    $this->displayField('name'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsToMany('ChildNodes', [ 
     'className' => 'Nodes', 
     'joinTable' => 'nodes_nodes', 
     'foreignKey' => 'parent_node_id', 
     'targetForeignKey' => 'child_node_id' 
    ]); 

    $this->belongsToMany('ParentNodes', [ 
     'className' => 'Nodes', 
     'joinTable' => 'nodes_nodes', 
     'foreignKey' => 'child_node_id', 
     'targetForeignKey' => 'parent_node_id' 
    ]); 

} 

Node.php

class Node extends Entity 
{ 

    protected $_accessible = [ 
     '*' => true, 
     'id' => false, 
     'ParentNodes' => true, 
     'ChildNodes' => true, 
     '_joinData' => true, 
    ]; 
} 
:

nodes_nodes table +----+----------------+---------------+ | id | parent_node_id | child_node_id | +----+----------------+---------------+ | 1 | 2 | 3 | | 2 | 1 | 4 | | 3 | 1 | 5 | +----+----------------+---------------+

nodes table +----+--------+ | id | name | +----+--------+ | 1 | Node1 | | 2 | Node2 | | 3 | Node3 | | 4 | Node4 | | 5 | Node5 | +----+--------+

노드는 하나 이상의 부모와 하나 이상의 차일

NodesTable.php을 가질 수 있습니다

NodesController.php

public function add() 
{ 
    $node = $this->Nodes->newEntity(); 
    if ($this->request->is('post')) { 
     $node = $this->Nodes->patchEntity($node, $this->request->data); 
     debug($node); 
     if ($this->Nodes->save($node)) { 
      $this->Flash->success(__('The node has been saved.')); 

      return $this->redirect(['action' => 'index']); 
     } 
     $this->Flash->error(__('The node could not be saved. Please, try again.')); 
    } 
    $nodes = $this->Nodes->find('list', ['limit' => 200]); 
    $this->set(compact('node', 'nodes')); 
    $this->set('_serialize', ['node']); 
} 

add.ctp 양식 : 추가에 $ 노드 개체 디버그의

<?= $this->Form->create($node) ?> 
<fieldset> 
    <legend><?= __('Add Node') ?></legend> 
    <?php 
     echo $this->Form->input('name'); 
     echo $this->Form->input('ParentNodes._ids', ['options' => $nodes]); 
     echo $this->Form->input('ChildNodes._ids', ['options' => $nodes]); 
    ?> 
</fieldset> 
<?= $this->Form->button(__('Submit')) ?> 
<?= $this->Form->end() ?> 

출력() 함수 :

object(App\Model\Entity\Node) { 

    'name' => 'Node6', 
    'ParentNodes' => [ 
     '_ids' => [ 
      (int) 0 => '15', 
      (int) 1 => '12' 
     ] 
    ], 
    'ChildNodes' => [ 
     '_ids' => [ 
      (int) 0 => '13' 
     ] 
    ], 
    '[new]' => true, 
    '[accessible]' => [ 
     '*' => true, 
     'ParentNodes' => true, 
     'ChildNodes' => true, 
     '_joinData' => true 
    ], 
    '[dirty]' => [ 
     'name' => true, 
     'ParentNodes' => true, 
     'ChildNodes' => true 
    ], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[invalid]' => [], 
    '[repository]' => 'Nodes' 

} 

내가 새로운 노드를 저장 연결이 저장되지 않습니다. ['associated' => ['ParentNodes', 'ChildNodes']]$this->Nodes->save()에 추가하려고 시도했습니다.

답변

0

이름 지정 규칙을 올바르게 따르지 않습니다. belongsToMany의 속성 이름은 기본적으로 소문자이며 연관 별칭의 복수 변형을 강조합니다 (예 : parent_nodeschild_nodes). 필요한 경우 propertyName 연결 옵션을 통해 변경할 수 있습니다.

echo $this->Form->input('parent_nodes._ids', ['options' => $nodes]); 
echo $this->Form->input('child_nodes._ids', ['options' => $nodes]); 

또한 그 모든 것이 대량 할당 할 수 있습니다 당신은, true*를 설정 한 경우 $_accessible에 속성을 추가 할 필요가 없습니다. 나는 내가 확인 생각하지 않았다 모델과 컨트롤러에 초점을했다 ...

+0

참조하십시오 그리고 그것은 간단했다 양식을 올바르게 작성하십시오. '$ _accessible'의 속성은 필자가 발견 한 또 다른 스택에서 필사적으로 이동했습니다. 어쨌든, 고마워요! – alloa