时间:2021-07-01 10:21:17 帮助过:16人阅读
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 '
'; }
------解决方案--------------------
Tlist 表结构:
id INT 主键 递增
sortid INT 外键
title VARCHAR
Tsort 表结构:
id INT 主键 递增
name VARCHAR
$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 '
'; }