在此页面上,用户会看到他/她自己发布的链接列表,不包括其他人发布的休息符。我正在我的一个模型中编写函数,例如消息
我正在尝试使用仪表板的想法: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函数有些严重错误,有人能说清楚我们如何编写只能由一个用户检索帖子的函数?或者如果问题很小,请更正上面的功能吗?
答案 0 :(得分:2)
试试这个
你不能使用模型中的Session,所以从控制器
中传递function index () {
$userid = $this->Session->read('Auth.User.id');
$this->set('news', ClassRegistry::init('News')->showmy($userid ));
}
function showmy($userid) {
return $this->find('all', array('conditions' => array('News.user_id' => $userid)));
}
答案 1 :(得分:0)
我的方法似乎更像文档:
假设你有PostsController和Post Model。当您对帖子做索引时,您可以查询所有帖子。但对于一个用户,我认为你的意思是PostsController中的单个索引操作,如:
class Post extends AppModel {
public $belongsTo = 'User'; // This way you're telling cakephp that one user can have many posts (he's posts' author)
}
class PostsController extends AppController {
public function viewByUser() {
$id = $this->Auth->user('id');
$posts = $this->Post->findAllById($id);
$this->set('posts', $posts);
}
}
然后,在您的视图中,您构建了一个类似于索引操作的表
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#magic-find-types