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

Making progress on fixing relations

parent 3e26e05b
<?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Builder;
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));
}
/**
* @inheritdoc
*/
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
return $query;
}
/**
* Get the owner key with backwards compatible support.
*/
......
<?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as 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
*/
......@@ -25,6 +44,14 @@ class BelongsToMany extends EloquentBelongsToMany
return $columns;
}
/**
* @inheritdoc
*/
protected function shouldSelect(array $columns = ['*'])
{
return $columns;
}
/**
* @inheritdoc
*/
......@@ -260,6 +287,14 @@ class BelongsToMany extends EloquentBelongsToMany
return $this->foreignKey;
}
/**
* @inheritdoc
*/
public function getQualifiedForeignKeyName()
{
return $this->foreignKey;
}
/**
* 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
......
......@@ -5,6 +5,26 @@ use Illuminate\Database\Eloquent\Relations\HasMany as 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.
*
......@@ -16,7 +36,7 @@ class HasMany extends EloquentHasMany
{
$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
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;
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.
*
......@@ -14,9 +44,9 @@ class HasOne extends EloquentHasOne
*/
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
$key = $this->wrap($this->getQualifiedParentKeyName());
return $query->where($this->getHasCompareKey(), 'exists', true);
}
/**
* Get the plain foreign key.
*
* @return string
*/
public function getPlainForeignKey()
{
return $this->getForeignKey();
return $query->where($this->getForeignKeyName(), 'exists', true);
}
}
......@@ -28,7 +28,7 @@ class MorphTo extends EloquentMorphTo
$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