Yii框架与MySQL FOUND_ROWS的分页问题

发布于 2024-10-09 01:05:57 字数 871 浏览 0 评论 0原文

我正在尝试使用 Yii 在搜索结果中实现分页。我的浏览记录页面上有分页功能,但由于某种原因,我在搜索中无法使用分页功能。

到目前为止,我有以下 SQL 来生成搜索结果:

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;

它将按如下方式传递给 ActiveRecord:

$results = $this->findAllBySql($sql);

之后我立即运行此代码:

$rows = $this->findBySql("SELECT FOUND_ROWS() as row_count;");  
echo $rows->row_count;

奇怪的是,当我尝试执行上述代码时,我收到以下错误:

Property "Search. row_count”未定义。

由于某些原因,Yii 无法从 MySQL 检索 FOUND_ROWS 值。

我的浏览记录页面中有几乎完全相同的 PHP 代码(但 SQL 不同),并且它运行得很好。不知道为什么在这种情况下 Yii 无法检索 FOUND_ROWS 值。我尝试直接在 MySQL 中运行这段代码,看看我的 SQL 是否有问题,但它检索 FOUND_ROWS 值没有任何问题 - 这个问题仅当我尝试在 Yii 中执行时才会发生。

知道我可能做错了什么吗?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2024-10-16 01:05:57

事实证明,我需要在模型中定义 $row_count ,然后它才会将其识别为我可以引用的变量。

Turns out that I needed to define $row_count inside the model before it would recognise it as a variable I can refer to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文