CakePHP 2.5复杂通过可包含的密钥查找过滤

时间:2014-09-27 19:28:56

标签: mysql pagination cakephp-2.0 cakephp-2.3

我有4张桌子相互连接

人才表

+--------------------+------------------+------+-----+---------+----------------+
| 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

来自我的控制器,以便我可以在我的paginator设置中实现它。我一直在尝试一切,没有运气。

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

请帮我解决如何实现这个目标

1 个答案:

答案 0 :(得分:0)

我设法通过创建自定义分页来实现这一点,我不知道这是否是正确的方法,至少我的解决方案现在适用

首先我覆盖Cake的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'));