2014-07-15 56 views
0

我有一個新聞提要應用程序正在寫,我想找出一種方法可以將每篇新聞文章標記爲已被每個「查看」用戶單獨。我試圖實施的當前方法是將articles_users表中的任何行視爲「此用戶查看此文章」的含義,如果他們有條目將其當前user_id與當前article_id相關聯。如何僅顯示錶中沒有HABTM關聯的行

這裏是我迄今爲止...

在我Article型號:

App::uses('AuthComponent', 'Controller/Component'); 

class Article extends AppModel { 

    public $hasAndBelongsToMany = array(
     'User' => array(
      'className' => 'User', 
      'joinTable' => 'articles_users', 
      'foreignKey' => 'article_id', 
      'associationForeignKey' => 'user_id', 
      'unique' => true 
     ) 
    ); 

    public $findMethods = array('available' => true); 

    protected function _findAvailable($state, $query, $results = array()) { 
     if ($state === 'before') { 
      $limit = (Configure::read('AppSettings.itemsperpage') ? Configure::read('AppSettings.itemsperpage'): '8'); 
      $query['limit'] = $limit; 
      $query['order'] = 'Article.listdatetime DESC'; 
      if (!empty($query['operation']) && $query['operation'] === 'count') { 
       return $query; 
      } 
      $query['joins'] = array(
       array(
        'table' => 'articles_users', 
        'alias' => 'ArticlesUser', 
        'type' => 'INNER', 
        'conditions' => array(
         'ArticlesUser.user_id' => CakeSession::read("Auth.User.id"), 
         'ArticlesUser.article_id <> Article.id' 
        ) 
       ) 
      ); 
      $query['group'] = 'Article.id'; 
      return $query; 
     } 
     return $results; 
    } 

    public $validate = array(
     'title' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A title is required' 
      ) 
     ), 
     'permalink' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A permalink is required' 
      ) 
     ), 
     'feed' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A feed is required' 
      ) 
     ) 
    ); 

} 

在我ArticlesController:

public function index() { 
    $this->paginate = array('available'); 
    $articles = $this->paginate(); 
    $this->set(compact('articles')); 
} 

結構的出口和我articles_users內容表:

-- 
-- Table structure for table `articles_users` 
-- 

CREATE TABLE IF NOT EXISTS `articles_users` (
`id` int(11) NOT NULL, 
    `article_id` int(11) NOT NULL, 
    `user_id` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; 

-- 
-- Dumping data for table `articles_users` 
-- 

INSERT INTO `articles_users` (`id`, `article_id`, `user_id`) VALUES 
(2, 3266, 4); 

但是,儘管article_id 3266與user_id 4相關聯,但我仍然看到文章顯示,因爲在條件下我有ArticlesUser.article_id <> Article.id(不等於)。在我的index()方法/動作中,我怎樣才能顯示沒有與當前user_id關聯的article_id的文章?

+0

articlesuser.article_id不在(從文章where user_id = 4選擇ID)? – franglais

+0

謝謝你franglais,你讓我真的很接近你的評論。我用''Article.id不在'(選擇article_id FROM articles_users)''代替'ArticlesUser.article_id <> Article.id'' –

+0

是的,我有一種感覺,它會與這個塊!你可能想把它寫成這個問題的答案! – franglais

回答

0

感謝franglais的評論。如果其他人有這個問題,我現在的工作:

protected function _findAvailable($state, $query, $results = array()) { 
    if ($state === 'before') { 
     $limit = (Configure::read('AppSettings.itemsperpage') ? Configure::read('AppSettings.itemsperpage'): '8'); 
     $query['limit'] = $limit; 
     $query['order'] = 'Article.listdatetime DESC'; 
     if (!empty($query['operation']) && $query['operation'] === 'count') { 
      return $query; 
     } 
     $query['joins'] = array(
      array(
       'table' => 'articles_users', 
       'alias' => 'ArticlesUser', 
       'type' => 'INNER', 
       'conditions' => array(
        'ArticlesUser.user_id' => CakeSession::read("Auth.User.id"), 
        'ArticlesUser.article_id <> Article.id' 
       ) 
      ) 
     ); 
     $query['group'] = 'Article.id'; 
     return $query; 
    } 
    return $results; 
} 
+0

也許我說得太快了。我現在注意到這隻有在articles_users表至少有一行將用戶關聯到文章時才能正常工作。如果'articles_users'表爲空,那麼index()方法不返回任何行。 –