2010-05-25 67 views
1

我在使用原則選擇數據子集時遇到了問題。通過關係查找原則

我有3個表

位置 聯繫 Contact_location

該聯繫人和位置表中保存的名稱和其他表僅持有ID的一個ID。例如:

Location 
loc_id: 1 
name: detroit 
Contact 
contact_id: 1 
name: Mike 
Contact_location 
loc_id: 1 
contact_id: 1 

教義是有許多與contact_location作爲ref_class的地點和聯繫表之間的多對多關係。

我想要做的就是在我的位置頁我想找到其中例如loc_id = 1

我嘗試了所有聯繫人:

$this->installedbases = Doctrine::getTable('contact')->findByloc_id(1); 

希望學說將看到的關係,並得到它,但它不。

我如何在相關的相關表格中進行原則搜索?我讀過它可以使用Findby完成,但我發現文檔不清楚。

回答

2

你的表類添加一個方法:

class ContactTable extends Doctrine_Table 
{ 
    public function findByLocationId($id) 
    { 
    return self::createQuery("c") 
     ->innerJoin("c.Location l") 
     ->where("l.loc_id = ?", $id) 
     ->execute(); 
    } 
} 

然後調用它,如下所示:

$loc_id = 1; 
$result = Doctrine::getTable("Contact")->findByLocationId($loc_id); 

主義應該使用refclass執行內部連接爲您服務。

7

更改findByloc_id()findByLocId()。該方法被魔術__call()捕獲。