CakePHP:如何仅列出用户自己(一个用户)发布的帖子

发布于 2024-10-15 06:28:20 字数 1111 浏览 1 评论 0原文

在此页面上,用户将看到自己发布的链接列表,不包括其他人发布的其余链接。我正在我的一个模型中编写该函数,例如

我正在尝试使用仪表板的想法的新闻: 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 技术交流群。

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

发布评论

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

评论(2

伴我心暖 2024-10-22 06:28:20

试试这个

你不能使用模型中的会话,所以从控制器传递

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)));

    }   

Try this

You can't use Session from model so passit from controller

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)));

    }   
苦笑流年记忆 2024-10-22 06:28:20

我的方法似乎更像文档:

假设您有带有 Post 模型的 PostsController。当您对帖子建立索引时,可以查询所有帖子。但对于一个用户来说,我认为您指的是 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/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:

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);
    }
}

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

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