2

我有一个父/子自关联表,当 id = parent_id 时,该 id 是它自己的父级。但是我无法从添加操作/视图将数据保存到我的表中

从 add.ctp 视图 - 添加新记录时,我从下拉框中选择 parent_id 并输入名称。

<?php
    echo $this->Form->input('parent_id', array('empty' => 'No Parent'));
    echo $this->Form->input('name');
?>

如果用户选择“No Parent”,这意味着我想要 parent_id = id,其中 id 是保存时在 DB 中自动创建的唯一 ID。

这是在选择“No Parent”时传递给 $this->request->data 的内容。

array(
    'Item' => array(
        'parent_id' => '',
        'name' => 'testname'
    )
)

我试图在 beforeSave 中设置 parent_id = id 但由于 id 尚不存在,因此没有任何内容可以分配 parent_id 。我也尝试过先调用“父”模型保存并在控制器中保存所有,但这些也不起作用。

控制器

public function add() {
    if ($this->request->is('post')) {
        $this->Item->create();

        //have tried calling parent model in self association first but 
        //if ($this->Item->ParentItem->save($this->request->data)) {
        if ($this->Item->saveAll($this->request->data)) {

            $this->Session->setFlash(__('The item has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The item could not be saved. Please, try again.'));
        }
    }

Item.php/模型关系

public $belongsTo = array(
  'ParentItem' => array(
  'className' => 'Item',
  'foreignKey' => 'parent_id',
  'conditions' => '',
  'fields' => '',
  'order' => ''
  )
);

public $hasMany = array(
  'ChildItem' => array(
  'className' => 'Item',
  'foreignKey' => 'parent_id',
  'dependent' => false,
  'conditions' => '',
  'fields' => '',
  'order' => '',
  ) 
);

如何获取刚刚创建/保存的 ID 并将其保存到另一个字段,在本例中为 parent_id?

更新:我一直在努力解决这个问题,我已经使用 getInsertId() 来获取最后插入的 Id,并且我试图将其保存到 parent_id 中,但是有一些东西可以防止这种情况发生。我已删除所有模型验证以确保不是那样。但是蛋糕(或我的关联设置)中是否有一些不允许 parent_id = id 的东西(即一行是它自己的父级?

这是我添加操作中的最新代码...这会将一行保存到数据库中,但没有父 ID。然后我尝试使用添加操作进行编辑并设置 parent_id = id,但即使是编辑也不允许保存。

public function add() {
    if ($this->request->is('post')) {
        $this->Item->create();
        if ($this->Item->save($this->request->data)) {
            $last_id = $this->Item->getInsertId();
            $this->Item->saveField('parent_id', $last_id);
            $this->Session->setFlash(__('The item has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The item could not be saved. Please, try again.'));
        }
    }

我也尝试调用 $this->Item->ParentItem->save(), saveAll, 'deep' => true,但仍然没有任何东西允许我更新 parent_id 列。一行哥

提前致谢

4

2 回答 2

1

在您的关联模型中您可以设置foregin_keyparent_id,并将自动填充

于 2013-03-21T07:49:40.850 回答
0

So the problem I was having... trying to set a parent_id = id (making id it's own parent basically) in a self join model is due to this piece of code in my Model file

public $actsAs = array('Tree');

After reading through the Tree Behavior again, I realized that $this->Model->save (or saveField) does not really work well with Tree structures and updating parent IDs. Should use the behaviors functions instead form my understanding. Also the TreeBehavior expects some parent_ids to be null (at least top level parent_ids), so if I were to leave this as a tree, the ids with a null parent_id would be considered the parent.

http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html

"The parent field must be able to have a NULL value! It might seem to work if you just give the top elements a parent value of zero, but reordering the tree (and possible other operations) will fail."

So I need to decide if I want to use tree behavior or have simple parent/child relationship without using Tree... think I will do the later as I don't have a need for multilevel tree, just a parent and one level of children.

Thanks for replies.

于 2013-03-22T19:40:16.970 回答