When using with findAll() - it works. But it doesn't work with find().
When using with findAll() - it works. But it doesn't work with find().
What is the error information? Any example that can duplicate the problem?
there is no error information.
when i use
$news=News::model()->with(array('author'=>array( 'select'=>'??.username,??.displayname' ), 'category'=>array( 'select'=>'??.name, ??.altname', 'condition'=>'??.blogId = 1 and ??.visible = 1' ) ))->together()->find($criteria);
i get 2 sql queries in log messages. when i change find() to findAll() - i get only 1 sql query with 2 joins
author is belongs to relation, category - many to many.
What is $criteria?
Can you show the two SQL queries with find(), and also the one SQL query with findAll()?
$criteria=new CDbCriteria; $criteria->condition='news.altname=:altname'; $criteria->params=array(':altname'=>$_GET['news']);
here i get users table with news table (author relation)
query with SQL: SELECT `news`.`newsId` AS t0_c0, `news`.`blogId` AS t0_c1, `news`.`authorId` AS t0_c2, `news`.`altname` AS t0_c3, `news`.`title` AS t0_c4, `news`.`createDate` AS t0_c5, `news`.`openDate` AS t0_c6, `news`.`metaKeyword` AS t0_c7, `news`.`allowComments` AS t0_c8, `news`.`viewsCnt` AS t0_c9, `news`.`commentsCnt` AS t0_c10, `news`.`published` AS t0_c11, `news`.`short` AS t0_c12, `news`.`full` AS t0_c13, `news`.`categoryCache` AS t0_c14, t1.`username` AS t1_c2, t1.`displayname` AS t1_c3, t1.`userId` AS t1_c0 FROM `news` LEFT OUTER JOIN `users` t1 ON `news`.`authorId`=t1.`userId` WHERE news.altname=:altname LIMIT 1
and here news + category
query with SQL: SELECT `news`.`newsId` AS t0_c0, t2.`name` AS t2_c2, t2.`altname` AS t2_c3, t2.`categoryId` AS t2_c0 FROM `news` LEFT OUTER JOIN `NewsCategory` category_t2 ON `news`.`newsId`=category_t2.`newsId` LEFT OUTER JOIN `category` t2 ON t2.`categoryId`=category_t2.`categoryId` WHERE `news`.`newsId`=1 AND t2.blogId = 1 and t2.visible = 1
and this i get when i use findAll()
query with SQL: SELECT `news`.`newsId` AS t0_c0, `news`.`blogId` AS t0_c1, `news`.`authorId` AS t0_c2, `news`.`altname` AS t0_c3, `news`.`title` AS t0_c4, `news`.`createDate` AS t0_c5, `news`.`openDate` AS t0_c6, `news`.`metaKeyword` AS t0_c7, `news`.`allowComments` AS t0_c8, `news`.`viewsCnt` AS t0_c9, `news`.`commentsCnt` AS t0_c10, `news`.`published` AS t0_c11, `news`.`short` AS t0_c12, `news`.`full` AS t0_c13, `news`.`categoryCache` AS t0_c14, t1.`username` AS t1_c2, t1.`displayname` AS t1_c3, t1.`userId` AS t1_c0, t2.`name` AS t2_c2, t2.`altname` AS t2_c3, t2.`categoryId` AS t2_c0 FROM `news` LEFT OUTER JOIN `users` t1 ON `news`.`authorId`=t1.`userId` LEFT OUTER JOIN `NewsCategory` category_t2 ON `news`.`newsId`=category_t2.`newsId` LEFT OUTER JOIN `category` t2 ON t2.`categoryId`=category_t2.`categoryId` WHERE news.altname=:altname AND t2.blogId = 1 and t2.visible = 1
I see. That's expected because the find() statement is using "LIMIT 1" to ensure only one row is returned. As a result, it will always require two SQLs (the first one is to get the main record, and the second to get all the related objects).
mmm… and what i need to do? i don't want to use findall because it returns array, and i need to write $post=$post[0]… and i also don't want to use 2 queries…
Could you please create a ticket for this? Thanks!
if i will post issue with link to forum - it’s ok? because my english is baaad
Actually I just fixed this. So no need to create ticket. Thanks.
i need to wait for next release? or download develop version?
Use 1.0 branch SVN or grab a nightly snapshot. Otherwise wait for formal release.
ok, thanks!