热搜:NVER 

YII 框架有关问题

2024-05-17 12:45:01
 YII 框架有关问题

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 表是一对多的关系.
希望对你有帮助.
祝你好运.
------解决方案--------------------

PHP code

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

PHP code

$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 '
'; }