Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output rows as array #120

Open
aolko opened this issue Aug 16, 2016 · 1 comment
Open

output rows as array #120

aolko opened this issue Aug 16, 2016 · 1 comment

Comments

@aolko
Copy link

aolko commented Aug 16, 2016

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)

@surferxo3
Copy link

$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'];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants