Using Doctrine 1.2, I am having some issues deleting items from a Doctrine_Collection.
I have a Doctrine collection filled with transient Doctrine_Records. The $record->delete()
and $record->unlink()
functions don't seem to work properly here, as they use the record's IDs. (Which transient records don't have, since they don't appear in the database yet.)
Example (in which Company has many Employee)
/* Get a Company that has no Employees yet. */
$company = Doctrine::getTable('Company')->find(1);
/* Add some Employees */
$names = array('Arthur','Bob','Charlie');
foreach ($names as $name)
{
$employee = new Employee;
$employee->name = "Arthur"
$company->Employee->add($employee);
}
Now, before saving anything to the database, I have one employee in $emp
, which I want to remove from the collection.
$emp->delete() /* Does not work when $emp is transient. */
What does work is this, but I really doubt that is the way to go.
foreach($company->Employee as $key => $value)
if ($emp == $value)
{
$company->Employee->remove($key);
break;
}
This doesn't feel like the easiest way to do this. Is there a more recommended way of doing this?
I ended up creating a subclass for Doctrine_Collection, and implemented this function, which gave me exactly the behaviour I wanted.