I'm having a problem deleting a child record in a has_many association. I want to delete the child record ( address ) when the parent ( company ) is saved. No-mater what I do, the associate id for the parent is set to null but the child record is not deleted.
var $has_many = array('addresses');
var $belongs_to = array('company'=>array('dependent'=>'destroy'));
elseif (preg_match('/^-\d+$/', $this->params['company']['addresses'][$address_counter]['id']))
{
/* remove the delete flag from the id */
$this->params['company']['addresses'][$address_counter]['id'] =
trim($this->params['company']['addresses'][$address_counter]['id'],'-');
/* call $has_many->delete(&$object) */
$this->company->address->delete(
$this->company->address->build($this->params['company']['addresses'][$address_counter])
);
}
else
{
/* update address */
$this->company->addresses[$address_counter]->setAttributes(
$this->params['company']['addresses'][$address_counter]
);
}
if($this->Request->isPost() && $this->company->save())
{
$this->flash['notice'] = $this->t('Company was successfully updated.');
$this->redirectTo(array('action' => 'show', 'id' => $this->company->getId()));
}
belongs_to doesn't have a 'dependent'-option.
var $has_many = array('addresses'=>array('dependent'=>'destroy'));
$this->company->address->delete(...->build()); is conflicting. the object you want to delete must be an ActiveRecord already created or read from the db.
something from the ->addresses-collection or from a ->find(id)-operation.
...
I updated the models and stepped through AkHasmany->delete() with a debugger and found that $allow_dependency is always false which prevents $Associated[$k]->destroy() from being called. Not sure why or how to write a test for it.
Line 303: $allow_dependency = isset($AssociatedModel->$owner_type->_associatedAs)
&& $AssociatedModel->$owner_type->_associatedAs == 'belongsTo'
&& $AssociatedModel->$owner_type->getAssociationOption('dependent') ;
...
Line 309: case 'destroy':
Line 310: $success = $allow_dependency && $Associated[$k]->destroy() ? $success : false;
It looks like the last condtion, && $AssociatedModel->$owner_type->getAssociationOption('dependent')
causes the failure, since the owner (belongs_to) does not have a 'dependent' option.
ouch,
I had a false positive test on this. thanks for the ticket.
1 to 5 of 5