prashanna
(Prashannajabara)
June 25, 2013, 5:57am
1
Hello and good morning every one.
I’m facing a problem in passing data of a model from a controller action after finding it using condition.
My controller action is like this.
[b]public function actionView($id) {
[indent]if (Yii::app()->user->checkRole() == "Client") {
[indent]$models = BloodCounts::model()->findAll(array('condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id']))));
$this->render('view', array(
'model'=>$models,
));[/indent]
} else {
[indent]$this->render('view', array(
'model' => $this->loadModel($id),
));[/indent]
}[/indent]
}
[/b]
and in view:-
[b]<table id="bloodViewTable">
[indent]<tr>
[indent]<td class="firstTD">Date</td>
<td class="middleTD">Y/M/D</td>
<?php
$count = BloodCounts::model()->count(array('condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id']))));
for ($i = 0; $i < $count; $i++) {
?>
<td class="dataTD"><?php echo $model->date ?></td> [size="5"][i][b]// here this echo dose not gets the data?[/b][/i][/size]
<?php } ?>[/indent]
</tr>
<tr>
[indent]<td class="firstTD">WBC</td>
<td class="middleTD">/mm</td>
<?php for ($i = 0; $i < $count; $i++) { ?>
<td class="dataTD"><?php echo $model->WBC ?></td>
<?php } ?>[/indent]
</tr>[/indent]
…
</table>[/b]
A client has multiple records in bloodcount of different date. In view i don’t find the data of the client.
count gives the correct count for loop. When I print like print_r($models) in action then it prints the whole array with data but when i try to fetch it in view i the all columns get printed without data. i’ve attached files of my viev as well. where the client say john has 3 records in bloodcounts.
mirunho
(D Mirecki)
June 25, 2013, 6:15am
2
Hello and good morning every one.
I’m facing a problem in passing data of a model from a controller action after finding it using condition.
My controller action is like this.
public function actionView($id) {
[indent]if (Yii::app()->user->checkRole() == "Client") {
[indent]$models = BloodCounts::model()->findAll(array('condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id']))));
$this->render('view', array(
'model'=>$models,
));[/indent]
} else {
[indent]$this->render('view', array(
'model' => $this->loadModel($id),
));[/indent]
}[/indent]
}
and in view:-
<table id="bloodViewTable">
[indent]<tr>
[indent]<td class="firstTD">Date</td>
<td class="middleTD">Y/M/D</td>
<?php
$count = BloodCounts::model()->count(array('condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id']))));
for ($i = 0; $i < $count; $i++) {
?>
<td class="dataTD"><?php echo $model->date ?></td> // try to var_dump model->date to see what is inside
<?php } ?>[/indent]
</tr>
<tr>
[indent]<td class="firstTD">WBC</td>
<td class="middleTD">/mm</td>
<?php for ($i = 0; $i < $count; $i++) { ?>
<td class="dataTD"><?php echo $model->WBC ?></td>
<?php } ?>[/indent]
</tr>[/indent]
.........
</table>
$model->date in Your view is an array so I dont see why You want to display array in for loop ?
You need to get 1 record from this array, isnt it ?
And I dont know why You do count inside view ? It should be in controller.
public function actionView($id) {
if (Yii::app()->user->checkRole() == "Client") {
$models = BloodCounts::model()->findAll(array('condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id']))));
$this->render('view', array(
'model'=>$models,
));
} else {
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
}
findAll gives a 2D array
$this->loadModel($id) gives a 1D array
just var_dump or print_r the $model in the view , you will get better idea
prashanna
(Prashannajabara)
June 25, 2013, 7:26am
4
Thank you all for your quick reply. I solved the problem but i think it contradicts MVC. I found that my view was unable to get model data from controller so i copied that findAll method in my view.
what i excatly did was in view.
<table id="bloodViewTable">
<tr>
<td class="firstTD">Date</td>
<td class="middleTD">Y/M/D</td>
<?php
$count = BloodCounts::model()->count(array('condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id']))));
$model = BloodCounts::model()->findAll(array(
'condition' => 'user_iduser=:userId',
'params' => array(':userId' => intval($_GET['id'])),
));
for ($i = 0; $i < $count; $i++) {
?>
<td class="dataTD"><?php echo $model[$i]->date; ?></td>
<?php } ?>
</tr>
<tr>
<td class="firstTD">WBC</td>
<td class="middleTD">/mm</td>
<?php for ($i = 0; $i < $count; $i++) { ?>
<td class="dataTD"><?php echo $model[$i]->WBC; ?></td>
<?php } ?>
</tr>
and i got the desired result.
$this->render('view', array(
'model' => $this->loadModel($id),
));
if you used this
you will get as $model