Active record/query condition based on a related model attribute...
#1
Posted 12 January 2010 - 02:12 PM
#2
Posted 12 January 2010 - 02:43 PM
skyblaze, on 12 January 2010 - 02:12 PM, said:
try this
$model=ModelName::model()->with('relatedModel')->find('relatedModel.fieldName=:condition',array(':condition'=>'%xyz%'));
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#3
Posted 13 January 2010 - 04:07 AM
Horacio Segura, on 12 January 2010 - 02:43 PM, said:
$model=ModelName::model()->with('relatedModel')->find('relatedModel.fieldName=:condition',array(':condition'=>'%xyz%'));
I got this:
$criteria=new CDbCriteria;
$criteria->with=array('tags');
if (isset($_GET['tag'])) {
$criteria->condition='published=:published AND tags.name=:tag';
$criteria->params=array('published'=>1, 'tag'=>$_GET['tag']);
} else {
$criteria->condition='published=:published';
$criteria->params=array('published'=>1);
}
but Yii says that id doesn't find the tags.name column
#4
Posted 13 January 2010 - 05:22 AM
skyblaze, on 13 January 2010 - 04:07 AM, said:
$criteria=new CDbCriteria;
$criteria->with=array('tags');
if (isset($_GET['tag'])) {
$criteria->condition='published=:published AND tags.name=:tag';
$criteria->params=array('published'=>1, 'tag'=>$_GET['tag']);
} else {
$criteria->condition='published=:published';
$criteria->params=array('published'=>1);
}
but Yii says that id doesn't find the tags.name column
sorry, in 1.0.11 work fine
the name of relation is 'tags'? , in 1.1.x the alias is equal to the name of relation
you call a findAll? see this http://www.yiiframew...-contains-with/ maybe help you
try
$criteria=new CDbCriteria;
//$criteria->with=array('tags'); remove !
if (isset($_GET['tag'])) {
$criteria->condition='published=:published AND tags.name=:tag';
$criteria->params=array('published'=>1, 'tag'=>$_GET['tag']);
} else {
$criteria->condition='published=:published';
$criteria->params=array('published'=>1);
}
ModelName::model()->with('tags')->findAll($criteria);
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#5
Posted 13 January 2010 - 05:35 AM
skyblaze, on 13 January 2010 - 04:07 AM, said:
$criteria=new CDbCriteria;
$criteria->with=array('tags');
if (isset($_GET['tag'])) {
$criteria->condition='published=:published AND tags.name=:tag';
$criteria->params=array('published'=>1, 'tag'=>$_GET['tag']);
} else {
$criteria->condition='published=:published';
$criteria->params=array('published'=>1);
}
but Yii says that id doesn't find the tags.name column
The problem might be in your $criteria->params where you omitted the ':'. It should be
$criteria->params=array(':published'=>1);
#6
Posted 13 January 2010 - 08:50 AM
Onman, on 13 January 2010 - 05:35 AM, said:
$criteria->params=array(':published'=>1);
My code is :
$criteria=new CDbCriteria;
if (isset($_GET['tag'])) {
$criteria->condition='published=:published AND tags.name=:tag';
$criteria->params=array('published'=>1, 'tag'=>$_GET['tag']);
} else {
$criteria->condition='published=:published';
$criteria->params=array('published'=>1);
}
$pages=new CPagination(Post::model()->with('tags')->count($criteria));
$pages->pageSize=3;
$pages->applyLimit($criteria);
$posts=Post::model()->with('tags')->findAll($criteria);
$this->render('list',array('posts'=>$posts,'pages'=>$pages,));
In the log i see where the error occurs. I see sql generated by active record and i see that at the point when i do: $posts=Post::model()->with('tags')->findAll($criteria); it shows me this in the log:
14:56:33.191131 trace system.db.ar.CActiveRecord
Post.findAll() eagerly
14:56:33.191704 trace system.db.CDbCommand
Querying SQL: SELECT `t`.`id` AS `t0_c0`, `t`.`title` AS `t0_c1`,
`t`.`body` AS `t0_c2`, `t`.`shortBody` AS `t0_c3`, `t`.`created_at` AS
`t0_c4`, `t`.`updated_at` AS `t0_c5`, `t`.`user_id` AS `t0_c6`,
`t`.`published` AS `t0_c7`, `t`.`permalink` AS `t0_c8` FROM `Post` `t`
WHERE (published=:published AND tags.name=:tag) LIMIT 3
...as you can see there is no join between the Post and Tag tables so it won't find the Tag name column. But why active record makes no join even if i use the with('tags) method?
#7
Posted 13 January 2010 - 09:39 AM
skyblaze, on 13 January 2010 - 08:50 AM, said:
$criteria=new CDbCriteria;
if (isset($_GET['tag'])) {
$criteria->condition='published=:published AND tags.name=:tag';
$criteria->params=array('published'=>1, 'tag'=>$_GET['tag']);
} else {
$criteria->condition='published=:published';
$criteria->params=array('published'=>1);
}
$pages=new CPagination(Post::model()->with('tags')->count($criteria));
$pages->pageSize=3;
$pages->applyLimit($criteria);
$posts=Post::model()->with('tags')->findAll($criteria);
$this->render('list',array('posts'=>$posts,'pages'=>$pages,));
In the log i see where the error occurs. I see sql generated by active record and i see that at the point when i do: $posts=Post::model()->with('tags')->findAll($criteria); it shows me this in the log:
14:56:33.191131 trace system.db.ar.CActiveRecord
Post.findAll() eagerly
14:56:33.191704 trace system.db.CDbCommand
Querying SQL: SELECT `t`.`id` AS `t0_c0`, `t`.`title` AS `t0_c1`,
`t`.`body` AS `t0_c2`, `t`.`shortBody` AS `t0_c3`, `t`.`created_at` AS
`t0_c4`, `t`.`updated_at` AS `t0_c5`, `t`.`user_id` AS `t0_c6`,
`t`.`published` AS `t0_c7`, `t`.`permalink` AS `t0_c8` FROM `Post` `t`
WHERE (published=:published AND tags.name=:tag) LIMIT 3
...as you can see there is no join between the Post and Tag tables so it won't find the Tag name column. But why active record makes no join even if i use the with('tags) method?
try update your CActiveRecord class
with http://code.google.c...e/detail?r=1704
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#8
Posted 13 January 2010 - 10:05 AM
#9
Posted 13 January 2010 - 10:08 AM
skyblaze, on 13 January 2010 - 10:05 AM, said:
you've done what he said onman
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#10
Posted 13 January 2010 - 10:10 AM
#13
Posted 13 January 2010 - 10:31 AM
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#14
Posted 13 January 2010 - 11:18 AM
Horacio Segura, on 13 January 2010 - 10:31 AM, said:
I isolated the problem. The problem (related model column not found 'cause there is an active record generated sql without the join to Tag table) is present with this line:
$pages->applyLimit($criteria);
If i remove this line everything is ok but pagination doesn't work of course
#15
Posted 13 January 2010 - 11:33 AM
skyblaze, on 13 January 2010 - 11:18 AM, said:
$pages->applyLimit($criteria);
If i remove this line everything is ok but pagination doesn't work of course
In other words if i apply a limit to the criteria also by hand:
$criteria->limit = 3;
...the generated active record sql is different and without a join so the error. But i don't understand why this behaviour
#16
Posted 13 January 2010 - 03:59 PM
skyblaze, on 13 January 2010 - 11:33 AM, said:
$criteria->limit = 3;
...the generated active record sql is different and without a join so the error. But i don't understand why this behaviour
the code is simple
// in CPaganation class
public function applyLimit($criteria)
{
$criteria->limit=$this->pageSize;
$criteria->offset=$this->currentPage*$this->pageSize;
}
the only diff is
$criteria->offset=$this->currentPage*$this->pageSize;
is rare
see the values of $pages after
$pages->applyLimit($criteria);
var_dump($pages);
What database do you use?
sorry, I am only risking solutions
good luck
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#17
Posted 15 January 2010 - 05:25 AM
Horacio Segura, on 13 January 2010 - 03:59 PM, said:
// in CPaganation class
public function applyLimit($criteria)
{
$criteria->limit=$this->pageSize;
$criteria->offset=$this->currentPage*$this->pageSize;
}
the only diff is
$criteria->offset=$this->currentPage*$this->pageSize;
is rare
see the values of $pages after
$pages->applyLimit($criteria);
var_dump($pages);
What database do you use?
sorry, I am only risking solutions
good luck
Anyway the problem with that code is still present. I resolved by using DAO instead of active record
#18
Posted 15 January 2010 - 07:04 AM
skyblaze, on 15 January 2010 - 05:25 AM, said:
put this issue in http://www.yiiframew...11-bug-reports/
maybe the Master sees it and help you
it is rare that only happen to you<_<
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/
#19
Posted 15 January 2010 - 03:06 PM
$allArticles = Article::model()->with('category', 'author', 'reviews', 'proficiency')->together()->findAll(array('order'=>'Article.proficiencyId,Article.title'));
This worked in the 1.0.x series but breaks in 1.1.x. I saw something in the docs about CSort but wasn't sure if it applied to the code above, which returns a database error about being unable to find the sort columns in the unambiguated table. Those columns do exist.
#20
Posted 15 January 2010 - 03:27 PM
drech, on 15 January 2010 - 03:06 PM, said:
$allArticles = Article::model()->with('category', 'author', 'reviews', 'proficiency')->together()->findAll(array('order'=>'Article.proficiencyId,Article.title'));
This worked in the 1.0.x series but breaks in 1.1.x. I saw something in the docs about CSort but wasn't sure if it applied to the code above, which returns a database error about being unable to find the sort columns in the unambiguated table. Those columns do exist.
maybe the problem is the alias of "Article"
in 1.1.0 the alias is harcode to "t"
see http://www.yiiframew...de/database.arr
Disambiguating Column Names
try
$allArticles = Article::model()->with('category', 'author', 'reviews', 'proficiency')->together()->findAll(array('order'=>'t.proficiencyId,t.title'));
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
http://hmsegura.blogspot.com/

Help















