yii2 - select query using raw query binding parameters and data reader

yii2
voted: 1
vote
php yii2 datareader array

yii2 select query using raw query binding parameters and data reader

<?php

    $items = Yii::$app->db->createCommand("SELECT id FROM item WHERE item_type_id = :item_type_id");
    $itemTypeId = $itemType->id;
    $items->bindParam(':item_type_id', $itemTypeId);
    $items = $items->query(); //query database into Data Reader object

    //Data Reader object can be accessed in foreach like this:

    $itemsArray = array();

    foreach($items as $item){
        $itemsArray[] = $item;
    }

   //can be also accesed like with while like this:

    $itemsArray = array();

    while($item = $items->query()){
        $itemsArray[] = $item;
    }

   //or can be loaded into array like this:

    $itemsArray = $items->readAll();
working url: not entered
version: 2
Leave a Comment:
Characters left:
No comments yet.