Skip to content

Commit

Permalink
Crate (like Postgres and Sqlite) does not allow table prefix in updates.
Browse files Browse the repository at this point in the history
We remove table prefix that was added in
laravel/framework#26031
  • Loading branch information
Ratko Rudic committed Mar 5, 2019
1 parent 11ae628 commit e7026b5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/RatkoR/Crate/Eloquent/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace RatkoR\Crate\Eloquent;

use Illuminate\Database\Eloquent\Builder as BaseBuilder;

class Builder extends BaseBuilder
{
/**
* Add the "updated at" column to an array of values.
*
* Note: Crate (like postgres and sqlite) MUST not have table prefix in
* update query. It throws with:
* [SQLParseException: Column reference "my_table.update_at"
* has too many parts. A column must not have a schema or a table here.]
*
* Remove table prefix for updated_at field.
* Eq:
* update t1 set a = 1, t1.updated_at = 123456
* change to:
* update t1 set a = 1, updated_at = 123456
*
* See also: https://github.com/laravel/framework/pull/26031
*
* @param array $values
* @return array
*/
protected function addUpdatedAtColumn(array $values)
{
$values = parent::addUpdatedAtColumn($values);
$newValues = [];

$updatedAtColumn = $this->model->getUpdatedAtColumn();

foreach ($values as $field => $value) {
$isUpdateAtField = (strpos($field, '.' . $updatedAtColumn) !== false);

$isUpdateAtField ?
$newValues[$updatedAtColumn] = $value :
$newValues[$field] = $value;
}

return $newValues;
}
}

0 comments on commit e7026b5

Please sign in to comment.