Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

yii

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;

        }
like image 488
MEM Avatar asked Dec 08 '11 22:12

MEM


People also ask

What is model yii2?

Models are part of the MVC architecture. They are objects representing business data, rules and logic. You can create model classes by extending yii\base\Model or its child classes.

What is controller in yii?

Controllers are part of the MVC architecture. They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.


2 Answers

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

...
like image 114
aslingga Avatar answered Sep 29 '22 19:09

aslingga


If I'm not mistaken, you can also pass any variable available in a view, on to the layout. You just do it from the view that has your variable. This is the catch: you need to declare the variable which will receive your value, in the controller, like this:

<?php

class MyController extends Controller
{

    public $myvariable;

After this, you will assign your model or whatever to this public variable inside your view, like this:

$this->myvariable = $modeldata;

After you have assigned your model data to controller's public attribute, you can easily display it inside your layout e.g.

echo $this->myvariable;

Yii already does this by assigning menu items to column2 sidebar menu, from view, like this:

$this->menu=array(
    array('label'=>'List Item', 'url'=>array('index')),
    array('label'=>'Manage Item', 'url'=>array('admin')),
);

You can see it in all create/update views that gii crud creates.

like image 41
tonino.j Avatar answered Sep 29 '22 18:09

tonino.j