【发布时间】:2011-05-13 04:27:52
【问题描述】:
所以我正在尝试学习 Kohana,但在谈到他们的 ORM 模块时我遇到了很大的障碍。 在尝试设置一对多 ORM 对象时,我可以更新/插入来自父模型的信息,但它不允许我关联(插入/更新)任何新子模型。
为了清楚起见,这是我的数据库结构...
recipes
--id
--recipe
--directions
--servings
ingredients
--id
--recipe_id
--amount
--serving
items
--id
--item
...我的模型...
class Model_Recipe extends ORM
{
protected $_has_many = array( 'ingredient' => array() );
}
class Model_Ingredient extends ORM
{
protected $_belongs_to = array( 'recipe' => array() );
protected $_has_one = array( 'item' => array() );
}
class Model_Item extends ORM
{
protected $_belongs_to = array( 'ingredient' => array() );
}
...还有我的控制器...
class Controller_Recipe extends Controller
{
function action_save_form()
{
$recipe = ORM::factory( 'recipe', 1 );
$recipe->ingredient->recipe_id = 1;
$recipe->ingredient->amount = 1;
$recipe->ingredient->measurement_type = 'tablespoon';
$recipe->ingredient->save();
$recipe->ingredient->item->item = 'butter';
$recipe->ingredient->item->ingredient_id = $recipe->ingredient->id;
$recipe->ingredient->item->save();
}
}
我坦率地承认这是由于我的无能,但我已经通过源代码浏览了 docs/wiki/read(ing),但找不到任何接近的东西。感谢每个人可能有的任何帮助/想法
编辑:重读后,可能不是很清楚。我想要做的是更新 $recipe 对象,然后更新/添加成分,以及它们的一对一子对象( items ),如下所示:
【问题讨论】:
-
如果有帮助,我正在使用 Kohana 3.1.2
标签: orm kohana kohana-3 has-many