Re: need a little help with model::saveAll function
Modifying data in the controller seems to me like a nasty workaround. The reason why your beforeValidate is failing to modify your data is because validation is only run when you call save. You are calling saveAll... so the data beforeValidate sees is in fact just a single record. You *could* override saveAll to unset the blank records, returning parent::saveAll($data, $options);, but I fear that would unnecessarily duplicate an awful lot of code to match the API. Why don't you just create a mySaveAll() ? [code] function mySaveAll($data) { foreach($data['InvoicesItem'] as $row) { ... } $options = array(... ); return $this->saveAll($data, $options); } [/code] On Nov 25, 3:23 pm, Ernesto wrote: > i'ts working! thank you very much > > On 24 Nov, 18:19, Johannes wrote: > > > i had exactly the same problem and solved it by removing empty lines > > in the beforeFilter() of the controller responsible for saving items. > > In my case its very specific you could apppend your logic to make it > > more universal. > > > p.s. im not sure about the following line in your example, because im > > not sure if !null is defined in php > > if (array_search(!null, $record)) $validRecords[] = $record; > > maybe im wrong but i dont think this filters your data correctly. > > > --- in my app i use: > > class InvoicesController extends AppController { > > function beforeFilter() { > > if (!empty($this->data)) { > > > // remove empty forms from data > > if (isset($this->data['InvoicesItem'])) { > > $data = array(); > > foreach ($this->data['InvoicesItem'] as > > $row) { > > if (!empty($row['quantity']) || > > !empty($row['price'])) { > > $data[] = $row; > > } > > } > > if (empty($data)) { > > unset($this->data['InvoicesItem']); > > } > > else { > > $this->data['InvoicesItem'] = $data; > > } > > } > > } > > > } > > ... > > > } > > > On Nov 24, 4:28 pm, Ernesto wrote: > > > > A - i already have a routine that *should* to that... but it isn't > > > working properly. here's the code > > > > function beforeValidate() { > > > $validRecords = array(); > > > foreach ($this->data as $model => $records) { > > > foreach ($records as $key => $record) { > > > if (is_numeric($key)) { > > > if (array_search(!null, $record)) $validRecords[] > > > = $record; > > > } else break; > > > $this->data[$model] = $validRecords; > > > } > > > } > > > return true; > > > > } > > > > B - my saveAll is already overwritten in AppModel and atm it's working > > > ok. i was just asking if it's possible to do that in a Behavior :) > > > > On 23 Nov, 20:52, Marcelo Andrade wrote: > > > > > On Mon, Nov 23, 2009 at 12:29 PM, Ernesto wrote: > > > > > (..) > > > > > A - user can insert just 2 or 3 rows, it's not necesasry to fill all > > > > > the 10 "Item" rows. saveAll tries to save even empty insertions. Blank > > > > > rows = validation errors. is there any way to avoid this? > > > > > You'll have to loop $this->data and unset the entries when > > > > you get blank fields. > > > > > > B - is it pobbile to create a behavior that forces "validate" => > > > > > "first" to every saveAll call? > > > > > You can override the saveAll method in your AppModel, > > > > changing the validate entry in the options to 'first' always. > > > > > Best regards. > > > > > -- > > > > MARCELO DE F. ANDRADE > > > > Belem, PA, Amazonia, Brazil > > > > Linux User #221105 Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com 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?hl=en
Re: need a little help with model::saveAll function
i'ts working! thank you very much On 24 Nov, 18:19, Johannes wrote: > i had exactly the same problem and solved it by removing empty lines > in the beforeFilter() of the controller responsible for saving items. > In my case its very specific you could apppend your logic to make it > more universal. > > p.s. im not sure about the following line in your example, because im > not sure if !null is defined in php > if (array_search(!null, $record)) $validRecords[] = $record; > maybe im wrong but i dont think this filters your data correctly. > > --- in my app i use: > class InvoicesController extends AppController { > function beforeFilter() { > if (!empty($this->data)) { > > // remove empty forms from data > if (isset($this->data['InvoicesItem'])) { > $data = array(); > foreach ($this->data['InvoicesItem'] as $row) > { > if (!empty($row['quantity']) || > !empty($row['price'])) { > $data[] = $row; > } > } > if (empty($data)) { > unset($this->data['InvoicesItem']); > } > else { > $this->data['InvoicesItem'] = $data; > } > } > } > > } > ... > > } > > On Nov 24, 4:28 pm, Ernesto wrote: > > > > > A - i already have a routine that *should* to that... but it isn't > > working properly. here's the code > > > function beforeValidate() { > > $validRecords = array(); > > foreach ($this->data as $model => $records) { > > foreach ($records as $key => $record) { > > if (is_numeric($key)) { > > if (array_search(!null, $record)) $validRecords[] > > = $record; > > } else break; > > $this->data[$model] = $validRecords; > > } > > } > > return true; > > > } > > > B - my saveAll is already overwritten in AppModel and atm it's working > > ok. i was just asking if it's possible to do that in a Behavior :) > > > On 23 Nov, 20:52, Marcelo Andrade wrote: > > > > On Mon, Nov 23, 2009 at 12:29 PM, Ernesto wrote: > > > > (..) > > > > A - user can insert just 2 or 3 rows, it's not necesasry to fill all > > > > the 10 "Item" rows. saveAll tries to save even empty insertions. Blank > > > > rows = validation errors. is there any way to avoid this? > > > > You'll have to loop $this->data and unset the entries when > > > you get blank fields. > > > > > B - is it pobbile to create a behavior that forces "validate" => > > > > "first" to every saveAll call? > > > > You can override the saveAll method in your AppModel, > > > changing the validate entry in the options to 'first' always. > > > > Best regards. > > > > -- > > > MARCELO DE F. ANDRADE > > > Belem, PA, Amazonia, Brazil > > > Linux User #221105 Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com 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?hl=en
Re: need a little help with model::saveAll function
i had exactly the same problem and solved it by removing empty lines in the beforeFilter() of the controller responsible for saving items. In my case its very specific you could apppend your logic to make it more universal. p.s. im not sure about the following line in your example, because im not sure if !null is defined in php if (array_search(!null, $record)) $validRecords[] = $record; maybe im wrong but i dont think this filters your data correctly. --- in my app i use: class InvoicesController extends AppController { function beforeFilter() { if (!empty($this->data)) { // remove empty forms from data if (isset($this->data['InvoicesItem'])) { $data = array(); foreach ($this->data['InvoicesItem'] as $row) { if (!empty($row['quantity']) || !empty($row['price'])) { $data[] = $row; } } if (empty($data)) { unset($this->data['InvoicesItem']); } else { $this->data['InvoicesItem'] = $data; } } } } ... } On Nov 24, 4:28 pm, Ernesto wrote: > A - i already have a routine that *should* to that... but it isn't > working properly. here's the code > > function beforeValidate() { > $validRecords = array(); > foreach ($this->data as $model => $records) { > foreach ($records as $key => $record) { > if (is_numeric($key)) { > if (array_search(!null, $record)) $validRecords[] > = $record; > } else break; > $this->data[$model] = $validRecords; > } > } > return true; > > } > > B - my saveAll is already overwritten in AppModel and atm it's working > ok. i was just asking if it's possible to do that in a Behavior :) > > On 23 Nov, 20:52, Marcelo Andrade wrote: > > > On Mon, Nov 23, 2009 at 12:29 PM, Ernesto wrote: > > > (..) > > > A - user can insert just 2 or 3 rows, it's not necesasry to fill all > > > the 10 "Item" rows. saveAll tries to save even empty insertions. Blank > > > rows = validation errors. is there any way to avoid this? > > > You'll have to loop $this->data and unset the entries when > > you get blank fields. > > > > B - is it pobbile to create a behavior that forces "validate" => > > > "first" to every saveAll call? > > > You can override the saveAll method in your AppModel, > > changing the validate entry in the options to 'first' always. > > > Best regards. > > > -- > > MARCELO DE F. ANDRADE > > Belem, PA, Amazonia, Brazil > > Linux User #221105 Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com 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?hl=en
Re: need a little help with model::saveAll function
A - i already have a routine that *should* to that... but it isn't working properly. here's the code function beforeValidate() { $validRecords = array(); foreach ($this->data as $model => $records) { foreach ($records as $key => $record) { if (is_numeric($key)) { if (array_search(!null, $record)) $validRecords[] = $record; } else break; $this->data[$model] = $validRecords; } } return true; } B - my saveAll is already overwritten in AppModel and atm it's working ok. i was just asking if it's possible to do that in a Behavior :) On 23 Nov, 20:52, Marcelo Andrade wrote: > On Mon, Nov 23, 2009 at 12:29 PM, Ernesto wrote: > > (..) > > A - user can insert just 2 or 3 rows, it's not necesasry to fill all > > the 10 "Item" rows. saveAll tries to save even empty insertions. Blank > > rows = validation errors. is there any way to avoid this? > > You'll have to loop $this->data and unset the entries when > you get blank fields. > > > B - is it pobbile to create a behavior that forces "validate" => > > "first" to every saveAll call? > > You can override the saveAll method in your AppModel, > changing the validate entry in the options to 'first' always. > > Best regards. > > -- > MARCELO DE F. ANDRADE > Belem, PA, Amazonia, Brazil > Linux User #221105 Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com 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?hl=en
Re: need a little help with model::saveAll function
is there any example? i can't figure out where and when loop through $this->data On 23 Nov, 20:52, Marcelo Andrade wrote: > On Mon, Nov 23, 2009 at 12:29 PM, Ernesto wrote: > > (..) > > A - user can insert just 2 or 3 rows, it's not necesasry to fill all > > the 10 "Item" rows. saveAll tries to save even empty insertions. Blank > > rows = validation errors. is there any way to avoid this? > > You'll have to loop $this->data and unset the entries when > you get blank fields. > > > B - is it pobbile to create a behavior that forces "validate" => > > "first" to every saveAll call? > > You can override the saveAll method in your AppModel, > changing the validate entry in the options to 'first' always. > > Best regards. > > -- > MARCELO DE F. ANDRADE > Belem, PA, Amazonia, Brazil > Linux User #221105 -- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-...@googlegroups.com. 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?hl=en.
Re: need a little help with model::saveAll function
On Mon, Nov 23, 2009 at 12:29 PM, Ernesto wrote: > (..) > A - user can insert just 2 or 3 rows, it's not necesasry to fill all > the 10 "Item" rows. saveAll tries to save even empty insertions. Blank > rows = validation errors. is there any way to avoid this? You'll have to loop $this->data and unset the entries when you get blank fields. > B - is it pobbile to create a behavior that forces "validate" => > "first" to every saveAll call? You can override the saveAll method in your AppModel, changing the validate entry in the options to 'first' always. Best regards. -- MARCELO DE F. ANDRADE Belem, PA, Amazonia, Brazil Linux User #221105 -- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-...@googlegroups.com. 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?hl=.