0

I'm beginner in YII. I want ask, I have two model with diferent table and no relation. example. Model User and Model Product. so in User Controller my code is

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));
}

But the record can't show, just attribute.

so, I can show user record and product record in one page. In CI I wrote

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

so, my question. how should I write to render multiple model with multiple table and without relation each other in one page in yii?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Prabu Karana
  • 302
  • 1
  • 4
  • 15

2 Answers2

0

you should use renderPartial() inside rendered view, because as it's documented, render():

Renders a view with a layout.

This method first calls renderPartial to render the view (called content view). It then renders the layout view which may embed the content view at appropriate place. In the layout view, the content view rendering result can be accessed via variable $content. At the end, it calls processOutput to insert scripts and dynamic contents if they are available.

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

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

}

then in view,

$this->renderPartial('application.views.Product.view',array('model'=>$model));
Developerium
  • 7,155
  • 5
  • 36
  • 56
0

(IDK this->renderPartial work, and i try used ur code but just one record appear, in fact i have 1000 more record )

i solved this problem with 2 diferent method for render multiple model with multiple table without relation.

first method i used widget.

create widget in components folder

class ProductWidget extends CWidget {
public function run() {
$models =      Product::model()->findAll(array('condition'=>'product_token="A"'));
$this->render('category', array(
'models'=>$models   
    ));
}
}

And then Create View for this widget in Components/View

<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->product_token; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

And last in Views (layouts/column) folder i write this

<?php $this->widget('ProductWidget'); ?>

And for second method (Function actionIndex in User Controller )

public function actionIndex() {
$models = Product::model()->findAll(array('condition'=>'Product_token="A"'))
$dataProvider=new CActiveDataProvider('User');
$this->render('index',array(
        'dataProvider'=>$dataProvider,
         'models' => $models,
));

AND Last in View. (Views/User/Index)

<?php foreach($models as $model):
echo $model->product_token;
endforeach;
?>

I think second method is better, u can add pagination or something.

Prabu Karana
  • 302
  • 1
  • 4
  • 15