Sometime we may need auth data for inserting in bforeSave method, i know it break the MCV rule but we sometime we need it for doing faster.
I used it in beforeSave method in a module
function beforeSave(){ App::import('Component', 'SessionComponent'); $session = new SessionComponent(); // say we just retrieve the auth data $auth = $session->read("Auth.user"); // here i used to add row value $this->data['ModelName']['field_name'] = $auth['login_user_id']; return true; }
In this way we can use full session component data here. 🙂
Thanks for this tip. Works for me. I could find no other way to pass some conditions to my custom validation rule other than to break the MCV in this way. I needed to validate isunique for a field, but only test against a certain subset of the table, not the whole table.
Throws an error:
Argument 1 passed to Component::__construct() must be an instance of ComponentCollection, none given
Only works with cakephp 1.x, in 2.x you get the error capetownseo has
in 2.x you may need to do something like this
App::import(‘Component’, ‘SessionComponent’);
$auth = SessionComponent::read(“User”);
SessionComponent::read() is not a static method, and invoking statically will produce errors. Checking the source, SessionComponent::read() is a dumb-wrapper for CakeSession::read(); so the better solution would be
App::uses(‘CakeSession’, ‘Model/Datasource’);
$auth = CakeSession::read(‘User’);
6 years later, and still fighting it.
How To asign AuthuserId in model as
tableprefix
Every weekend i used to visit this website, as i
wish for enjoyment, since this this web page conations in fact pleasant funny material too.