Php YII中一页中没有关系表的多个模型

Php YII中一页中没有关系表的多个模型,php,yii,model,renderpartial,Php,Yii,Model,Renderpartial,我是YII的初学者。 我想问,我有两个模型,不同的表,没有关系。 例子。模型用户和模型产品。 所以在用户控制器中,我的代码是 public function actionIndex() { $model= new Product; $dataProvider=new CActiveDataProvider('User'); $this->render('index',array( 'dataProvider'=>$dataProvider, )); $

我是YII的初学者。 我想问,我有两个模型,不同的表,没有关系。 例子。模型用户和模型产品。 所以在用户控制器中,我的代码是

public function actionIndex() {
$model= new Product;
$dataProvider=new CActiveDataProvider('User');
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
$this->renderPartial('application.views.Product.view',array('model'=>$model));
}
但是记录不能显示,只能显示属性

所以,我可以在一页中显示用户记录和产品记录。我在CI中写道

$data['user']= $this->user->record();
$data['product']=$this->product->record();
$this->load->view('index',$data)

所以,我的问题是。我应该如何编写以在yii中的一个页面中呈现具有多个表且彼此不相关的多个模型?

您应该在呈现视图中使用renderPartial(),因为作为,render():

使用布局渲染视图

此方法首先调用renderPartial来呈现视图(称为内容视图)。然后呈现布局视图,布局视图可以将内容视图嵌入到适当的位置。在布局视图中,可以通过变量$content访问内容视图渲染结果。最后,它调用processOutput来插入脚本和动态内容(如果它们可用)

public function actionIndex() {
$model= new Product;
$dataProvider=new CActiveDataProvider('User');

$this->render('applicationindex',array(
    'dataProvider'=>$dataProvider,
));

}
那么,

$this->renderPartial('application.views.Product.view',array('model'=>$model));
(IDK this->renderPartial work,我尝试使用您的代码,但只显示了一条记录,事实上我还有1000条记录)

我用2种不同的方法解决了这个问题,用多个表无关系地渲染多个模型

我使用widget的第一个方法。

public function actionIndex() {
$models = Product::model()->findAll(array('condition'=>'Product_token="A"'))
$dataProvider=new CActiveDataProvider('User');
$this->render('index',array(
        'dataProvider'=>$dataProvider,
         'models' => $models,
));
在组件文件夹中创建小部件

class ProductWidget extends CWidget {
public function run() {
$models =      Product::model()->findAll(array('condition'=>'product_token="A"'));
$this->render('category', array(
'models'=>$models   
    ));
}
}
然后在Components/View中为此小部件创建视图

<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->product_token; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
最后一次。(视图/用户/索引)


我认为第二种方法更好,你可以加上分页之类的

public function actionIndex() {
$models = Product::model()->findAll(array('condition'=>'Product_token="A"'))
$dataProvider=new CActiveDataProvider('User');
$this->render('index',array(
        'dataProvider'=>$dataProvider,
         'models' => $models,
));
<?php foreach($models as $model):
echo $model->product_token;
endforeach;
?>