Hi all,
I just ran into this same problem - I want the image upload to be optional,
but don't want to go to the trouble of creating another Model (I had this
system originally, and am trying to improve things by bringing the image
into the main model!)
Anyway, I found a sneaky way to have one's cake:
---
// IN CONTROLLER
// Images are optional
*if ($this->request->data['Model']['file']['name'] == '') {*
* $this->Model->saveField('file',*
* 'PATH_TO_SOME_DEFAULT_IMAGE');*
*}*
if ($this->Model->save($this->request->data)) {
...
This works** because you add the file field after the new model is created,
but before the beforeSave() stuff kicks in.
The end result - it thinks you already have a file, so doesn't check - then
overwrites it with a blank value anyway. I don't think it even matters what
default value you enter, doesn't even have to be a valid image location....
End result : no file, valid save.
** AFAICS, better explanations welcome!
Cheers, A
On Wednesday, February 29, 2012 4:23:04 PM UTC, jeremyharris wrote:
>
> What about setting an empty hidden field in the view, like
>
> echo $this->Form->hidden('Model.file', array('value' => ''));
>
> I'd probably be more prone to just copy and paste the Coupler to my own
> behaviors and modify it, or create a new small behavior that is just
> dedicated to this task.
>
> On Wednesday, February 29, 2012 7:49:01 AM UTC-8,
> [email protected]:
>>
>> I tried it earlier by adding the following to the end of the beforeSave()
>> in our Users model:
>>
>> if (!isset($this->data['User']['file'])) { $this->data['User']['file'] =
>> null; }
>>
>> But when I tested it I discovered that the beforeSave() in the Media
>> Coupler was firing before the beforeSave() in our Users model, so it was
>> too late to add the empty 'file' key to the array as the 'User' had already
>> been unset by the Coupler. I've therefore had to roll the change back and
>> go back to the drawing board for now.
>>
>> Keith Gorman
>> *Class Outfit*
>> **
>> [email protected]
>> www.classoutfit.com
>>
>> On 29 Feb 2012, at 15:21, jeremyharris wrote:
>>
>> I probably didn't take your reply the right way. Hopefully I wasn't too
>> harsh myself.
>>
>> Anyway, hopefully you'll get a response from David soon. Did you ever get
>> to try out passing an empty 'file' key in the form (i.e., a hidden field)?
>> That way, the key would exist and the Coupler wouldn't unset the data. I
>> don't think this is a very elegant solution either, but I was curious if
>> you tried it out.
>>
>> On Tuesday, February 28, 2012 8:41:39 PM UTC-8, Jeremy Burns wrote:
>>>
>>> Gosh my reply sounded harsh on you Jeremy - that wasn't my intention.
>>> We've commented out the line that unsets the data and it *all seems fine*,
>>> which also feels dirty. The user records are saving correctly, we can
>>> subsequently add images to user records and all other dedicated image
>>> tables that use the coupler are functioning too. I did reach out to David
>>> yesterday, hoping to learn more from him so that we can implement this
>>> properly and will report back.
>>>
>>> Jeremy Burns
>>> Class Outfit
>>>
>>> http://www.classoutfit.com
>>>
>>> On 28 Feb 2012, at 21:15:25, jeremyharris wrote:
>>>
>>> I didn't say I agreed with how it worked, I was just explaining what I
>>> think the author's intended use case was. The question was if you were
>>> missing a setting or doing anything wrong. You weren't, so I just threw out
>>> some ideas based on how the Coupler works. Feel free to take it up with the
>>> author or create a Coupler that works for you.
>>>
>>> I'm glad that an empty image doesn't create garbage data, that thought
>>> was based off of what the Coupler does down the road (check the file and
>>> populate dirname and basename).
>>>
>>> On Tuesday, February 28, 2012 12:06:23 PM UTC-8, Jeremy Burns wrote:
>>>>
>>>> I understand what you are saying but I don't think it's the full
>>>> answer. It's a perfectly valid use case to have a table that has an
>>>> optional image in it without the need to have a separate join table. The
>>>> absence of the image doesn't create any garbage data; the user row is
>>>> still
>>>> valid. A bio snap for a user is good example. For the plugin to simply
>>>> clear the whole data array because the optional image fields aren't
>>>> populated is nuts. If the fields are empty simply return true and let the
>>>> main model get on with its work.
>>>>
>>>> Jeremy Burns
>>>> Class Outfit
>>>>
>>>> http://www.classoutfit.com
>>>>
>>>> On 28 Feb 2012, at 15:45:29, jeremyharris wrote:
>>>>
>>>> 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
>>>> [email protected] For more options, visit this
>>>> group at http://groups.google.com/group/cake-php
>>>>
>>>>
>>>>
>>> On Tuesday, February 28, 2012 12:06:23 PM UTC-8, Jeremy Burns wrote:
>>>>
>>>> I understand what you are saying but I don't think it's the full
>>>> answer. It's a perfectly valid use case to have a table that has an
>>>> optional image in it without the need to have a separate join table. The
>>>> absence of the image doesn't create any garbage data; the user row is
>>>> still
>>>> valid. A bio snap for a user is good example. For the plugin to simply
>>>> clear the whole data array because the optional image fields aren't
>>>> populated is nuts. If the fields are empty simply return true and let the
>>>> main model get on with its work.
>>>>
>>>> Jeremy Burns
>>>> Class Outfit
>>>>
>>>> http://www.classoutfit.com
>>>>
>>>> On 28 Feb 2012, at 15:45:29, jeremyharris wrote:
>>>>
>>>> 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
>>>> [email protected] For more options, visit this
>>>> group at http://groups.google.com/group/cake-php
>>>>
>>>>
>>>>
>>> --
>>> 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
>>> [email protected] For more options, visit this
>>> group at http://groups.google.com/group/cake-php
>>>
>>>
>>>
>> On Tuesday, February 28, 2012 8:41:39 PM UTC-8, Jeremy Burns wrote:
>>>
>>> Gosh my reply sounded harsh on you Jeremy - that wasn't my intention.
>>> We've commented out the line that unsets the data and it *all seems fine*,
>>> which also feels dirty. The user records are saving correctly, we can
>>> subsequently add images to user records and all other dedicated image
>>> tables that use the coupler are functioning too. I did reach out to David
>>> yesterday, hoping to learn more from him so that we can implement this
>>> properly and will report back.
>>>
>>> Jeremy Burns
>>> Class Outfit
>>>
>>> http://www.classoutfit.com
>>>
>>> On 28 Feb 2012, at 21:15:25, jeremyharris wrote:
>>>
>>> I didn't say I agreed with how it worked, I was just explaining what I
>>> think the author's intended use case was. The question was if you were
>>> missing a setting or doing anything wrong. You weren't, so I just threw out
>>> some ideas based on how the Coupler works. Feel free to take it up with the
>>> author or create a Coupler that works for you.
>>>
>>> I'm glad that an empty image doesn't create garbage data, that thought
>>> was based off of what the Coupler does down the road (check the file and
>>> populate dirname and basename).
>>>
>>> On Tuesday, February 28, 2012 12:06:23 PM UTC-8, Jeremy Burns wrote:
>>>>
>>>> I understand what you are saying but I don't think it's the full
>>>> answer. It's a perfectly valid use case to have a table that has an
>>>> optional image in it without the need to have a separate join table. The
>>>> absence of the image doesn't create any garbage data; the user row is
>>>> still
>>>> valid. A bio snap for a user is good example. For the plugin to simply
>>>> clear the whole data array because the optional image fields aren't
>>>> populated is nuts. If the fields are empty simply return true and let the
>>>> main model get on with its work.
>>>>
>>>> Jeremy Burns
>>>> Class Outfit
>>>>
>>>> http://www.classoutfit.com
>>>>
>>>> On 28 Feb 2012, at 15:45:29, jeremyharris wrote:
>>>>
>>>> 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
>>>> [email protected] For more options, visit this
>>>> group at http://groups.google.com/group/cake-php
>>>>
>>>>
>>>>
>>> On Tuesday, February 28, 2012 12:06:23 PM UTC-8, Jeremy Burns wrote:
>>>>
>>>> I understand what you are saying but I don't think it's the full
>>>> answer. It's a perfectly valid use case to have a table that has an
>>>> optional image in it without the need to have a separate join table. The
>>>> absence of the image doesn't create any garbage data; the user row is
>>>> still
>>>> valid. A bio snap for a user is good example. For the plugin to simply
>>>> clear the whole data array because the optional image fields aren't
>>>> populated is nuts. If the fields are empty simply return true and let the
>>>> main model get on with its work.
>>>>
>>>> Jeremy Burns
>>>> Class Outfit
>>>>
>>>> http://www.classoutfit.com
>>>>
>>>> On 28 Feb 2012, at 15:45:29, jeremyharris wrote:
>>>>
>>>> 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
>>>> [email protected] For more options, visit this
>>>> group at http://groups.google.com/group/cake-php
>>>>
>>>>
>>>>
>>> --
>>> 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
>>> [email protected] For more options, visit this
>>> group at http://groups.google.com/group/cake-php
>>>
>>>
>>>
>> --
>> 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
>> [email protected] For more options, visit this group
>> at http://groups.google.com/group/cake-php
>>
>>
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php