orm - Related models with conditions in Phalcon -
how setup filtering on related model data using $this->hasmany()
or $this->hasone()
in model?
for instance:
i have somedata table can referenced modala or modelb. in modela , modelb have:
$this->hasmany(array('id', 'somedata', 'foreign_key');
in modela want somedata somedata.foreign_key = id , somedata.model = "modela"
.
i can them with:
$this->getrelated( 'somedata', array("model = :model:", 'bind' => array('model' => 'modela') );
but $modela->somedata
gives me somedata modela , modelb.
i've tried adding conditions in $this->hasmany()
without luck.
you can way:
// question ids 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 $id) { $ids[] = (int) $id->question_id; } unset($ids_already_in_test); if (count($ids)) $questions_query->notinwhere('model_userquestion.id', $ids); }
there 2 steps: 1) ids, don't or need 2) results ids set "notinwhere" or "inwhere" in main query
Comments
Post a Comment