2011-01-31 25 views
0

このページでは、自分自身が投稿したリンクの一覧が表示されます。他の人によって投稿されました。私は自分のモデルの1つに関数を書く途中です。ニュースCakePHP:自分自身(1人のユーザー)が投稿した投稿のみを一覧表示する方法

私はダッシュボードのアイデアを使用しようとしています: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()); 
      } 

// ::モデルでは、私は(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 
0としてあります

私はshowyy関数で何かがひどく間違っていることを知っています。誰かが光を当てることができます。または問題が軽微な場合は、上記の関数を修正しますか?

答えて

2

ので

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

    } 
+0

こんにちはRSKのためのようなテーブルを構築し、Iそれに応じて変更されましたが、まだ同じエラーが発生しています。 – woel

+0

最初のエラーは次のとおりです:未定義のプロパティ:News :: $ News [APP \ models \ news.php、line 8] – woel

+0

申し訳ありません。 plz編集を参照してください 'return $ this-> find(' – RSK

0

コントローラからpassit私のアプローチは、より多くのドキュメントのようであるように思わあなたはモデルからのセッションを使用することはできません。この

をお試しください:

レッツは、あなたがポストにPostsControllerのを持っていると言いますモデル。投稿のインデックスを作成すると、すべての投稿をクエリします。しかし、一人のユーザーのために、私はあなたのようなPostsControllerの中に、単一のindexアクションを意味すると思う:

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

をそして、あなたのビューで、あなたはindexアクション

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#magic-find-types

関連する問題