Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

php - Yii - how to retrieve model data into a layout page?

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

and then in the main layout

<?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 ?

Here is the full 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;

        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

May be this answer can help you. First you must create a Widget so you can use it more effectively.

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   
        ));
    }
}

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') ?>

...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...