YII 框架问题
有人用过YII 框架没?里面的之间的通讯联系我不是清楚!求高人指点下
------解决方案--------------------
class Academic_list extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function relations()
{
return array(
'sorts'=>array(
self::BELONGS_TO, 'Academic_sort', 'sortid'
)
);
}
}
class Academic_sort extends CActiveRecord
{
public $subs = null;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function relations()
{
return array(
'lists'=>array(
self::HAS_MANY, 'Academic_list', 'id'
)
);
}
}
$row = Academic_list::model()->with('sorts')->findByPk($id);
$row->sorts->name;
------解决方案--------------------
以上 Academic_list 表和 Academic_sort 表是一对多的关系.
希望对你有帮助.
祝你好运.
------解决方案--------------------
class Tlist extends CActiveRecord{ public function relations() { return array( 'sorts'=>array( self::BELONGS_TO, 'Tsort', 'sortid', ) ); }}class Tsort extends CActiveRecord{ public function relations() { return array( 'lists'=>array( self::HAS_MANY, 'Tlist', 'id', ) ); }}$dataProvider = new CActiveDataProvider('Tlist', array( 'criteria'=>array(// 'condition'=>'status=1 AND tags LIKE :tags',// 'params'=>array(':tags'=>$_GET['tags']), 'with'=>array('sorts'), ), 'pagination'=>array( 'pageSize'=>5, 'currentPage'=>2, ),));$rows = $dataProvider->getData();foreach($rows as $row){ echo $row->id; echo '<br>';}<br><font color="#e78608">------解决方案--------------------</font><br>Tlist 表结构:<br>id INT 主键 递增<br>sortid INT 外键<br>title VARCHAR<br><br>Tsort 表结构:<br>id INT 主键 递增<br>name VARCHAR<br><br>
$dataProvider = new CActiveDataProvider('Tlist', array( 'criteria'=>array(// 'condition'=>'status=1 AND tags LIKE :tags',// 'params'=>array(':tags'=>$_GET['tags']), 'with'=>array('sorts'), ), 'pagination'=>array( 'pageSize'=>5, 'currentPage'=>1, ),));$rows = $dataProvider->getData();foreach($rows as $row){ echo $row->id; echo '---'; echo $row->sorts->name; echo '---'; echo $row->title; echo '<br>';}<div class="clear"> </div>