2015-04-14 53 views
1

是否有向Phalcon嚮導功能添加附加條件的選項hasManyToMany?我已經檢查了phalcon docs,但他們沒有列出hasManyToMany的所有可能選項(僅列出了別名和foreginKey)。 如果這不能用hasManyToMany完成,是否有其他Phalcon選項可用 - 例如。檢索所有,然後過濾一些模型加載事件?Phalcon模型與附加條件具有多種關係

簡單示例: 我有3個表組,關係和用戶,並希望使用關係表在用戶模型中創建兩個屬性 - active_groups和inactive_groups。

代碼:

class GroupModel extends Model { 
    public $id;  
    public $name; 
} 

class RelationModel extends Model { 
    public $id;  
    public $related_id_one; 
    public $related_id_two; 
    public $active; 
} 

class UserModel extends Model { 

    public function initialize() { 

     $this->hasManyToMany(
      "id", 
      "RelationModel", 
      "related_id_one", 
      "related_id_two", 
      "GroupModel", 
      "id", 
      array( 'alias' => 'active_groups', 
        'conditions' => 'RelationModel.active = true') 
     ); 

     $this->hasManyToMany(
      "id", 
      "RelationModel", 
      "related_id_one", 
      "related_id_two", 
      "GroupModel", 
      "id", 
      array( 'alias' => 'inactive_groups', 
        'conditions' => 'RelationModel.active = false') 
     ); 
    } 
} 

TNX!

回答

2

你可以這樣做,而不是。順便說一句,不完全確定這段代碼是否運行正常,但你希望得到這個想法。

我不知道是否爾康有一個條件/何選項或不

class GroupModel extends Model { 
    public $id;  
    public $name; 
} 

class RelationModel extends Model { 
    public $id;  
    public $related_id_one; 
    public $related_id_two; 
    public $active; 
} 

class UserModel extends Model { 

    public function initialize() { 

     $this->hasManyToMany(
      "id", 
      "RelationModel", 
      "related_id_one", 
      "related_id_two", 
      "GroupModel", 
      "id", 
      array( 'alias' => 'groups') 
     ); 

    } 

    public function getActiveGroups() { 
     return $this->getGroups('RelationModel.active = true'); 
    } 

    public function getInactiveGroups() { 
     return $this->getGroups('RelationModel.inactive = false'); 
    } 
} 
+0

TNX的答覆。最後,我們實施了這個解決方案......雖然hasManyToMany中的條件會更酷 – zac

+0

這真的是一個好主意。非常感謝你 ! –