ThinkPHP之RelationModel设置子表查询条件

未结帖
0 1623
ajian lily 2017-01-22
悬赏:5飞吻
<?php
class PowenModel extends Model {
    protected $_validate = array(
        array('type','require','po文类型必须填写',1),
        array('channel_id','require','频道id必须填写',1),
        array('president_id','require','商户id必须填写',0),
        array('user_id','require','po主id必须填写',1),
        array('content','require','内容详情必须填写',0),
        array('city_id','require','po文类型必须填写',0),
        array('city_name','require','城市名必须填写',1),
        array('img_num','require','图片数量必须填写'),
    );
    protected $_auto = array(
        array('po_zan_count',0),
        array('po_reply_count',0),
        array('add_time', 'time', 1, 'function'),
        array('is_top',0),
        array('turn', 0),
        array('is_show',1),
    );

    public function setRelationModel($table,$link_table,$current_client_id){
        $newModel = new RelationModel($table);
        $link = $this->setLink($link_table,$current_client_id);
        $newModel->setProperty('_link',$link);


        return $newModel;
    }


    public function setLink($link_table,$current_client_id){
        $_link = array(
            'powen_reply'=> array(
                'mapping_type'=>HAS_MANY,
                'class_name'=>'powen_reply_'.$link_table,
                'foreign_key'=>'po_id',
                'mapping_order'=>'add_time desc',
                'condition'=>'replied_client_id is null and is_show=1',
                'mapping_limit'=>'4',
                'mapping_fields'=>'id,po_id,client_id,content,add_time',
            ),
            'powen_pic'=> array(
                'mapping_type'=>HAS_MANY,
                'class_name'=>'powen_pic_'.$link_table,
                'condition'=>'is_show=1',
                'foreign_key'=>'po_id',
            ),
            'powen_zan'=> array(
                'mapping_type'=>HAS_MANY,
                'class_name'=>'powen_zan_'.$link_table,
                'foreign_key'=>'po_id',
                'condition'=>'zan_client_id='.$current_client_id,
                'mapping_order'=>'add_time desc',
                'mapping_fields'=>'id,zan_client_id',
            ),


        );
        return  $_link;


    }

}
?>
$Powen = D('Powen');
//实例化关系模型
$PowenRelation = $Powen->setRelationModel('powen_'.$yearMonth,$yearMonth,$current_client_id);

//最新数据集合$latestLists
$allLists = $PowenRelation->relation(true)->where($where)->order('id desc')->limit(10)->select();


热忱回答0


最近热帖

近期热议

  1. javascript——prototype与__proto 9
  2. Mysql 中出现的Data truncated for column 3
  3. 在nginx中使用x-sendfile的解决方案 3
  4. 高版本jQuery面插件实现Ajax上传图片 1
  5. Thinkphp Socket.class.php 类的使用 1
  6. 使用ionic3创建第一个App 0
  7. ios-oc html5 0
  8. nginx.conf 0
  9. 基于ionic3.4.0的项目搭建 0
  10. php 缩略图 0