CakePHP:如何仅列出用户自己(一个用户)发布的帖子
在此页面上,用户将看到自己发布的链接列表,不包括其他人发布的其余链接。我正在我的一个模型中编写该函数,例如
我正在尝试使用仪表板的想法的新闻: http://nuts-and-bolts-of-cakephp.com/2008 /12/16/how-to-build-a-dashboard-for-your-application-in-cakephp/
我将仪表板控制器创建为:
function index () {
$this->set('news', ClassRegistry::init('News')->showmy());
}
// 在我的 news::model 中,我有一个名为 showmy 的函数()
function showmy() {
$userid = $this->Session->read('Auth.User.id');
$mynews = $this->News->find('all', array('conditions' => array('News.user_id' => '$userid')));
$this->set('mynews', $mynews);
}
//我得到的错误如下
Undefined property: News::$Session [APP\models\news.php, line 7]
Fatal error: Call to a member function read() on a non-object in C:\...\app\models\news.php on line 7
我知道 showmy 函数有严重错误,有人可以解释一下我们如何编写仅检索一个用户的帖子的函数吗?或者如果问题很小,请纠正上面的功能?
On this page, a user would see a list of links which he himself/herself has posted, not including the rests which were posted by others. I'm in the middle of writing the function in one of my models, e.g. news
I am trying to use the idea of a dashboard: http://nuts-and-bolts-of-cakephp.com/2008/12/16/how-to-build-a-dashboard-for-your-application-in-cakephp/
I created my dashboards controller as:
function index () {
$this->set('news', ClassRegistry::init('News')->showmy());
}
// In my news::model I have a function called showmy()
function showmy() {
$userid = $this->Session->read('Auth.User.id');
$mynews = $this->News->find('all', array('conditions' => array('News.user_id' => '$userid')));
$this->set('mynews', $mynews);
}
//The error I get is as follow
Undefined property: News::$Session [APP\models\news.php, line 7]
Fatal error: Call to a member function read() on a non-object in C:\...\app\models\news.php on line 7
I know something is terribly wrong with the showmy function, can someone shed some light how do we write the function that only retrieves the posts by one user? or if the problems are minor, correct the function above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
你不能使用模型中的会话,所以从控制器传递
Try this
You can't use Session from model so passit from controller
我的方法似乎更像文档:
假设您有带有 Post 模型的 PostsController。当您对帖子建立索引时,可以查询所有帖子。但对于一个用户来说,我认为您指的是 PostsController 中的单个索引操作,例如:
然后,在您看来,您构建了一个类似于索引操作的表
http://book.cakephp.org/2.0/en/models/retriving-your-data.html#magic-find-类型
My approach seems to be more documentation-like:
Let say you have PostsController with Post Model. When you do index on posts it's fine, you query for all posts. But for one user, I think you mean single index action in PostsController like:
And then, in your view, you build a table like for index action
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#magic-find-types