2013-08-01 40 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

是的,这是不可能的:)我只是想告诉你一些其他的方式来完成工作,虽然它还没有被框架支持 –