2012-10-28 18 views
1

我對此很陌生,並且已經放棄了Gallery3中的一個模塊並且正在尋找添加一些功能。我搜遍了所有,一直沒能找到解決方案。Kohana ORM在模型中的多個關係

我有以下表格。

Products {id, name, description, cost, discount_id, postage_id) 
Discounts {id, name, threshold, amount, maxamount) 
Postage_bands {id, name, flatrate, peritem) 

我增加了折扣,根據消費金額向顧客提供折扣。我將其基於現有郵資表格並複製關聯頁面,以便使用新的折扣功能。我希望產品的管理頁面列出哪些折扣和郵資與每個產品相關聯。

折扣的管理頁面工作得很好。

我遇到的問題是產品與折扣和郵資之間的關係。我只能得到一個或另一個工作。

有模型,每個都在它們自己的文件中。

class Product_Model extends ORM { 
    var $rules = array(
    "name" => "length[1,32]", 
    "description" => "length[0,255]"); 
    protected $belongs_to=array('discount'); 
    protected $belongs_to=array('postage_band'); 
} 

class Postage_Band_Model extends ORM { 
    var $rules = array(
    "name" => "length[1,32]"); 
    protected $has_many=array('products'); 
} 

class Discount_Model extends ORM { 
    var $rules = array(
    "name" => "length[1,32]"); 
    protected $has_many=array('products'); 
} 

在視圖頁面的代碼顯示的信息與貼現或郵資

<?= html::clean($product->discount->name) ?> 
or 
<?= html::clean($product->postage_band->name) ?> 

我只能得到一個或另一個工作註釋掉枯萎從貼現或郵資線Products_model以及將其顯示在視圖頁面中的代碼。

如何獲得一個表中的列與兩個單獨的表中的列的關係。在這種情況下,

  • Products.discount_id to Discounts.id,因此我可以指向折扣表中的名稱字段。
  • Products.postage_id爲Postage_bands.id,所以我可以指向Postage_bands表中的名稱字段。

或這些表中的任何其他字段,如果我需要它們。

在此先感謝

保羅

+0

不要忘記接受正確的答案 – biakaveron

回答

1

您聲明$ belongs_to的兩倍。嘗試:

class Product_Model extends ORM { 

    protected $belongs_to=array('discount', 'postage_band'); 
} 

而且,我認爲這是$ _belongs_to

+0

謝謝,這解決了這個問題。至於「$ _belongs_to」vs「$ belongs_to」,我不知道爲什麼,但Gallery 3不使用程序_。在尋找答案時,我看到很多帖子使用了_並且嘗試過,但根本沒有工作。 – user1780848