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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8438660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:42:26  来源:igfitidea点击:

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

phpyii

提问by MEM

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:

我希望在我的布局 main.php 页面上列出一些类别名称。由于布局没有任何关联的控制器或模型,我希望在类别模型上创建一个像这样的静态方法:

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;

        }

回答by aslingga

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

也许这个答案可以帮助你。首先,您必须创建一个 Widget,以便您可以更有效地使用它。

First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.

首先创建一个新的小部件。假设名称是CategoryWidget. 将此小部件放在 components 目录下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。把它放在下面protected/components/views

category.php

类别.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') ?>

...

回答by tonino.j

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:

Yii 已经通过将菜单项分配给 column2 侧边栏菜单来做到这一点,从视图来看,如下所示:

$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.

您可以在 gii crud 创建的所有创建/更新视图中看到它。