与ORM联合的Kohana(KO3)专栏

时间:2011-12-05 09:14:33

标签: orm join kohana

我正在尝试使用Kohana ORM从数据库中检索信息。

我的数据库中有两个相关的表:

    id          smallint
    parent_id   smallint
    name        varchar
    active      int

branches_options

    id          mediumint
    branche_id  smallint
    name        varchar
    customer_id int

使用以下代码,我想从branches_options表中检索信息

`    $branchesOptions[] = ORM::factory('branches_option')
        ->where('branche_id', '=', $subBranche)
        ->join('branches', 'LEFT')->on('branches.id', '=', 'branches_options.branche_id')
        ->order_by('name')
        ->find_all()
        ->as_array();`

现在我想在结果集中看到branches.name的值,但我不确定如何在Kohana中执行此操作。

模型的代码是:

    `class Model_Branche extends ORM
     {
        protected $_has_many = array( 
            "options" => array('model' => 'branches_option'),
            "adwords_templates" => array ('model' => 'adwords_template')
        );

        public $result = array();`

    `class Model_Branches_option extends ORM
     {
        protected $_has_many = array ( 
            "keywords" => array('model' => 'branches_options_keyword')
        );

        protected $_has_and_belongs_to = array (
            "adwords_templates" => array (
                "model" => "adwords_template", 
                "through" => "branches_options_templates"
            )
        );

        protected $_belongs_to = array ( "branche" => array () );`

可以这样做,如果是,怎么做?

2 个答案:

答案 0 :(得分:2)

您需要对模型进行一些重要更改才能使其正常工作:

class Model_Branche extends ORM
{
    protected $_has_many = array( 
        'options' => array(
            'model' => 'branches_option',
            'foreign_key' => 'branche_id'
        )
    );
}

Branches_Option模型(它应该在model/branche/文件夹中):

class Model_Branches_Option extends ORM
{
    protected $_belongs_to = array(
        'branche' => array()
    );
}

现在你可以这样做了:

$options = ORM::factory('branche', $branche_id)
    ->options
    ->find_all();

foreach ($options as $option)
{
    $branche_active = $option->branche->active;
    $branche_name = $option->branch->name;
    $option_name = $option->name;
}

这里最重要的变化之一是我们在foreign_key关系中指定了$_has_many选项。由于ORM使用的是 Kohana Inflector 帮助器,因此它可能无法自动识别( 分支 以单数形式 branch < / em> 而非 分支 )。

如果不起作用,请尝试指定$_table_name属性,原因相同。

答案 1 :(得分:0)

我也试图在这里抓住这个概念。为了在这方面像ORM一样思考,您需要从主表引用的表开始作为相关信息。

所以在我的世界里,我有一份废物报告,(模型/废物/报告),还有另一张包含所有代码(模型/废物/代码)的表格。所以在waste_reports表中有一个名为code的列。该字段可能有2E。没有waste_codes表,2E意味着什么。 waste_codes表是(id,code,name)。

我这样定义了这种关系:

class Model_Waste_code extends ORM {

    protected $_primary_key = "code";

    protected $_has_one = array(
        'waste_report' => array()
    );
}

然后在我的废物报告模型中:

class Model_Waste_report extends ORM
{
    protected $_belongs_to = array(
        'codes' => array(
            'model' => 'waste_code',
            'foreign_key' => 'code'
        )
    ); 
}

显示不同的例子:

public function action_ormwaste() {
    $test = ORM::factory('waste_report',76);
    if ($test->loaded())
        echo $test->codes->name;

    echo "<hr noshade />";

    $test = ORM::factory('waste_report')->find_all();
    foreach($test as $row)
        echo $row->codes->name . "<BR>";

}

输出:

Error with image file (mirrored, etc)
-------------------------------------
Wrong Size
Extra Prints
Error with image file (mirrored, etc)
etc...

因此,在本质上,数据的连接是即时处理的。我正在使用Kohana 3.2。

谢谢,这让我感到高兴。