在 Kohana 的 ORM 模型中使用 $_has_many
所以我正在尝试学习 Kohana,但当涉及到他们的 ORM 模块时我遇到了相当大的障碍。 当尝试设置一对多 ORM 对象时,我可以更新/插入父模型中的信息,但它不允许我关联(插入/更新)任何新的子模型。< /strong>
为了清楚起见,这是我的数据库结构...
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 ),如下所示:
So I'm trying to learn Kohana, and I've hit quite a snag when it comes to their ORM module. When trying to set up a one-to-many ORM object, I am able to update/insert the information from my parent model, but it won't allow me to associate ( insert/update ) any new children.
For clarity's sake, here is my database structure...
recipes
--id
--recipe
--directions
--servings
ingredients
--id
--recipe_id
--amount
--serving
items
--id
--item
...my models...
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() );
}
...and my controller...
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();
}
}
I freely admit this is due to my ineptitude, but I've been over the docs/wiki/read(ing) through the source code, and haven't been able to find anything even close. Appreciate any help/ideas everyone may have
EDIT: After re-reading, it may not be very clear. What I am trying to do, is update the $recipe object, and then update/add ingredients, and their one-to-one sub-objects ( items ) like so:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如奥斯汀指出的,有很多关系按照惯例应该是复数。
您缺少的另一件事是填充与数据有很多关系;按照您尝试的方式进行操作是没有意义的,而是:
注意:此示例不会捕获 ORM_Validation_Exceptions,应执行该操作以获得验证错误。
As Austin pointed out, has many relations should be plural by convention.
Another thing you're missing is filling has many relations with data; there is no sense in doing it the way you're trying but rather:
Note: this example doesn't catch ORM_Validation_Exceptions, which should be performed in order to get validation errors.
对于 $_has_many,您应该采用复数形式。
而不是:
protected $_has_many = array( 'ingredient' => array() );
尝试:
protected $_has_many = array( 'ingredients' => array() );< /代码>
For $_has_many, you should pluralize.
Instead of:
protected $_has_many = array( 'ingredient' => array() );
try:
protected $_has_many = array( 'ingredients' => array() );