Yii - how to retrieve model data into a layout page?(Yii - 如何将模型数据检索到布局页面中?)
问题描述
我希望在我的布局 main.php 页面上列出一些类别名称.由于布局没有任何关联的控制器或模型,我希望在类别模型上创建一个像这样的静态方法:
I wish to list some Categories name on my layout main.php page. Since the layout doesn't have any associated controller or model, I wish to create a static method like this on Category model:
public static function getHeaderModels()
{
// get all models here
return $models;
}
然后在主布局中
<?php
$models = Category::getHeaderModels();
foreach($models as $model)
{
// ....
}
?>
我的问题是一个非常基本的问题:如何从模型中检索这些类别名称?
My question is a very basic one: How can I retrieve those category names from the model ?
这是完整的模型:
class Category extends CActiveRecord {
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function tableName() {
return 'category';
}
public function rules() {
return array(
array('parent_id', 'numerical', 'integerOnly' => true),
array('name', 'length', 'max' => 255),
array('id, parent_id, name', 'safe', 'on' => 'search'),
);
}
public function relations() {
return array(
'users' => array(self::MANY_MANY, 'User', 'categories(category_id, user_id)'),
);
}
public function scopes()
{
return array(
'toplevel'=>array(
'condition' => 'parent_id IS NULL'
),
);
}
public function attributeLabels() {
$id = Yii::t('trans', 'ID');
$parentId = Yii::t('trans', 'Parent');
$name = Yii::t('trans', 'Name');
return array(
'id' => $id,
'parent_id' => $parentId,
'name' => $name,
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('parent_id', $this->parent_id);
$criteria->compare('name', $this->name, true);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
}
public static function getHeaderModels() {
//what sintax should I use to retrieve the models here ?
return $models;
}
推荐答案
也许这个答案可以帮到你.首先,您必须创建一个 Widget,以便您可以更有效地使用它.
May be this answer can help you. First you must create a Widget so you can use it more effectively.
首先创建一个新的小部件.假设名称是 CategoryWidget
.将此小部件放在组件目录protected/components
下.
First Create a new widget. Let say the name is CategoryWidget
. Put this widget under components directory protected/components
.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
然后为这个小部件创建一个视图.文件名为 category.php.把它放在 protected/components/views
Then create a view for this widget. The file name is category.php.
Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
然后从您的主布局调用这个小部件.
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>
...
这篇关于Yii - 如何将模型数据检索到布局页面中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Yii - 如何将模型数据检索到布局页面中?
基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01