2017-01-28 10 views
0

私はnodesテーブルとnodes_nodesテーブルを持っています。cakephp 3節約自己参照belongsToMany

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

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

ノードは、1つまたは複数の親と1つ以上のチャイルズを持つことができます

NodesTable.php:

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, 
    ]; 
} 

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