Cakephp - 仅当id不为null时才加入

时间:2011-11-27 21:57:22

标签: cakephp cakephp-2.0

我有一个包含3 $ belongsTo引用键的活动表。我需要来自这些id的加入信息,即显示评论文本 - 我不想将文本存储两次......(对于帖子和主题都相同)。

活动表:id | post_id | comment_id | topic_id
在每一行中只设置了post_id OR comment_id OR topic_id,其他两个id字段为NULL。

因此,如果post_id = 55,comment_id = NULL,topic_id = NULL我得到这个:

Array
(
[0] => Array
    (
        [Activity] => Array
            (
                [id] => 1
                [post_id] => 55
                [comment_id] => 
                [topic_id] => 
            )

        [Post] => Array
            (
                [id] => 55
                [name] => Post #1
                [description] => This is Post #1.
                ...
            )

        [Comment] => Array
            (
                [id] => 
                [post_id] => 
                [titel] => 
                [description] => 
                ...
                [created] => 
                [modified] => 
            )

        [Topic] => Array
            (
                [id] => 
                ...
                [created] => 
                [modified] => 
            )
    )

[1] => Array
    (
        ...

只有在引用标识为NOT NULL时才有加入的方法吗?我不想在使用php for-each循环查找之后杀死空数组。

另一个想法是这个数据库表:id | activitytype_id | refid加入动态绑定必要的表,具体取决于activitytype_id。 - 这没效果......

这就是我想要的 - 可能吗?

Array
(
[0] => Array
    (
        [Activity] => Array
            (
                [id] => 1
                [post_id] => 55
                [comment_id] => 
                [topic_id] => 
            )

        [Post] => Array
            (
                [id] => 55
                [name] => Post #1
                [description] => This is Post #1.
                ...
            )
    )

[1] => Array
    (
        [Activity] => Array
            (
                [id] => 2
                [post_id] => 
                [comment_id] => 2
                [topic_id] => 
            )

        [Comment] => Array
            (
                [id] => 2
                [post_id] => 4
                [titel] => Blabla
                [description] => This is the comment description
                ...
                [created] => 2011-01-01 01:30:00
                [modified] => 2011-01-01 01:30:00
            )
    )

[2] => Array
    (
        ...

提前致谢! : - )

2 个答案:

答案 0 :(得分:2)

您需要查询数据库以找出哪些ID为null,然后再次查询数据库以获取相关数据。

$activity = $this->Activity->read(null, 1);
// some logic to find foreign key with non-null value
$activity[$model] = $this->{$model}->read(null, $id);

我不会浪费你的时间写两个问题;让CakePHP在单个查询中获取所有结果。 :)

$activity = $this->Activity->findById(1);

只需将其添加到模型中即可从结果中过滤掉空值:

public function afterFind($results, $primary = false) {
    return Hash::filter($results);
}

答案 1 :(得分:0)

您始终可以手动进行连接。你想要的是INNER JOIN而不是蛋糕完成的LEFT JOIN。你也可以做一个afterFind()来删除这样的东西。

在模型中使用find方法

function afterFind($results){
    foreach($results as $k => $result){
       if (empty($result['Post']['id'])){
            unset($results[$k]['Post']);
       }
       if (empty($result['Comment']['id'])){
            unset($results[$k]['Comment']);
       }
       if (empty($result['Topic']['id'])){
            unset($results[$k]['Topic']);
       }
    }
}

虽然联接是一个更直接的解决方案。

相关问题