While writing my webapps I'm using AR even if I only select something. Where a DOA would be better, but is more work and doesnt look as clean as the code for the AR. The downside of using AR is that it isnt as fast…
It would be nice if there would be a way to write SQL selects in the same way as in AR. And still using the relations().
Something like:
$data = Blog::model()->selectOnly()->with('comment')->findByPk(1);
/* generates following statement
SELECT
blog.*, comment.*
FROM
blog
LEFT JOIN comment ON
comment.blog = blog.id
WHERE
blog.id = 1
*/
print_r($data);
/*
array(
'title' => 'blog title',
'content' => 'blog content',
'comments' => array(
array(
'user' => 'username1',
'text' => 'commenting on something'
),
array(
'user' => 'username2',
'text' => 'commenting on something 2'
)
)
)
*/
I know that this example doesnt include the problem with double column names.
The following code could fix this:
$data = Blog::model()->selectOnly(array(
'blog' => array('id','title','content'),
'comment' => array('id'=>'commentid','user','text')
))->with('comment')->findByPk(1);
/* generates following statement
SELECT
blog.id, blog.title, blog,content, comment.id AS commentid, comment.user, comment.text
FROM
blog
LEFT JOIN comment ON
comment.blog = blog.id
WHERE
blog.id = 1
*/