2013-08-01 39 views
3

如何在模型中使用$this->hasMany()$this->hasOne()在相關型號數據上設置過濾?相關型號帶條件Phalcon

例如:

我有一個可以參考ModalA或ModelB SomeData表。 在MODELA和ModelB我有:

$this->hasMany(array('id', 'SomeData', 'foreign_key'); 

在MODELA我想所有SomeData其中SomeData.foreign_key = id and SomeData.model = "ModelA"

我可以很容易地讓他們:

$this->getRelated(
    'SomeData', 
    array("model = :model:", 'bind' => array('model' => 'ModelA') 
); 

$modelA->SomeData給我SomeData爲MODELA和ModelB。

我試過在$this->hasMany()中添加條件,但沒有任何運氣。

+2

這種關係過濾尚未支持。隨意在Github問題頁面(https://github.com/phalcon/cphalcon)中添加NFR: –

+0

感謝您的回覆。我將很快發佈NFR的詳細信息:) – jodator

+0

作爲「解決方法」,您可以使用PHQL(http://docs.phalconphp.com/en/latest/reference/phql.html)並添加一個函數(例如)$ modelA- > getSomeDatas()返回一個Model \ ResultInterface – dompie

回答

0

你可以這樣來做:

// get what question ids are in test 
    $ids_already_in_test = $this->getDI() 
     ->get('modelsManager')->createBuilder() 
     ->from('Model_QuestionToTest') 
     ->columns(array('question_id')) 
     ->andWhere('Model_QuestionToTest.test_id = :test_id:', array('test_id' => 
            $search_options['not_in_test'])) 
     ->getQuery() 
     ->execute(); 
    // filter out these ids 
    $ids = array(); 
    foreach ($ids_already_in_test as $id) { 
     $ids[] = (int) $id->question_id; 
    } 
    unset($ids_already_in_test); 
    if (count($ids)) 
     $questions_query->notInWhere('Model_UserQuestion.id', $ids); 
    } 

有兩個步驟: 1)獲得的id,你不這樣做或做需要 2什麼的)得到這個結果的ID設爲「notInWhere」或主要查詢中的「inWhere」

+0

我認爲我的問題是關於不同的東西;)。無論如何 - 正如一些評論所說,這在目前版本的Phalcon中沒有實現。 – jodator

+0

是的,這是不可能的:)我只是想告訴你一些其他的方式來完成工作,雖然它還沒有被框架支持 –