在科哈纳';s-ORM模型

在科哈纳';s-ORM模型,orm,kohana,kohana-3,has-many,Orm,Kohana,Kohana 3,Has Many,因此,我正在尝试学习Kohana,当涉及到他们的ORM模块时,我遇到了相当大的障碍尝试设置一对多ORM对象时,我可以更新/插入父模型中的信息,但它不允许我关联(插入/更新)任何新的子对象。 class Model_Recipe extends ORM { protected $_has_many = array( 'ingredient' => array() ); } class Model_Ingredient extends ORM { protected $_be

因此,我正在尝试学习Kohana,当涉及到他们的ORM模块时,我遇到了相当大的障碍尝试设置一对多ORM对象时,我可以更新/插入父模型中的信息,但它不允许我关联(插入/更新)任何新的子对象。

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();
    }

}
为了清楚起见,这里是我的数据库结构

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();
    }

}
…和我的控制器…

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();
    }

}
我坦率地承认这是由于我的无能,但我已经浏览了文档/维基/阅读了(ing)源代码,没有找到任何接近的内容。感谢所有人的帮助/想法


编辑:重读后,可能不太清楚。我想做的是更新$recipe对象,然后更新/添加配料,以及它们的一对一子对象(项目),如:

对于$\u has\u many,您应该多元化

而不是:

protected$\u has_many=array('component'=>array())

尝试:


protected$\u has_many=array('components'=>array())

对于美元有很多,你应该多元化

而不是:

protected$\u has_many=array('component'=>array())

尝试:


protected$\u has_many=array('components'=>array())
你缺少的另一件事是填充与数据有很多关系;你这样做毫无意义,而是:

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

正如Austin指出的,有许多关系按惯例应为复数

你缺少的另一件事是填充与数据有很多关系;你这样做毫无意义,而是:

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

如果我没有弄错的话,这是针对Kohana-2的,而不是Kohana-3(我正在使用的)不——KO3仍然在使用屈折函数类在适当的位置使用单数/复数形式,使代码读起来更像自然语言中的表达方式。就我个人而言,我真的很欣赏这个功能。如果我没弄错的话,那是Kohana-2,不是Kohana-3(我正在使用的)不——KO3仍然在使用拐点类在适当的地方使用单数/复数形式,使代码读起来更像你在自然语言中说的那样。就我个人而言,我真的很欣赏这个功能。看看是否有帮助,我正在使用Kohana 3.1.2如果有帮助,我正在使用Kohana 3.1.2只是一个注释-您的代码用于Kohana v3.1.x(使用
save()
method而不是
create()
update()
for 3.0.x)@biakaveron感谢您澄清这一点,他已经指定他使用的是3.1.2只是一个注释-您的代码用于Kohana v3.1.x(对3.0.x使用
save()
方法,而不是
create()
update()
)@biakaveron感谢您澄清这一点,他已经指定使用3.1.2