Featured post
php - Datamapper ORM/ CodeIgniter - using and displaying join tables -
i've got "steps" table, "id" , text of step named "step". have "customers" table "id" , other customer info. finally, have "customers_steps" join table "customer_id" , "step_id". goal have list of steps, , show ones completed. i'm stuck...
to make sure i'm not missing anything, in "customer" model, have
var $has_many = array ('step');
in "step" model, have
var $has_many = array('customer');
right now, i'm looping steps, looping through customer's steps see if match... it's lot of code, , know there has faster way, , i'm missing it:
$c = new customer(); $c->get_by_id(1); $c->step->get(); $s = new step(); $s->get(); foreach($s $step) { foreach($c $customer) { if($customer->step->id == $step->id) { $match = true; } } if($match) { echo "match - " . $step->step; } else { echo $step->step; } }
this works... can make better? in advance.
you have many-to-many relationship, you'll never able in 1 go.
ideally, need left join between steps , customers_steps, produce resultset steps in it, , null value steps not present specific customer id. since datamapper relationships, can't report relations aren't there.
you use
// customer , steps $c = new customer(1); $c->step->get(); // create list of assigned step id's $list = array(); foreach ($c->step $step) { $list[] = $step->id; } // steps not assigned $s = new step(); $s->where_not_in('id', $list); // @ point $c->steps contains matching steps // , $s steps don't match print_r($c->steps->all_to_array()); print_r($s->all_to_array());
- Get link
- X
- Other Apps
Comments
Post a Comment