Re: saveAll() + blank records = headache

2012-01-11 Thread Ernesto
This isn't working :\

Here's my Behavior

?php

class CleanableBehavior extends ModelBehavior {

function beforeValidate ($model) { 

foreach ($model-data as $modelAlias = $modelData) {
if (isset($model-hasMany[$modelAlias])) {
  foreach ($modelData as $key = $record) {
 //here i loop through fields. if no fields are set 
i unset the $key
  }
  }
}

//here's the set() function  
$model-set($model-data);

return true;

}

}

?

-- 
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


Re: saveAll() + blank records = headache

2012-01-11 Thread jeremyharris
Oops, I meant to have you set it in the controller, like this:

// in the controller
$this-Model-set($this-data);
$this-Model-saveAll();

This puts the $data into Model-data (which honestly, saveAll should do). 
If this doesn't work then maybe something else is going on, because I've 
modified the data before just as you are.

Lastly, if all else fails, you place it in a model function instead:

// in the model
function clearEmpty($data) {
// unset fields
return $data;
}

// controller
$data = $this-Model-clearEmpty($this-data);
$this-Model-saveAll($data);

-- 
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


Re: saveAll() + blank records = headache

2012-01-11 Thread Ernesto
at this point it's simpler to override the saveAll() function in my AppModel

function saveAll ($data = null, $options = array()) {
//some loops to clear the unused records
return parent::saveAll($data, $options);
}

but that's what i'm trying to avoid :)

-- 
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


Re: saveAll() + blank records = headache

2012-01-11 Thread jeremyharris
Sadly, yeah. It's truly odd that modifying $this-data didn't stick. 

-- 
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


saveAll() + blank records = headache

2012-01-10 Thread Ernesto
Hi all

i have 2 models
Customer hasMany Phonenumber

my customers/add view has a form with a html table where a user can enter 
multiple phone numbers (up to five) for each customer.

Each phone number is referenced as 

Phonenumber.0.number
Phonenumber.0.description

Phonenumber.1.number
Phonenumber.1.description
and so on.

When i fire the saveAll() function the Phonenumber model throws many 
validation errors beacuse the field number is mandatory.
That's because Cake is processing the empty records.
I solved overriding the saveAll() function, looping through the data array 
and unsetting the blank records, but this solution looks kinda nasty to me.

is there any way to do this from a behavior?

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread jeremyharris
Just don't require it in your validation.

$validate = array(
'birth_date' = array(
'rule' = 'phone',
'required' = false, // allows the key not to be set at all (that is, it is 
not submitted via the form)
'allowEmpty' = true //allows an empty response as well
)
);

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread Ernesto
A Phonenumber without the number is pretty pointless :\

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread Ernesto
A Phonenumber without the number is pretty pointless :\

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread jeremyharris
I think I misunderstood what you wanted..

So if you present them with a list of 5 phone number fields and they only 
fill out 2, you just want the two, right? In that case, yeah just unsetting 
in the controller or write a little behavior or even just the beforeSave 
method on the model will do.

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread Ernesto
manipulating data in the controller doesn't sound MVC-ish to me.
I was searching a way to avoid that. My first though was coding this in a 
Behavior but... how?

i can't put the code in beforeSave because if validation fails (and it 
will, because as stated above the number field is mandatory) this 
callback will not be triggered

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread jeremyharris
Whoops. Use beforeValidate instead then.

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread Ernesto
i've already made a try with beforeValidate.

the problem here is that once the saveAll() is called and the model data is 
passed as argument any mod to the data array will be useless.
The saving process will go on with the array passed to saveAll

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread jeremyharris
Hm, I'm not super familiar with the internals of saveAll, but it still uses 
the save() method which sets the data property. That's the only way 
validates() knows what to look at. You should be safe modifying $this-data 
in beforeValidate.

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread jeremyharris
Or try using set() the set the data instead of passing the data through the 
function to see if that works for you.

-- 
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


Re: saveAll() + blank records = headache

2012-01-10 Thread Ernesto
uhm... i'll try the set() function.

thanks for the support :)

-- 
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