j4mie/idiorm

save() fails silently if you have select()ed some fields, but not the id

Opened this issue · 6 comments

Given the following table

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(254) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

With the data

INSERT INTO `users` (`id`, `email`)
VALUES ('1', 'foo@example.com');

And the following code

$user = ORM::for_table('users')
    ->where('id', 1)
    ->select('email')
    ->find_one()
;

$user->email = 'bar@example.com';

$success = $user->save();

$success is true, but the database will not have reflected this change (email will still be foo@example.com).

get_last_query() indicates that the following query is being executed

UPDATE `users` SET `email` = 'bar@example.com' WHERE `id` = ''

Notice that it could not find the id value to place in the query.

A work around is to remove the select() call altogether, or at least call select('id'), but I still think this is a bug.

This should probably throw an exception. Can you or anyone prepare a pull request for this issue? I don't have the time at the moment unfortunately.

@treffynnon should this be closed before #205 is merged or another solution committed?

It will be closed after #205 is merged. At the moment I don't see any issue
with merging your pull request - just gotta find the time to do it!

That's what I'm saying... you've closed it already.

Ah, I have? I got disturbed mid-way through working on it the other day so
probably didn't finish what I was I doing. Not to worry the pull request
acts as the open ticket for now anyway.

No worries.