It should only unset the data if you are creating the User and it's missing 
the 'file' key. If you are updating, you shouldn't *need* the 'file' key.

That said, you can probably get away with passing an empty (or a path to a 
default image) file key when you are creating the User using a hidden 
field. This should allow the coupler to continue with the save. This may 
save some empty garbage data that is probably unwanted.

The other solution would be to detach the Coupler behavior at runtime when 
you don't want it to "force" you to have an image.

The whole point of the Coupler is to tightly couple the physical image to 
the record, so if the image doesn't exist neither should the record. I tend 
to create a separate table for attachments for this very reason. A user 
should be allowed to exist without an image.

On Tuesday, February 28, 2012 5:42:11 AM UTC-8, Jeremy Burns wrote:
>
> I'm using David Persson's media plugin. I have a users table that 
> contains the fields necessary to link to an image file, but a user 
> does not have to have an image. When I include the Media.Coupler 
> behaviour in the User model User->create always fails on User->save 
> when the user does not have an image because of the beforeSave in 
> coupler.php: 
>
>         function beforeSave(&$Model) { 
>                 if (!$Model->exists()) { 
>                         if (!isset($Model->data[$Model->alias]['file'])) { 
>                                  unset($Model->data[$Model->alias]); 
>  <<<--- clears down my User 
> data 
>                                 return true; 
>                         } 
>                 } else { 
>
> This is unsetting the User data because the file key is missing. If I 
> comment out the unset my User->save is successful. The Media.Coupler 
> is working perfectly well for another table that hasMany images in an 
> associated table. 
>
> Is there a setting I need to configure or am I doing something else 
> wrong?

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to