2014-09-27 71 views
0

2.5复杂查找过滤我有4个表彼此连接CakePHP的通过中可容纳键

人才表

+--------------------+------------------+------+-----+---------+----------------+ 
| Field    | Type    | Null | Key | Default | Extra   | 
+--------------------+------------------+------+-----+---------+----------------+ 
| id     | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| created   | datetime   | YES |  | NULL |    | 
| user_id   | int(10) unsigned | NO |  | NULL |    | 
| firstname   | varchar(128)  | NO |  | NULL |    | 
| lastname   | varchar(128)  | NO |  | NULL |    | 
| phone_num   | varchar(32)  | NO |  | NULL |    | 
+--------------------+------------------+------+-----+---------+----------------+ 

这个表将包含行如

+----+-----------+------------+ 
| id | firstname | lastname | 
+----+-----------+------------+ 
| 1 | barney | stinson | 
| 2 | Ted  | Mosby  | 
+----+-----------+------------+ 

TalentCategory表

+----------------+------------------+------+-----+---------+----------------+ 
| Field   | Type    | Null | Key | Default | Extra   | 
+----------------+------------------+------+-----+---------+----------------+ 
| id    | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| created  | datetime   | NO |  | NULL |    | 
| talent_id  | int(10) unsigned | NO |  | NULL |    | 
| talent_name_id | int(11)   | NO |  | NULL |    | 
| is_active  | tinyint(1)  | NO |  | 1  |    | 
+----------------+------------------+------+-----+---------+----------------+ 

TalentName表

+--------------+------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+--------------+------------------+------+-----+---------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| created  | date    | NO |  | NULL |    | 
| name   | varchar(128)  | NO |  | NULL |    | 
| slug   | varchar(255)  | NO |  | NULL |    | 
| talent_count | int(11)   | NO |  | NULL |    | 
+--------------+------------------+------+-----+---------+----------------+ 

这个表将包含行如

+----+-------------------+-----------------+ 
| id | name    | slug   | 
+----+-------------------+-----------------+ 
| 1 | actor/actress | actor-actress | 
| 2 | dancer   | dancer   | 
| 3 | model    | model   | 
| 4 | singer/musician | singer-musician | 
+----+-------------------+-----------------+ 

和TalentMedia表

+--------------+------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+--------------+------------------+------+-----+---------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| created  | datetime   | NO |  | NULL |    | 
| talent_id | int(10) unsigned | NO |  | NULL |    | 
| media_id  | int(10) unsigned | NO |  | NULL |    | 
| is_cover  | tinyint(1)  | NO |  | 0  |    | 
| is_avatar | tinyint(1)  | NO |  | 0  |    | 
| like_count | int(11)   | NO |  | 0  |    | 
| view_count | int(11)   | NO |  | 0  |    | 
| is_published | tinyint(1)  | NO |  | 0  |    | 
| is_deleted | tinyint(1)  | NO |  | 0  |    | 
| is_approved | tinyint(1)  | NO |  | 0  |    | 
| is_suspended | tinyint(1)  | NO |  | 0  |    | 
+--------------+------------------+------+-----+---------+----------------+ 

人才hasMany TalentCategory belongsTo TalentName

人才hasMany个TalentMedia

我试图实现

SELECT 

Talent.id, 
Talent.firstname, 
Talent.lastname, 
TalentCategory.id, 
TalentCategory.talent_id, 
TalentCategory.talent_name_id, 
TalentName.name, 
TalentName.id, 
TalentMedia.talent_id, 
TalentMedia.media_id, 
TalentMedia.is_suspended, 
TalentMedia.is_avatar, 
TalentMedia.is_cover 


FROM talents AS Talent 

JOIN talent_talents AS TalentCategory ON TalentCategory.talent_id = Talent.id 
JOIN talent_names AS TalentName ON TalentName.id = TalentCategory.talent_name_id 
JOIN talent_medias AS TalentMedia ON TalentMedia.talent_id = Talent.id 

WHERE TalentName.id = 4 AND TalentMedia.is_suspended != 1 AND TalentMedia.is_cover !=1 AND TalentMedia.is_avatar = 1 
GROUP BY Talent.id 

select all talents which is a singer/musician that avatar is not suspended 

这里有一个SqlFiddle描述从我的控制器所需的输出

,这样我可以实现它在我的分页程序设置。我一直在尝试没有运气的一切。

我尝试custom find typescustom query pagination但我不太了解文档。

请帮我就如何实现这一目标

+0

您的数据库结构看起来有点过。如果一个人才拥有多个TalentCategory,那么你应该分别获取Talent和TalentCategory否? – 2014-09-27 20:15:05

+0

@AngelS。莫雷诺呃,我不太明白你的问题,一个天赋可以有很多talent_categories(巴尼斯汀是一个演员,一个歌手和一个舞者),这就是为什么我必须这样映射它。我更新了我的,希望它有助于解释 – littlechad 2014-09-27 20:28:49

+0

如果你喜欢,请考虑遵循这个简单的两步骤行动:1.如果你还没有这样做(你没有),提供*适当的* DDLs(和/或sqlfiddle),这样我们可以更容易地复制问题。 2.如果您尚未这样做,请提供与步骤1中提供的信息相对应的所需结果集。 – Strawberry 2014-09-27 21:17:25

回答

0

我设法通过创建一个自定义分页得到这个工作,我不知道这是正确的做法,至少我的解决方案适用于现在

首先,我覆盖蛋糕的paginatepaginateCount方法

public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { 
    $recursive = -1; 
    $sql_query = $this->paginateQuery; //same query as [SqlFiddle][1] 

    if(!empty($conditions)){ 
     $sql_query .= 'WHERE '; 
     foreach ($conditions as $key => $cond) { 
      $cond_str[] = $key .' '. $cond; 
     } 
     $sql_query .=implode(" AND ", $cond_str)." "; 

    } 

    $sql_query .= "GROUP BY Talent.id "; 
    $sql_query .= "ORDER BY Talent.id DESC "; 
    $sql_query .= "LIMIT " . (($page - 1) * $limit) . ', ' . $limit; 

    $results = $this->query($sql_query); 
    $this->virtualFields['fullname'] = 'CONCAT(Talent.firstname, " ", Talent.lastname)'; 
    return $results; 
} 

public function paginateCount($conditions = null, $recursive = 0, $extra = array()) { 
    $recursive = -1; 
    $sql_query = $this->paginateQuery; //same query as [SqlFiddle][1] 

    if(!empty($conditions)){ 
     $sql_query .= 'WHERE '; 
     foreach ($conditions as $key => $cond) { 
      $cond_str[] = $key .' '. $cond; 
     } 
     $sql_query .=implode(" AND ", $cond_str)." "; 

    } 

    $sql_query .= "GROUP BY Talent.id "; 
    $sql_query .= "ORDER BY Talent.id DESC "; 

    $this->recursive = $recursive; 
    $this->virtualFields['fullname'] = 'CONCAT(Talent.firstname, " ", Talent.lastname)'; 
    $results = $this->query($sql_query); 
    return count($results); 
} 

$this->paginateQuery;的值是相同的查询SqlFiddle

我还需要改变$conditions有点,因为它来作为一个数组,所以我需要以某种方式连接到一些可接受的条件数组。

在我的控制器接下来

,我叫像

$this->Paginator->settings = array(
    'Talent' => array(
     'limit' => 1, 
     'conditions' => array(
      'TalentCategory = ' => 3, 
      'TalentMedia.is_suspended !=' => 1, 
      'TalentMedia.is_cover  !=' => 1, 
      'TalentMedia.is_avatar = ' => 1, 
      //and any other conditions related to the joined table 
     ) 
    ) 
); 
$this->set('talents', $this->Paginator->paginate('Talent'));