Question

Does anyone know of a workaround within php-activerecord to allow object relationships to update via mass assignment?

Loaded object

$obj = Model::find(array(
                   'conditions'=>array('id=?', $id), 
                   'include'=>array('Relationship_one', 'relationship_two')
                   ));

the code

$obj->update_attributes(--post data--);

Will update object fine, but not its releationships.

Était-ce utile?

La solution

Not much of an answer, but I've fixed this problem by making a function that is called with post data that

  • Does some sanity checks on the post data because we can (apart from model sanity checks, just to be able to give more precise errors)
  • Sets appropriate fields in this model
  • Calls its sister-function in the models relationships.

The advantage you have is that you can create these relationsships too! If a relationship doesn't exist (example: the first time you add something in a one to many relationship), you can update the 'base' model, but cannot update the association: you need to create one first. You can check for that in your models function, and then call the relation-model's function to finish the job.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top