2011-01-14 117 views
0

ORM有另一個問題。Kohana3 ORM關係需要說明

我有三個模型:user.php,tag.php和/user/tag.php。

user.php的
名稱

tag.php
名稱
蛞蝓

/user/tag.php
ID
TAG_ID
user_id說明

我創造了用戶和user_tag模型之間有許多關係。所以,我得到使用下面的代碼的用戶標籤:

$user = ORM::factory('user', $user_id); 
$tags = $user->tags->find_all(); 

,這裏是我的問題,是有可能建立的關係,這將是自動查詢標籤的名稱太(或者我應該使用join()方法或離開ORM和爲此查詢生成器)?

回答

0

所有你需要的是一個has_many through relationship

$protected $_has_many = array(
    'tags' => array(
     'model' => 'tag', 
     'through' => 'user_tag', 
    ), 
); 

所以,$user->tags->find_all()將返回Model_Tag對象的數組。