duanlong4890 2013-12-11 09:57
浏览 22

Yii范围相关模型

I have Page model which HAS_MANY Attachment In Page model:

public function relations()
{
    return array(
        'attachments'=>array(self::HAS_MANY, 'Attachment', 'parent_id'),
    )
}

and I am looking for a way to do some scoping on these attachments.

In the PageController I have:

    $model = Page::model()->with(array('attachments'))->findByAttributes(array('slug' => $slug))

For example in the Page view I would like to:

  • get all attachments: $model->attachments (this works fine), but also I need:
  • get all published attachments (all with status = 1)
  • get first promoted attachment (the first one with promoted = 1)
  • get only the images (mime_type in_array 'image/jpeg', 'image/gif', ...)
  • get all other files (everything that is not an image)

and any combination of them. Ex: the first promoted and published image

I guess that the best option is to do it without any extra queries and just filter the $model->attachments , but is it possible?

Edit:

there is one pages table and another attachments table

in the attachments table I have: id, parent_id, file_name, mime_type, status, promoted

  • 写回答

1条回答 默认 最新

  • duanbin3021 2013-12-11 10:35
    关注

    you can do like this:

    $posts=Post::model()->with(array(
        'comments'=>array(
            'scopes'=>array('recently','approved')
        ),
    ))->findAll();
    // or since 1.1.7
    $posts=Post::model()->findAll(array(
        'with'=>array(
            'comments'=>array(
                'scopes'=>array('recently','approved')
            ),
        ),
    ));
    

    http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line