Thursday, July 5, 2012

Re: ModelValidator fails on SaveAssociated in Plugin

It seems I found the problem, at least at first sight. It appears it is not sufficient to do a Find on the model, it is necessary to invoke either Set or Create on the model with the found (or newly created) record so that all the data gets properly populated. Then it works fine.

On Wednesday, July 4, 2012 7:00:29 PM UTC+2, tigr wrote:
Hi!

There is something I do not understand. I have a plugin where I save some data into related models. When I do it with a Save, everything works fine. As soon as I try to use SaveAssociated, I get this error (and it is interfering of course with the headers). Can someone confirm that this is either something silly or a bug, please?

Warning (2): Illegal offset type in isset or empty [CORE/Cake/Model/ModelValidator.php, line 391]
Code Context
        $exists = $model->exists();          $methods = $this->getMethods();          $fields = $this->_validationList($fieldList);
$model = object(AccountPassword) {}  $whitelist = array(  	'Account' => array(  		[maximum depth reached]  	),  	'AccountFlag' => array(  		[maximum depth reached]  	)  )  $validateList = array()  $f = array(  	(int) 0 => 'active'  )
ModelValidator::_validationList() - CORE/Cake/Model/ModelValidator.php, line 391  ModelValidator::errors() - CORE/Cake/Model/ModelValidator.php, line 248  ModelValidator::validates() - CORE/Cake/Model/ModelValidator.php, line 85  Model::validates() - CORE/Cake/Model/Model.php, line 3010  ModelValidator::validateAssociated() - CORE/Cake/Model/ModelValidator.php, line 141  Model::validateAssociated() - CORE/Cake/Model/Model.php, line 2322  Model::saveAssociated() - CORE/Cake/Model/Model.php, line 2186  AccountsController::delete() - APP/Plugin/Z/Controller/AccountsController.php, line 103  ReflectionMethod::invokeArgs() - [internal], line ??  Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 485  Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 186  Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 161  [main] - APP/webroot/index.php, line 92

Code is simple and straightforward:

$options = array(
'fieldList' => array(
'Account' => array(
'active'
),
'AccountFlag' => array(
'deleted',
'deleted_date'
)
)
);
$this->Account->saveAssociated(null, $options);

And the alternative of

$this->Account->save(null, array('fieldList' => array('Account' => array('active'))));
$this->Account->AccountFlag->save(null,
array('fieldList' => array('AccountFlag' => array('deleted', 'deleted_date'))));

works just fine.

Thanks!

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

No comments: