2011-12-08 18 views
7

我希望在我的布局main.php页面上列出一些类别名称。 由于布局没有任何相关的控制器或模型,我希望在分类模型建立一个静态方法是这样的:Yii - 如何将模型数据检索到布局页面?

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; 

     } 

回答

19

可能是这样的回答可以帮助你。首先,您必须创建一个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; ?> 

然后调用您的主要布局这个小程序。

main.php

// your code ... 

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

... 
6

如果我没有记错的话,您还可以通过任何可用的变量在视图中,在布局。 你只是从具有你的变量的视图来做它。 这是陷阱:你需要声明将收到您的值的变量,在控制器中,像这样:

<?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创建/更新的观点。