php - Codeigniter Datamapper ORM issue with array and object? -
i have block code:
// creat object $privilege = new privilege(); // privileges $privilege->get_iterated(); $privileges = $privilege->all_to_array(array('id', 'name', 'description')); // user privileges $user_privileges = $privilege->get_user_privileges(array('id' => (int) $id), false); // if user has privilege marked 1, // otherwise marked 0 foreach ($privileges $key => $item) { foreach ($user_privileges $value) { $privileges[$key]['has_privilege'] = (in_array($value, $item) == true) ? 1 : 0; } }
my problem: $privileges contains privileges in cms , $user_privileges contains privileges of specific user. compare $privileges $user_privileges: if user has privilege marked 1 else marked 0 parse view
$this->_data['privileges'] = $privileges;
in view, must use $privileges['...'](array) show result want use: $privileges->....(object) it.
how can that? thank much.
if omit $privilege->all_to_array(...)
line can still assign new properties on objects created.
just change assignment syntax in loops this:
$privilege = new privilege(); // important! // don't use get_iterated if want iterate on results more once! $privilege->get(); $user_privileges = $privilege->get_user_privileges(array('id' => (int) $id), false); foreach ($privileges $privilege) { foreach ($user_privileges $value) { // individual $privilage instances wont cloned iteration // when add has_privilage property "remember" // when iterate on same $privilages object once again in vew $privilege->has_privilege = (in_array($value, $item) == true) ? 1 : 0; } }
in view can write this:
<?php foreach ($privileges $privilege): ?> <?php print $privilege->name ?> user <?php print $privilage->has_privilage ? 'have it' : 'dont have it' ?>. <?php endforeach; ?>
Comments
Post a Comment