Commit 924837f0 authored by Jens Segers's avatar Jens Segers

Making progress on fixing relations

parent 3e26e05b
<?php namespace Jenssegers\Mongodb\Relations; <?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Builder;
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
{ {
/** /**
...@@ -28,6 +30,14 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo ...@@ -28,6 +30,14 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
$this->query->whereIn($key, $this->getEagerModelKeys($models)); $this->query->whereIn($key, $this->getEagerModelKeys($models));
} }
/**
* @inheritdoc
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
return $query;
}
/** /**
* Get the owner key with backwards compatible support. * Get the owner key with backwards compatible support.
*/ */
......
<?php namespace Jenssegers\Mongodb\Relations; <?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
class BelongsToMany extends EloquentBelongsToMany class BelongsToMany extends EloquentBelongsToMany
{ {
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getHasCompareKey()
{
return $this->getForeignKey();
}
/**
* @inheritdoc
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
return $query;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -25,6 +44,14 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -25,6 +44,14 @@ class BelongsToMany extends EloquentBelongsToMany
return $columns; return $columns;
} }
/**
* @inheritdoc
*/
protected function shouldSelect(array $columns = ['*'])
{
return $columns;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -260,6 +287,14 @@ class BelongsToMany extends EloquentBelongsToMany ...@@ -260,6 +287,14 @@ class BelongsToMany extends EloquentBelongsToMany
return $this->foreignKey; return $this->foreignKey;
} }
/**
* @inheritdoc
*/
public function getQualifiedForeignKeyName()
{
return $this->foreignKey;
}
/** /**
* Format the sync list so that it is keyed by ID. (Legacy Support) * Format the sync list so that it is keyed by ID. (Legacy Support)
* The original function has been renamed to formatRecordsList since Laravel 5.3 * The original function has been renamed to formatRecordsList since Laravel 5.3
......
...@@ -5,6 +5,26 @@ use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; ...@@ -5,6 +5,26 @@ use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany;
class HasMany extends EloquentHasMany class HasMany extends EloquentHasMany
{ {
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getHasCompareKey()
{
return $this->getForeignKeyName();
}
/**
* @inheritdoc
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
$foreignKey = $this->getHasCompareKey();
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
}
/** /**
* Add the constraints for a relationship count query. * Add the constraints for a relationship count query.
* *
...@@ -16,7 +36,7 @@ class HasMany extends EloquentHasMany ...@@ -16,7 +36,7 @@ class HasMany extends EloquentHasMany
{ {
$foreignKey = $this->getHasCompareKey(); $foreignKey = $this->getHasCompareKey();
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true); return $query->select($foreignKey)->where($foreignKey, 'exists', true);
} }
/** /**
...@@ -35,14 +55,4 @@ class HasMany extends EloquentHasMany ...@@ -35,14 +55,4 @@ class HasMany extends EloquentHasMany
return $query->where($this->getHasCompareKey(), 'exists', true); return $query->where($this->getHasCompareKey(), 'exists', true);
} }
/**
* Get the plain foreign key.
*
* @return string
*/
public function getPlainForeignKey()
{
return $this->getForeignKey();
}
} }
...@@ -5,6 +5,36 @@ use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; ...@@ -5,6 +5,36 @@ use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne;
class HasOne extends EloquentHasOne class HasOne extends EloquentHasOne
{ {
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getForeignKeyName()
{
return $this->foreignKey;
}
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getHasCompareKey()
{
return $this->getForeignKeyName();
}
/**
* @inheritdoc
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
$foreignKey = $this->getForeignKeyName();
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
}
/** /**
* Add the constraints for a relationship count query. * Add the constraints for a relationship count query.
* *
...@@ -14,9 +44,9 @@ class HasOne extends EloquentHasOne ...@@ -14,9 +44,9 @@ class HasOne extends EloquentHasOne
*/ */
public function getRelationCountQuery(Builder $query, Builder $parent) public function getRelationCountQuery(Builder $query, Builder $parent)
{ {
$foreignKey = $this->getHasCompareKey(); $foreignKey = $this->getForeignKeyName();
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true); return $query->select($foreignKey)->where($foreignKey, 'exists', true);
} }
/** /**
...@@ -33,16 +63,6 @@ class HasOne extends EloquentHasOne ...@@ -33,16 +63,6 @@ class HasOne extends EloquentHasOne
$key = $this->wrap($this->getQualifiedParentKeyName()); $key = $this->wrap($this->getQualifiedParentKeyName());
return $query->where($this->getHasCompareKey(), 'exists', true); return $query->where($this->getForeignKeyName(), 'exists', true);
}
/**
* Get the plain foreign key.
*
* @return string
*/
public function getPlainForeignKey()
{
return $this->getForeignKey();
} }
} }
...@@ -28,7 +28,7 @@ class MorphTo extends EloquentMorphTo ...@@ -28,7 +28,7 @@ class MorphTo extends EloquentMorphTo
$query = $instance->newQuery(); $query = $instance->newQuery();
return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); return $query->whereIn($key, $this->gatherKeysByType($type))->get();
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment