Reputation:
I have a table containing fields
id, vehicle_id, vehicle_rating_type, vehicle_rating, rating_time
I want to get values based in vehicle_id
, so I fired query
$id = Yii::$app->getRequest()->getQueryParam('id');
$data = VehicleRating::find()->where(['vehicle_id' => $id]);
Now when I prints $data
array in view file using print_r
function I get following result.
yii\db\ActiveQuery Object (
[sql] =>
[on] =>
[joinWith] =>
[select] =>
[selectOption] =>
[distinct] =>
[from] =>
[groupBy] =>
[join] =>
[having] =>
[union] =>
[params] => Array ( )
[_events:yii\base\Component:private] => Array ( )
[_behaviors:yii\base\Component:private] => Array ( )
[where] => Array (
[vehicle_id] => 1
)
[limit] =>
[offset] =>
[orderBy] =>
[indexBy] =>
[modelClass] => backend\models\VehicleRating
[with] =>
[asArray] =>
[multiple] =>
[primaryModel] =>
[link] =>
[via] =>
[inverseOf] =>
)
How to retrive values from this array to show in view file? Say I want to say vehicle_id
and vehicle_rating
. How to print it?
Upvotes: 1
Views: 6752
Reputation: 25312
You should simply execute the query, e.g. :
$rating = VehicleRating::find()->where(['vehicle_id' => $id])->one();
echo $rating->vehicle_rating;
Or if you want array instead of object :
$rating = VehicleRating::find()->where(['vehicle_id' => $id])->asArray()->one();
echo $rating['vehicle_rating'];
Read more : http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#querying-data
Upvotes: 1
Reputation:
found the solution i added ->one() at the end of query and it works
$data = VehicleRating::find()->where(['vehicle_id' => $id])->one();
Upvotes: 0
Reputation: 2012
Use query params.
$vehicleRatingQuery = VehicleRating::find()->where('vehicle_id = :vehicleId', [
':vehicleId' => $id
]);
$vehicleRatingQueryParams = $vehicleRatingQuery->params;
Upvotes: 0