在 Kohana 的 ORM 模型中使用 $_has_many

发布于 2024-11-07 03:10:10 字数 1516 浏览 4 评论 0原文

所以我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-11-14 03:10:10

正如奥斯汀指出的,有很多关系按照惯例应该是复数。

您缺少的另一件事是填充与数据有很多关系;按照您尝试的方式进行操作是没有意义的,而是:

function action_save_form()
{
    $recipe = ORM::factory('recipe', 1);

    // Create an ingredient and attach it to the recipe (one-to-many)
    $ingredient = ORM::factory('ingredient')->values(array(
        'amount'            => 1,
        'measurement_type'  => 'tablespoon',
        'recipe'            => $recipe, // sets the fk
    ));

    $ingredient->create();

    // Update all ingredients?
    foreach ($recipe->ingredients->find_all() as $ingredient)
    {
        $ingredient->amount = 2;
        $ingredient->update();
    }

    // Create an item and attach to the recipe (one-to-one)
    $item = ORM::factory('item')->values(array(
        'item'          => 'butter',
        'ingredient'    => $ingredient,
    ));

    $item->create();

    // Update the recipes' item after it's been created
    $ingredient->item->item = 'chocolate';
    $ingredient->item->update();
}

注意:此示例不会捕获 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:

function action_save_form()
{
    $recipe = ORM::factory('recipe', 1);

    // Create an ingredient and attach it to the recipe (one-to-many)
    $ingredient = ORM::factory('ingredient')->values(array(
        'amount'            => 1,
        'measurement_type'  => 'tablespoon',
        'recipe'            => $recipe, // sets the fk
    ));

    $ingredient->create();

    // Update all ingredients?
    foreach ($recipe->ingredients->find_all() as $ingredient)
    {
        $ingredient->amount = 2;
        $ingredient->update();
    }

    // Create an item and attach to the recipe (one-to-one)
    $item = ORM::factory('item')->values(array(
        'item'          => 'butter',
        'ingredient'    => $ingredient,
    ));

    $item->create();

    // Update the recipes' item after it's been created
    $ingredient->item->item = 'chocolate';
    $ingredient->item->update();
}

Note: this example doesn't catch ORM_Validation_Exceptions, which should be performed in order to get validation errors.

滥情空心 2024-11-14 03:10:10

对于 $_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() );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文