【问题标题】:Using $_has_many in Kohana's ORM model在 Kohana 的 ORM 模型中使用 $_has_many
【发布时间】: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


【解决方案1】:

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

您缺少的另一件事是填充与数据有很多关系;以您尝试的方式这样做是没有意义的,而是:

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,应执行该异常以获取验证错误。

【讨论】:

  • 请注意 - 您的代码适用于 Kohana v3.1.x(使用 save() 方法而不是 create()update() 用于 3.0.x)
  • @biakaveron 感谢您明确表示,他已经指定他使用的是 3.1.2
【解决方案2】:

对于 $_has_many,你应该复数。

代替:

protected $_has_many = array( 'ingredient' => array() );

尝试:

protected $_has_many = array( 'ingredients' => array() );

【讨论】:

  • 如果我没记错的话,那是 Kohana-2,而不是 Kohana-3(我正在使用的)
  • 不——KO3 仍在使用 Inflector 类在适当的地方使用单数/复数形式,以使代码读起来更像你用自然语言所说的方式。就个人而言,我真的很欣赏这个功能。见kohanaframework.org/3.1/guide/orm/relationships#hasmany
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
相关资源
最近更新 更多