spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

updating primary key Value failed #210

Open Sysix opened 7 years ago

Sysix commented 7 years ago

Hey guys,

I think I have found a bug. When I update a Entity's primary key, it would be no changes at all. My fields are:

    public static function fields()
    {
        return [
            'scope' => [
                'type' => 'string',
                'primary' => true
            ],
            'is_default' => [
                'type' => 'boolean',
                'default' => 0
            ]
        ];
    }

My Update code is:

            /** @var \Spot\EntityInterface $entity */
            $entity->data(array(
'scope' => $newScope
));

            $scopes->getMapper()->update($entity);

Xdebug Outputs on Doctrine\DBAL\Connection on line 665:

$sql = "UPDATE `oauth_scopes` SET `scope` = ? WHERE `scope` = ?"
$params = array('newValue', 'newValue'); 

But it should be:

$params = array('newValue', 'oldValue'); 
tuupola commented 7 years ago

Primary keys should not contain business logic. Better to use unsigned integer as primary key. You can still have scope as unique.

Sysix commented 7 years ago

Hello,

yes I know its the "better" art of building a Database. But this way should be valid too. With "Vanilla-SQL" it works. So I think is should work with spot2 too.

I added a unsigned primary key id and now it works. But this problem is still active for the framework.