[eluser]macigniter[/eluser]
Sorry for posting again. I'm new to the DM class and still learning... The following problem is just driving me crazy:
In my application products can be assigned to clients by checking some checkboxes and saving the record. In the scenario where I remove all product relations from an existing client and then after saving add a product again, the client somehow gets a new user id although there are existing relations to other tables (e.g. users). Any idea why DM is increasing the client id?
The update function looks like this:
Code:
$c = new Client();
$c->get_by_id($id);
$p = new Product();
$p->where_related_client('id', $id);
$p->get();
// delete old product selection for this client
$c->delete($p->all);
$c->name = $this->input->post('name');
$c->descr = $this->input->post('descr');
$products = $this->input->post('product');
if (is_array($products))
{
$p = new Product();
$p->where_in('id', $products);
$p->get();
$c->save($p->all);
}
else
{
$c->save();
}
It looks like $c->delete($p->all) is where the problem is. I do find it strange though that even the relation from the client to the user table is being deleted... Any ideas?
UPDATE: I found the bug. Since the error only occured after adding a new product after all product/client relations have been previously deleted I had to add "if ($p->exists())" before the delete to prevent DM running a deletion on an empty relation table.
Quote:if ($p->exists()) $c->delete($p->all);