windman
(Mjoszko)
September 30, 2009, 5:48pm
1
Hi,
my problem looks like this:
I have two models ‘Author’ and ‘Post’.
‘Author’ has relation to ‘Post’ - HAS_MANY:
'posts'=>array(self::HAS_MANY, 'Post', 'authorId')
How can I find all ‘Author’ records where ‘active’ field in ‘Post’ table == 1 ?
so I want only these authors which has some posts, and the posts are active.
Thanks for help.
Alex_Muir
(Alex Muir)
September 30, 2009, 6:54pm
2
'posts'=>array(self::HAS_MANY, 'Post', 'authorId','condition'=>'??.active=1')
windman
(Mjoszko)
September 30, 2009, 7:15pm
3
active=1 it was only an example… what if I do not know the value of post attribute… for example i need all authors which have posts and the post title = ‘test 123’ etc.
select * from Author
where exists (select 1 from Post where Post.authorId = Author.authorId and Post.title like ‘test%’)
select * from Author
where exists (select 1 from Post where Post.authorId = Author.authorId and Post.active = 1)
In model Post
‘author’=>array(self::BELONG_TO, ‘Author’, ‘authorId’, [size=“4”][color="#ff0000 "]‘alias’=>’ [/color][/size][size=“4”][color="#ff0000 "]Post [/color][/size][size=“4”][color="#ff0000 "]’ [/color][/size])
in model Author
‘posts’=>array(self::HAS_MANY, ‘Post’, ‘authorId’,[size=“4”][color="#ff0000 "]‘alias’=>' Author [/color][/size][size=“4”][color="#ff0000 "]’ [/color][/size])
$criteria=new CDbCriteria;
$criteria->condition=‘exists (select 1 from Post where Post.authorId = Author.authorId and Post.active = :condition)’;
$criteria->params=array(’:condition’=>1);
$author=Author::model()->find($criteria);