cakephp 3保存自我引用属于TonToy [英] cakephp 3 saving self referencing belongsToMany

查看:59
本文介绍了cakephp 3保存自我引用属于TonToy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 nodes 表和一个 nodes_nodes 表.

nodes表+ ---- + -------- +|id |名称|+ ---- + -------- +|1 |节点1 ||2 |Node2 ||3 |节点3 ||4 |节点4 ||5 |节点5 |+ ---- + -------- +

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

一个节点可以有一个或多个父母和一个或多个孩子

A node can have one or more parents and one or more childs

NodesTable.php:

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

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格式:

add.ctp form:

<?= $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() ?>

在add()函数中$ node实体调试的输出:

Output of $node entity debug in add() function:

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'=>[code] $ this-> Nodes-> save()上的['ParentNodes','ChildNodes']] 没有成功

When I save a new node the association is not saved. I have tryed adding ['associated' => ['ParentNodes', 'ChildNodes']] on $this->Nodes->save() with no success

推荐答案

您没有正确遵循命名约定.默认情况下, belongsToMany 关联的属性名称是小写的,加了下划线的关联别名的复数形式,即 parent_nodes child_nodes .如果需要,可以通过 propertyName 关联选项进行更改.

You're not following the naming conventions properly. Property names for belongsToMany associations are by default lowercase, underscored plural variants of the association aliases, ie parent_nodes and child_nodes. In case required, this can be changed via the propertyName association option.

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

如果将 * 设置为 true ,则无需将属性添加到 $ _ accessible ,因为这样可以使所有内容分配的数量.

Also there's no need to add the properties to $_accessible if you've set * to true, as that allows everything to be mass assigned.

另请参见

这篇关于cakephp 3保存自我引用属于TonToy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆