【问题标题】:Yii - how to retrieve model data into a layout page?Yii - 如何将模型数据检索到布局页面中?
【发布时间】:2014-07-25 07:37:33
【问题描述】:

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

public static function getHeaderModels()
{
   // get all models here
   return $models;
}

然后在主布局中

<?php
$models = Category::getHeaderModels();
foreach($models as $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;

        }

【问题讨论】:

    标签: php yii


    【解决方案1】:

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

    首先创建一个新的小部件。假设名称是CategoryWidget。将此小部件放在组件目录protected/components下。

    class CategoryWidget extends CWidget {
    
        public function run() {
            $models = Category::model()->findAll();
    
            $this->render('category', array(
                'models'=>$models   
            ));
        }
    }
    

    然后为这个小部件创建一个视图。文件名为 category.php。 放在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; ?>
    

    然后从您的主布局中调用此小部件。

    ma​​in.php

    // your code ...
    
    <?php $this->widget('CategoryWidget') ?>
    
    ...
    

    【讨论】:

      【解决方案2】:

      如果我没记错的话,您还可以将视图中可用的任何变量传递给布局。 您只需从具有变量的视图中执行此操作。 这就是问题所在:您需要在控制器中声明将接收您的值的变量,如下所示:

      <?php
      
      class MyController extends Controller
      {
      
          public $myvariable;
      

      在此之后,您将把您的模型或其他任何东西分配给您视图中的这个公共变量, 像这样:

      $this->myvariable = $modeldata;
      

      将模型数据分配给控制器的公共属性后, 您可以轻松地在布局中显示它,例如

      echo $this->myvariable;
      

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-02
        • 2020-07-16
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 2020-10-15
        • 1970-01-01
        相关资源
        最近更新 更多