I am trying to implement pagination in my search results with Yii. I have pagination working on my browse record pages, but for some reason, am having trouble with getting it working in search.
So far, I have the following SQL to produce the search results:
SELECT SQL_CALC_FOUND_ROWS DISTINCT user.id FROM user, personal_info WHERE (personal_info.bio LIKE '%a%' ) AND personal_info.user_id = user.id AND user.role = 'F' LIMIT 0, 8;
Which is passed to ActiveRecord as follows:
$results = $this->findAllBySql($sql);
Immediately afterwards I run this code:
$rows = $this->findBySql("SELECT FOUND_ROWS() as row_count;");
echo $rows->row_count;
开发者_如何学运维
Strangely, I am receiving the following error when I try and execute the above code:
Property "Search.row_count" is not defined.
For some reason, Yii is not able to retrieve the FOUND_ROWS value from MySQL.
I have pretty much exactly the same PHP code in my browse records page (but different SQL), and it works perfectly. Not sure why in this situation Yii is unable to retrieve the FOUND_ROWS value. I've tried running this code directly inside MySQL to see if there was something wrong with my SQL, but it retrieves the FOUND_ROWS value with no problems - this problem only happens when I try to do it inside Yii.
Any idea what I maybe doing wrong?
Many thanks!
Turns out that I needed to define $row_count inside the model before it would recognise it as a variable I can refer to.
精彩评论