牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2013-12-04 投稿人: 浏览次数:1439

首先在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"]的形式。


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。