【问题标题】:CakePHP Save newly created ID in Parent ID field of self assoc modelCakePHP 在自关联模型的 Parent ID 字段中保存新创建的 ID
【发布时间】:2013-03-21 03:15:28
【问题描述】:

我有一个父/子自关联表,其中当 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 中,但是有一些东西可以防止这种情况发生。我已删除所有模型验证以确保不是那样。但是 Cake(或我的关联设置)中是否存在不允许 parent_id = id 的内容(即一行是它自己的父项?

这是我添加操作中的最新代码...这会将一行保存到数据库中,但没有 parent_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 列。一行ge

提前致谢

【问题讨论】:

  • 支持一个带有详细解释的完美问题,它可以帮助我解决我正在搜索的单个数据库表类别/parent_id 问题..

标签: cakephp


【解决方案1】:

在你的关联模型中你可以将foregin_key设置为parent_id,会自动填充

【讨论】:

  • 感谢您的回复。我的模型中确实有一个 foreign_key 设置,这就是为什么它让我感到困惑......我已经编辑了我的问题以显示模型关系设置
  • 在玩了更多之后,似乎我需要对项目执行保存(保存时此自动生成主 id 键),然后对新创建的项目执行编辑以插入id 到 parent_id... 但是这似乎是需要做的,但我怀疑有更直接的方法可以做到这一点...
【解决方案2】:

所以我遇到的问题...试图在自联接模型中设置 parent_id = id(基本上使 id 成为自己的父级)是由于我的模型文件中的这段代码

public $actsAs = array('Tree');

在再次阅读 Tree Behavior 之后,我意识到 $this->Model->save(或 saveField)对于 Tree 结构和更新父 ID 并不能很好地工作。应该使用行为函数来代替我的理解。 TreeBehavior 还期望某些 parent_ids 为空(至少顶级 parent_ids),因此如果我将其保留为树,则具有空 parent_id 的 id 将被视为父项。

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

“父字段必须能够有一个 NULL 值!如果您只是将顶部元素的父值设为零,这似乎可行,但重新排序树(以及可能的其他操作)将失败。”

所以我需要决定是否要使用树行为或在不使用树的情况下具有简单的父/子关系...我会稍后再做,因为我不需要多级树,只需要父和一级儿童。

感谢您的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多