2011-12-05 24 views
1

我試圖從使用Kohana ORM的數據庫檢索信息。ORM加入Kohana(KO3)列

有我的數據庫中的兩個相關表:

分支

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

可以這樣做,如果是這樣,如何?

+0

你可以發佈你的模型的代碼嗎?你是否正確定義了它們之間的關係? – Thorsten

+0

編輯我的問題來添加模型。 – Erik

+0

這些數組是什麼? 'array('model'=>'branches_options_keyword') );'請正確地發佈你的代碼。什麼是'$ _has_and_belongs_to'? Kohanas ORM沒有這個選項。 – Thorsten

回答

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

其中一個最重要的變化在這裏我我們在$_has_many關係中指定了foreign_key選項。由於ORM使用Kohana的變形器幫手它可能無法自動識別它(分支以單數形式是分支,而不是領域)。

如果不起作用,請嘗試指定$_table_name屬性出於同樣的原因。

0

我想在這裏也把握這個概念。爲了在這方面像ORM一樣思考,您需要從主表引用相關信息的表開始。所以在我的世界裏,我有一個浪費的報告,(模型/廢物/報告),並且存在另一個包含所有代碼(模型/廢物/代碼)的表格。所以在waste_reports表中有一個名爲code的列。該字段可能有2E。沒有waste_codes表,2E就沒有任何意義。 waste_codes表是(id,代碼,名稱)。

我定義這種關係是這樣的:

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。

謝謝,這清除了我。