Skip to content

Commit

Permalink
fixed for 5.4 (almost)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Apr 22, 2017
1 parent 843f885 commit f37f508
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/Mappable.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected function joinSegment(Builder $query, $segment, EloquentModel $parent)
? $parent->getMorphClass()
: $related->getMorphClass();

$join->where($relation->getMorphType(), '=', $morphClass);
$join->where($relation->getQualifiedMorphType(), '=', $morphClass);
}
});
}
Expand Down Expand Up @@ -306,11 +306,11 @@ protected function alreadyJoined(Builder $query, $table)
protected function getJoinKeys(Relation $relation)
{
if ($relation instanceof HasOne || $relation instanceof MorphOne) {
return [$relation->getForeignKey(), $relation->getQualifiedParentKeyName()];
return [$relation->getQualifiedForeignKeyName(), $relation->getQualifiedParentKeyName()];
}

if ($relation instanceof BelongsTo && !$relation instanceof MorphTo) {
return [$relation->getQualifiedForeignKey(), $relation->getQualifiedOtherKeyName()];
return [$relation->getQualifiedForeignKey(), $relation->getQualifiedOwnerKeyName()];
}

$class = get_class($relation);
Expand Down
14 changes: 7 additions & 7 deletions src/Relations/Joiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function getJoinClause(Model $parent, Relation $relation, $table, $typ
$join = (new Join($this->query, $type, $table))->on($fk, '=', $pk);

if ($relation instanceof MorphOneOrMany) {
$join->where($relation->getMorphType(), '=', $parent->getMorphClass());
$join->where($relation->getQualifiedMorphType(), '=', $parent->getMorphClass());
}

return $join;
Expand All @@ -153,10 +153,10 @@ protected function joinIntermediate(Model $parent, Relation $relation, $type)
{
if ($relation instanceof BelongsToMany) {
$table = $relation->getTable();
$fk = $relation->getForeignKey();
$fk = $relation->getQualifiedForeignKeyName();
} else {
$table = $relation->getParent()->getTable();
$fk = $table.'.'.$parent->getForeignKey();
$fk = $relation->getQualifiedFirstKeyName();
}

$pk = $parent->getQualifiedKeyName();
Expand All @@ -181,19 +181,19 @@ protected function getJoinKeys(Relation $relation)
}

if ($relation instanceof HasOneOrMany) {
return [$relation->getForeignKey(), $relation->getQualifiedParentKeyName()];
return [$relation->getQualifiedForeignKeyName(), $relation->getQualifiedParentKeyName()];
}

if ($relation instanceof BelongsTo) {
return [$relation->getQualifiedForeignKey(), $relation->getQualifiedOtherKeyName()];
return [$relation->getQualifiedForeignKey(), $relation->getQualifiedOwnerKeyName()];
}

if ($relation instanceof BelongsToMany) {
return [$relation->getOtherKey(), $relation->getRelated()->getQualifiedKeyName()];
return [$relation->getQualifiedRelatedKeyName(), $relation->getRelated()->getQualifiedKeyName()];
}

if ($relation instanceof HasManyThrough) {
$fk = $relation->getRelated()->getTable().'.'.$relation->getParent()->getForeignKey();
$fk = $relation->getQualifiedFarKeyName();

return [$fk, $relation->getParent()->getQualifiedKeyName()];
}
Expand Down
36 changes: 18 additions & 18 deletions tests/MetableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function meta_orders()
public function meta_whereExists()
{
$sql = 'select * from "metables" where exists (select 1 from "metables" where exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" > 10))';

$model = $this->getModel();
Expand All @@ -202,7 +202,7 @@ public function meta_whereExists()
public function meta_whereDates($type, $placeholder)
{
$sql = 'select * from "metables" where "name" = ? and exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and strftime(\''.$placeholder.'\', "meta_value") = ?)';

$query = $this->getModel()->where('name', 'jarek')->{"where{$type}"}('published_at', '=', 'date_value');
Expand All @@ -227,7 +227,7 @@ public function whereDateTypes()
public function meta_orWhereNull()
{
$sql = 'select * from "metables" where "name" = ? or not exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ?)';

$query = $this->getModel()->where('name', 'jarek')->orWhereNull('color');
Expand All @@ -242,7 +242,7 @@ public function meta_orWhereNull()
public function meta_orWhereNotNull()
{
$sql = 'select * from "metables" where "name" = ? or exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ?)';

$query = $this->getModel()->where('name', 'jarek')->orWhereNotNull('color');
Expand All @@ -257,7 +257,7 @@ public function meta_orWhereNotNull()
public function meta_whereNotNull()
{
$sql = 'select * from "metables" where "name" = ? and exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ?)';

$query = $this->getModel()->where('name', 'jarek')->whereNotNull('color');
Expand All @@ -272,7 +272,7 @@ public function meta_whereNotNull()
public function meta_whereNull()
{
$sql = 'select * from "metables" where "name" = ? and not exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ?)';

$query = $this->getModel()->where('name', 'jarek')->whereNull('color');
Expand All @@ -287,7 +287,7 @@ public function meta_whereNull()
public function meta_whereNotBetween()
{
$sql = 'select * from "metables" where "name" = ? and not exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" >= ? and "meta_value" <= ?)';

$query = $this->getModel()->where('name', 'jarek')->whereNotBetween('size', ['5','10']);
Expand All @@ -302,7 +302,7 @@ public function meta_whereNotBetween()
public function meta_whereBetween_numeric()
{
$sql = 'select * from "metables" where "name" = ? and exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" >= 5 and "meta_value" <= 10.5)';

$model = $this->getModel();
Expand All @@ -320,7 +320,7 @@ public function meta_whereBetween_numeric()
public function meta_whereBetween_string()
{
$sql = 'select * from "metables" where "name" = ? and exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" >= ? and "meta_value" <= ?)';

$query = $this->getModel()->where('name', 'jarek')->whereBetween('size', ['M','L']);
Expand All @@ -335,7 +335,7 @@ public function meta_whereBetween_string()
public function meta_orWhereBetween()
{
$sql = 'select * from "metables" where "name" = ? or exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" >= ? and "meta_value" <= ?)';

$query = $this->getModel()->where('name', 'jarek')->orWhereBetween('size', ['M','L']);
Expand All @@ -350,7 +350,7 @@ public function meta_orWhereBetween()
public function meta_orWhereNotBetween()
{
$sql = 'select * from "metables" where "name" = ? or not exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" >= ? and "meta_value" <= ?)';

$query = $this->getModel()->where('name', 'jarek')->orWhereNotBetween('size', ['M','L']);
Expand All @@ -365,7 +365,7 @@ public function meta_orWhereNotBetween()
public function meta_orWhereNotIn()
{
$sql = 'select * from "metables" where "name" = ? or not exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" in (?, ?, ?))';

$query = $this->getModel()->where('name', 'jarek')->orWhereNotIn('size', ['L', 'M', 'S']);
Expand All @@ -380,7 +380,7 @@ public function meta_orWhereNotIn()
public function meta_whereNotIn()
{
$sql = 'select * from "metables" where "name" = ? and not exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" in (?, ?, ?))';

$query = $this->getModel()->where('name', 'jarek')->whereNotIn('size', ['L', 'M', 'S']);
Expand All @@ -395,7 +395,7 @@ public function meta_whereNotIn()
public function meta_whereIn()
{
$sql = 'select * from "metables" where "name" = ? and exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" in (?, ?, ?))';

$query = $this->getModel()->where('name', 'jarek')->whereIn('size', ['L', 'M', 55]);
Expand All @@ -410,7 +410,7 @@ public function meta_whereIn()
public function meta_orWhereIn()
{
$sql = 'select * from "metables" where "name" = ? or exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" in (?, ?, ?))';

$query = $this->getModel()->where('name', 'jarek')->orWhereIn('size', ['L', 'M', 'S']);
Expand All @@ -425,7 +425,7 @@ public function meta_orWhereIn()
public function meta_orWhere()
{
$sql = 'select * from "metables" where "name" = ? or exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" = ?)';

$query = $this->getModel()->where('name', 'jarek')->orWhere('color', 'red');
Expand All @@ -440,7 +440,7 @@ public function meta_orWhere()
public function meta_where_numeric()
{
$sql = 'select * from "metables" where exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" > 5)';

$model = $this->getModel();
Expand All @@ -458,7 +458,7 @@ public function meta_where_numeric()
public function meta_where()
{
$sql = 'select * from "metables" where exists (select * from "meta_attributes" '.
'where "meta_attributes"."metable_id" = "metables"."id" and "meta_attributes"."metable_type" = ? '.
'where "metables"."id" = "meta_attributes"."metable_id" and "meta_attributes"."metable_type" = ? '.
'and "meta_key" = ? and "meta_value" = ?)';

$query = $this->getModel()->where('color', 'red');
Expand Down
1 change: 1 addition & 0 deletions tests/SearchableBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ public function getModel($driver = 'MySql')
$schema->shouldReceive('getColumnListing')->andReturn(['id', 'first_name', 'last_name']);
$connection = m::mock('Illuminate\Database\ConnectionInterface', ['getQueryGrammar' => $grammar, 'getPostProcessor' => $processor]);
$connection->shouldReceive('getSchemaBuilder')->andReturn($schema);
$connection->shouldReceive('getName')->andReturn($driver);
$resolver = m::mock('Illuminate\Database\ConnectionResolverInterface', ['connection' => $connection]);
$class = get_class($model);
$class::setConnectionResolver($resolver);
Expand Down

0 comments on commit f37f508

Please sign in to comment.