Markup multiple models in CakePHP 1.2.5

I am working on a presentation for a blog that mixes blog posts, comments, and uploaded media elements into one large grid layout. I created separate models in CakePHP and associations, some of which are as follows:

Comment BelongsTo Post or Media
Post HasMany Media

What I'm working with is trying to sort all three models ( Comment, Media, Post) into one large dataset, which I can then paginate.

I already have a "created" datetime field in the database. I understand how to split every single database call with CakePHP PaginationHelper. I also combined arrays. However, mixing the individual database queries and then joining the arrays seems to break the pagination, as it does not work with PaginationHelper(as I understand it).

Do you have any suggestions on this?

In addition, I would like the number of database calls to be reduced, so any suggestions on these lines would be great. Thank!

~ Andrew

+3
source share
1 answer

It looks like you want to use some kind of UNION select for these three models. You can achieve this by overriding the default paginate and paginateCount models. The basics are described in the Cooks Book ( http://book.cakephp.org/view/249/Custom-Query-Pagination ).

It looks like you will want to replace find (all) with find ('query') as follows:

/**
 * Overridden paginate method - group by week, away_team_id and home_team_id
 */
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
    $recursive = -1;
    $group = $fields = array('week', 'away_team_id', 'home_team_id');
    return $this->query('SELECT field1, field2 FROM table1 
                         UNION SELECT field1, field2 FROM table2 
                         LIMIT '.(($page-1)*$limit).', '.$limit);
}
+5
source

Source: https://habr.com/ru/post/1726547/


All Articles