簡體   English   中英

如何在不做很長的“ OR”條件列表的情況下找到模型的所有記錄?

[英]How can I find all records for a model without doing a long list of “OR” conditions?

我在編寫CakePHP find()時遇到麻煩,該方法返回我正在尋找的記錄。

我的聯想是這樣的:

用戶->(有很多)->朋友,
用戶->(有很多)->帖子

我正在嘗試顯示所有用戶的朋友最近的帖子的列表,換句話說,列出由當前用戶登錄的朋友創建的每個帖子。

我能想到的唯一方法是將所有用戶朋友的user_id放入一個大數組中,然后遍歷每個人,以便find()調用看起來像:

$posts = $this->Post->find('all',array(
            'conditions' => array(
                'Post.user_id' => array(
                    'OR' => array(
                        $user_id_array[0],$user_id_array[1],$user_id_array[2] # .. etc
                    )
                )           
            )
        ));

我覺得這不是最好的處理方式,就好像該用戶很受歡迎是因為存在很多OR條件。 誰能提出更好的選擇?

為了澄清,這是我的數據庫的簡化版本:

“用戶”表
ID
用戶名
等等

“朋友”表
ID
用戶身份
friend_id
等等

“職位”表
ID
用戶身份
等等

復習完您寫的內容后,我想我知道您在做什么。 您當前的結構不起作用。 在POSTS中沒有引用給朋友。 因此,根據您發布的架構,朋友無法添加任何帖子。 我認為您要嘗試將一個朋友稱為其他用戶之一。 這意味着,一個用戶FRIEND實際上只是USERS表中的另一個USER。 這是一種自我參照的HABTM關系。 所以這是我的建議:

1-首先,確保在數據庫中創建了HABTM表:

-MySQL創建表users_usersuser_id char(36)NOT NULL,
friend_id char(36)NOT NULL);

2-在用戶模型中建立關系。

 var $hasAndBelongsToMany = array( 'friend' => array('className' => 'User', 'joinTable' => 'users_users', 'foreignKey' => 'user_id', 'associationForeignKey' => 'friend_id', 'unique' => true, ), ); var $hasMany = array( 'Post' => array( 'className' => 'Post', 'foreignKey' => 'user_id' ), ); 

3-使用腳手架插入一些記錄,鏈接朋友並添加帖子。 4-將視圖記錄功能添加到用戶控制器:

 function get_user($id) { $posts = $this->User->find('first', array( 'conditions' => array('User.id' => $id), 'recursive' => '2' )); pr($posts); } 

5-現在,您可以使用以下命令使用遞歸查詢User表來提取記錄:

http:// test / users / get_user / USER_ID

6-當您pr($ posts)時,您的輸出將(遞歸)顯示所有記錄,包括返回的數據樹中的朋友及其帖子。

我知道這是一篇很長的文章,但是我認為它將為您要嘗試的工作提供最佳解決方案。 CakePHP的功能令人難以置信。 是學習曲線扼殺了我們。

編碼愉快!

如果Post.user_id指向Friend.id (不遵循約定順便說一句),那么它將是

$posts = $this->Post->find('all',array(
    'conditions' => array(
        'Post.user_id' => $user_id_array      
    )
);

這將導致.. WHERE Post.user_id IN (1, 2, 3) ..

根據您的設置,運行兩個查詢可能比嘗試通過Cake東西將它們鏈接在一起更快。 我建議在用戶模型中添加諸如getFriendsPosts()之類的內容。

<?php
 class UserModel extends AppModel {
 // ... stuff
  function getFriendsPosts( $user_id )
  {
   $friends = $this->find( ... parameters to get user IDs of all friends );
   // flatten the array or tweak your params so they fit the conditions parameter.  Check out the Set class in CakePHP
   $posts = $this->find( 'all', array( 'conditions' => array( 'User.id' => $friends ) ) );
   return $posts;
  }
 }
?>

然后調用它,在控制器中執行

$friends = $this->User->getFriendsPosts( $this->Auth->User('id') );

HTH,特拉維斯

CakePHP是否已生成以下高效代碼:

SELECT * from Posts WHERE user_id IN (id1, id2 ...)

如果沒有,你可以做

$conditions='NULL';
foreach($user_id_array as $id) $conditions.=", $id";

$posts = $this->Posts->find('all', array(
    'conditions' => "Post.user_id IN ($conditions)",
));

如果您的模型正確關聯,Cake將自動檢索相關的模型記錄。 因此,當您搜索特定用戶時,Cake將自動檢索相關的朋友以及這些朋友的相關帖子。 您需要做的就是將遞歸級別設置得足夠高。

$user = $this->User->find('first', array('conditions' => array('User.id' => $id), 'recursive' => 2));
debug($user);

// gives something like:
array(
    User => array()
    Friend => array(
        0 => array(
            ...
            Post => array()
        ),
        1 => array(
            ...
            Post => array()
        )
    )
)

您需要做的就是從用戶的朋友中提取帖子,這很容易:

$postsOfFriends = Set::extract('/Friend/Post/.', $user);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM