Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    •  
      CommentAuthorriffed
    • CommentTimeFeb 28th 2008 edited
     

    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.

    The company model

    var $has_many   = array('addresses');
    

    The address model

    var $belongs_to = array('company'=>array('dependent'=>'destroy')); 
    

    In the company_controller

    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()));
    }
    
    • CommentAuthorKaste
    • CommentTimeFeb 28th 2008
     

    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.

    ...

    •  
      CommentAuthorriffed
    • CommentTimeFeb 29th 2008 edited
     

    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;
    
    •  
      CommentAuthorriffed
    • CommentTimeFeb 29th 2008 edited
     

    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.

    • CommentAuthorKaste
    • CommentTimeMar 1st 2008
     

    ouch,

    I had a false positive test on this. thanks for the ticket.

Add your comments
    Username Password
  • Format comments as