gpt4 book ai didi

cakephp - 在 model::beforeSave 中添加关联数据

转载 作者:行者123 更新时间:2023-12-01 10:45:32 27 4
gpt4 key购买 nike

我试图在 cakePHPs beforeSave() 期间添加相关数据,但看起来 cakePHP 忽略了它。

<?php
App::uses("AppModel", "Model");

class Recipe extends AppModel {
public $hasMany = array('Ingredient');

public function beforeSave($options=array()) {
if(!isset($this->data["Ingredient"])) {
Debugger::log("data[Ingredient] didn't exist! adding some ingredients...");
$this->data["Ingredient"] = array(
array(
'Ingredient' => array(
'name' => 'Gluten Free Flour',
'amount' => '12 cups'
)
),
array(
'Ingredient' => array(
'name' => 'Sugar',
'amount' => '50 cups'
)
),
array(
'Ingredient' => array(
'name' => 'A shoe',
'amount' => 'Just one'
)
),
);
}
Debugger::log("Saving Data: ");
Debugger::log($this->data, 7, 10);

return true;
}
}

在我的 RecipesController 中我有这个:

public function test_save() {
//Test saving With the ingredients in the recipe
Debugger::log("Saving w/ ingredients");
Debugger::log($this->Recipe->saveAssociated(array(
'Recipe' => array(
'name' => 'Test Recipe 1'
),
'Ingredient' => array(
array(
'name' => 'Test Ingredient 1 for Test Recipe 1',
'amount' => 'a few'
),
array(
'name' => 'Test Ingredient 2 for Test Recipe 1',
'amount' => 'a lot'
)
)
)));

Debugger::log("Saving w/o ingredients");
Debugger::log($this->Recipe->saveAssociated(array(
'Recipe' => array(
'name' => 'Test Recipe 2'
)
)));
}

有食材的保存有效,无食材的保存无效,beforeSave返回前Recipe->data中的数据是这样的:

array(
'Recipe' => array(
'name' => 'Test Recipe 1'
),
'Ingredient' => array(
(int) 0 => array(
'Ingredient' => array(
'name' => 'Test Ingredient 1 for Test Recipe 1',
'amount' => 'a few'
)
),
(int) 1 => array(
'Ingredient' => array(
'name' => 'Test Ingredient 2 for Test Recipe 1',
'amount' => 'a lot'
)
)
)
)

当在保存前添加成分时:

array(
'Recipe' => array(
'name' => 'Test Recipe 1'
),
'Ingredient' => array(
(int) 0 => array(
'Ingredient' => array(
'name' => 'Gluten Free Flour',
'amount' => '12 cups'
)
),
(int) 1 => array(
'Ingredient' => array(
'name' => 'Sugar',
'amount' => '50 cups'
)
),
(int) 2 => array(
'Ingredient' => array(
'name' => 'A shoe',
'amount' => 'Just one'
)
)
)
)

当我调用保存方法之前数据在数组中时,它会工作得很好,只有当我尝试在 beforeSave 中添加关联数据时。

最佳答案

来自文档:

Be sure that beforeSave() returns true, or your save is going to fail.

http://book.cakephp.org/2.0/en/models/callback-methods.html#beforesave


已编辑: 我测试了您的代码,您是对的,beforeSave() 中添加的相关数据被忽略了。发生这种情况是因为您无法在模型的 beforeSave() 中添加或修改相关数据。这意味着,在您的 Recipe 的 beforeSave() 中,您既不能添加成分,也不能修改您之前在 Controller 中附加到 Recipe 的成分。

在您的 Recipe 的 beforeSave() 中,您只能修改 Recipe 数据。

这是一个错误吗?根据 Mark Story(CakePHP 的创建者)的说法,事实并非如此:

Its a feature, as currently saveAll only lets you use beforeSave to modify the record that's about to be saved, not the full data set. I personally don't feel that should change, but its something that could be discussed.

这句话是关于saveAll()的,但重要的部分是他所说的关于beforeSave()的内容。检查完整线程:https://github.com/cakephp/cakephp/issues/1765

关于cakephp - 在 model::beforeSave 中添加关联数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26810677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com