nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
309 stars 59 forks source link

DbalMapper::remove fails when entity has differently arranged primary properties compared to attributes in database storage #296

Closed pavelkovar closed 6 years ago

pavelkovar commented 6 years ago

For example we have table _user_tocourse with attributes: _userid, _courseid and _roleid, and entity:

/**
 * @property-read array $id {primary-proxy}
 * @property Course|null $course {primary} {m:1 Course::$users}
 * @property User|int $user {primary} {m:1 User::$courses}
 * @property Role|string $role {primary} {m:1 Role::$users}
 */
class UserToCourse extends AbstractEntity
{
}

Then code snippet:

$found = $this->repository->getBy([
    'this->course->id' => 'course_1',
    'this->user->id' => '123',
    'this->role->id' => 'role_1'
]);

if ($found !== null) {
    $this->repository->removeAndFlush($found);
}

fails with exception:

Nextras\Dbal\QueryException

ERROR:  invalid input syntax for integer: "course_1"
LINE 1: DELETE FROM "user_to_course" WHERE "user_id" = 'course_1' AND "course_id" = 123 AND "role_id" = 'role_1'

When I change order of entity properties to match order of attributes in database storage then everything works as expected.

hrach commented 6 years ago

Hi, thank you for the detailed bug-report! I will take a closer look soon. In the mean time you may try dev-master. I think I've already fixed this, it's not only released in stable version: https://github.com/nextras/orm/commit/64ed252c30cef247519803dd7131db264aaf6fa2

pavelkovar commented 6 years ago

Hi, unfortunately it doesn't work even on dev-master. Incorrect behavior is caused by this line since result of getPersistedId is ordered by entity properties and result of getStoragePrimaryKey is ordered by columns in database storage.

hrach commented 6 years ago

Thanks for checking it. I will fix it this week.

hrach commented 6 years ago

Fixed, thanks a lot. https://github.com/nextras/orm/commit/bad39470bcf690627e4179f9f29d8b67475bca32