EllisLab text mark
Advanced Search
     
ORM datamapper - many subqueries
Posted: 06 October 2012 03:31 PM   [ Ignore ]
Joined: 2012-09-16
6 posts

I am new in using Datamapper ORM for Codeigniter, so I need some help. I am trying to do my own blog in CI with ORM. I have it written in classic CI Active record class, and there is a problem to write some queries in ORM syntax (or I do not know it and I need help). I have the blog, where are articles and they have comments. User can also vote the post. In classic CI AR I have this query:

$query $this->db->select("
a.title AS articles_title, a.content AS articles_content,
CONCAT(up.first_name, ' ', up.last_name) AS articles_author, 
DATE_FORMAT(a.created, '%T %d.%m.%Y') AS articles_created,
(SELECT IFNULL(ROUND(AVG(av.vote),1), 0) FROM articles_votes av WHERE a.id = av.article_id) AS articles_votes,
(SELECT COUNT(ac.id) FROM articles_comments ac 
    WHERE a.id = ac.article_id) AS articles_totalcomments"
FALSE)
                ->
from('articles a')
                ->
join('user_profiles up''a.author_id = up.user_id')
                ->
where('page_id'$page_id)
                ->
order_by("a.id")
                ->
limit($num$offset)
                ->
get(); 

This give me array with all informations I need. I only pass this array to view and show it. This query returns me author full name (from table profiles, not user - I use TankAuth), and it gives me counted comments, it also gives me average mark of voting. Is possible to make this in ORM? I need your help, please!