Yii学习(7)----使用with关系

首先在model里面定义一个relations关系,如下:

public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'fishcenter'=>array(self::BELONGS_TO,'FishCenter','fcid'),
			//'typeid'=>array(self::BELONGS_TO,'CommonClass','id'),		
		);
	}

这里的关系被命名为fishcenter,下面我们在controller中使用这样的关系,

$new = Pond::model()->with(array(
			'fishcenter'=>array('fcid','address','fcname'),
		))->findAll(array(
			'select'=>array('pondid','pondname','address','price'),
			'order'=>'pondid DESC',
			'limit'=>10,
		));
这样获得的数据就是这样的结构:

$new=array(

'pondid'=>'',

'pondname'=>'',

......

fishcenter=>array(

'fcid'=>'',

'address'=>'',

......

)

);


所以说如果要获取fcid的数据,必须是:$new['fishcenter']['fcid']的形式。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值