计算cakephp中的帖子数 [英] Count number of posts in cakephp

查看:128
本文介绍了计算cakephp中的帖子数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在蛋糕php中创建一个菜单,我也可以知道该节内有多少文章,我应该使用手动查询,还是存在一些现有的方法来做?



我的网站菜单:
- 作品(12)
- 照片(35)
- b $ b - 联系人

我的问题是我没有得到如何访问数据这对于每个视图,这应该是一个主菜单,所以我应该使用这在每个视图,但如果我把它放在 default.ctp ,每个模型deosn't存在,因为我无法从视图中访问它。



有一些页面介绍这个吗?

解决方案

由于这些是彼此不相关的独立模型,因此您需要执行手动计数。

  $ this-> Model-> find('count'); 

EDIT



Ok,所以看起来你正在谈论不同的模型。



如果在菜单中使用,这意味着它将显示在所有页面中。



您有两种方法。



您可以使用 AppController 为您的应用程序。基本上,您可以将此代码放在beforeRender方法中,以便在每次提交请求时运行

  function beforeRender b $ b App :: import('Model',array('Work','Photo','Stuff')); 
$ work = new Work();
$ workCount = $ work-> find('count');

//对其他的做同样的操作

$ this-> set('workCount',$ workCount);
}

有关回调的更多详细信息,请参阅: http://book.cakephp.org/view/977/Controller-Methods#Callbacks-984



其次,你可以通过帮助器来做到这一点。您可以将相同的代码(即 bforeRender 中)放入辅助程序中,您可以调用辅助程序方法。



您可以在这里查看有关创建助手的更多信息: http://book.cakephp.org/view/1097/Creating-Helpers


I'm trying to create a menu in cake php where I can also know how many articles are inside the section, should I use a manual query, or does exist some existing method to do it?

My site menu:
- Works (12)
- Photos (35)
- Stuff (7)
- Contacts

My problem is also I didn't get how I can access to data like this for every view, this should be a main menu, so I should use this in every view but If i put it in default.ctp, every model deosn't exist, because I cannot access it from a view.

Does exist some page which talks about this?

解决方案

Since those are separate models that are not related to each other, you'll need to do a manual count.

$this->Model->find('count');

EDIT

Ok, so looks like you are talking about different models.

If this is used in a menu, that means it will be shown in all pages.

You have two ways of doing this.

You can do it by having an AppController for you application. Basically, you can put this code in the beforeRender method so it runs everytime your a request is rendered

function beforeRender() {
   App::import('Model', array('Work', 'Photo', 'Stuff'));
   $work = new Work();
   $workCount = $work->find('count');

   //do the same for the other

   $this->set('workCount', $workCount);
}

Have a look at this for more details on callbacks : http://book.cakephp.org/view/977/Controller-Methods#Callbacks-984

Secondly, you can do this via a helper. You can put the same code (that is inside the bforeRender) into a helper, and you can call the helper method.

You can look here for more info on creating a helper : http://book.cakephp.org/view/1097/Creating-Helpers

这篇关于计算cakephp中的帖子数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆