Saturday, January 21, 2012

Using beforeValidate to edit fields of a hasMany Model?

Hi, i have this realtionship:

Comment hasMany CommentFile

On Comment's beforeValidation i have this:

function beforeValidation(){

foreach ($this->data['CommentFile'] as $k => &$f) {
if(!array_key_exists('filename', $f) || !$f['filename']['size'] )
unset($this->data['CommentFile'][$k]);
$f += array(
'batch_id' => $this->data['Comment']['batch_id'],
'class_id' => $this->data['Comment']['class_id'],
);
}

Please notice i add batch_id and class_id to the table, i need this
cause i want to query on Batches and Classes for Files. This is the
full relationship Batches hasMany ClassModel hasMany Comments hasMany
Files.

The problem is that even when i set that the fields arent saved. I
checked Model.php and notice that the $this->data is used for
validation and $data for save on saveAssociated, so it its a
duplicated array, and the one used ($data) doesnt have my
modifications.

I wonder if this is a bug, but i dont think so, but then, what is the
right place to do this? Please note im using Comment saveAll. And i
dont want to set this on view, i think this should be on model.

For the momment the solution i found is override the saveAssciated
function, but i want to ask what is the right place to put this code.

Arnold

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate