Yii - 如何将模型数据检索到布局页面中?

2022-01-04 00:00:00 php 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.

ma​​in.php

// your code ...

<?php $this->widget('CategoryWidget') ?>

...

相关文章