在控制器yii中创建模型对象

在控制器yii中创建模型对象,yii,Yii,有谁能告诉我如何在yii中的构造函数中创建模型对象吗。我写的代码如下 <?php class DistributorsController extends Controller { public $layout = '//layouts/column4'; public $defaultAction = null; public function __construct() { echo '<br>Into constructor';

有谁能告诉我如何在yii中的构造函数中创建模型对象吗。我写的代码如下

<?php
class DistributorsController extends Controller
{
   public $layout = '//layouts/column4';
   public $defaultAction = null;

   public function __construct()
   {
     echo '<br>Into constructor';
     parent::__construct('Distributors','distributors');
   }
   public function actionDistributors()
   {
     $this->render("ChannelMask");      
   }
 }


但是它只显示“Into constructor”字符串,视图没有显示在我的浏览器中。

您需要在控制器中调用模型

创建一个模型,然后在控制器中,像这样调用它:

Distributor::model()->findAll($criteria); //many models


如果要创建新模型,请与任何其他位置一样:

$model = new Distributors();

如果要使用现有数据填充模型,则:

$model = Distributor::model()->findAll(); // all there is

$model = $this->loadModel($id, 'Distributors');
$model = Distributor::model()->findAll(); // all there is
$model = Distributor::model()->findByPk($id); // find by primary key