vrana/notorm

output rows as array

Opened this issue · 1 comments

aolko commented

how do i output all rows as an assoc array with relations?

$ormdb_channels = $ormdb->channel()
    ->select("*")
    ->where("channel.user_id=?", $_SESSION['user_id'])
;

//Get all the fields from a query
$ormdb_channel = $ormdb_channels->fetch();
$channel_arr = (array) $ormdb_channel->getIterator();

It outputs just fine, but what if i want to output only channels with $ormdb_channels->user["username"]?
i.e.

array (size=8)
  'id' => string '1' (length=1)
  'name' => string 'testtv' (length=6)
  'desc' => null
  'slug' => string 'testtv' (length=6)
  'settings' => null
  'is_live' => string '0' (length=1)
  'qi' => string '5.0' (length=3)
  'user_id' => string '1' (length=1) // maybe even remove this
  'username' => string 'test' (length=4) // <- user["username"]

case 2: same but with a whole related user

'user' => array(...)
    'username' => string 'test' (length=4)
    [...]

Upd:

//Define the table and get all the fields
$ormdb_channels = $ormdb->channel()->select("*")->where("channel.user_id=?", $_SESSION['user_id']);
$ormdb_channel = $ormdb_channels->fetch();
$channel_arr = (array) $ormdb_channel->getIterator();
$channel_arr["user"] = (array) $ormdb_channel->user->getIterator();

still want to know a correct way (and probably my way is getting only one row/field instead of multiple)

$user_channels = $ormdb_channels = $ormdb->channel()
->select("*")
->where("channel.user_id=?", $_SESSION['user_id']);

// way 1: loop result set
foreach (user_channels as $user_channel) {
echo $user_channel['username'];
}

// way 2: get result set as associative array
$user_channels_assoc = array_map('iterator_to_array', iterator_to_array($user_channels));
foreach (user_channels_assoc as $user_channel) {
echo $user_channel['username'];
}