On Friday, 18 May 2012 11:56:24 UTC+2, Richard@Home wrote:
>
> CakePHP 2:
>
> Not noticed this behaviour before, is it by design? (Seems odd if it is...)
>
> CREATE TABLE IF NOT EXISTS `users` (
> `id` int(11) NOT NULL AUTO_INCREMENT,
> `username` varchar(32) NOT NULL,
> `password` varchar(64) NOT NULL,
> `is_active` tinyint(1) NOT NULL DEFAULT '0',
> PRIMARY KEY (`id`),
> UNIQUE KEY `username` (`username`),
> KEY `is_active` (`is_active`)
> ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
>
> (Notice in particular the NOT NULL flags on username & password:
>
Mysql doesn't enforce that - i.e. it won't error out if you try to insert a
row without passing those values (just to clarify).
>
>
> class User extends AppModel {
>
> public function activate($id) {
>
> $this->User->id = $id;
> $this->User->saveField('is_active', 1);
>
> }
>
> }
>
There's no validation in the above model - so cake isn't going to enforce
the equivalent of NOT NULL either.
>
> With an empty table:
>
> $this->User->activate(1);
>
> Creates a new user with an id of 1, an empty string for username and
> password, and is_active = 1;
>
> I expected this to fail as User.id 1 doesn't exist.
>
> $this->User->activate(99);
>
> Creates a new user with an id of 2, an empty string for username and
> password, and is_active = 1;
>
> I expected this to fail as User.id 99 doesn't exist.
>
Doesn't matter - you are calling save which will insert a row if it doesn't
exist. The model doesn't implement any "if it doesn't exist - it's an
error" logic.
> Even if I disregard that user 99 doesn't exist, I'd expect the new record
> to be created with an id of 99 (not 2)
>
I wouldn't expect that either, but you're calling saveField - not
(directly) save, since it doesn't exist it seems reasonable to not force
the id - since the table definition is autoincrement.
AD
--
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