Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
Improvements to relationship methods and args
Browse files Browse the repository at this point in the history
- Adding 'relation' param to BelongsTo relations
- Adding 'localKey' to MorphOne/MorphMany (refs #204)
- Creating MorphToMany/MorphedByMany relations, as per #232 / @mklenk
  • Loading branch information
igorsantos07 committed May 10, 2015
1 parent 5e8ef76 commit 20fc03a
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/LaravelBook/Ardent/Ardent.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ abstract class Ardent extends Model {
* @see \Illuminate\Database\Eloquent\Model::morphTo
* @see \Illuminate\Database\Eloquent\Model::morphOne
* @see \Illuminate\Database\Eloquent\Model::morphMany
* @see \Illuminate\Database\Eloquent\Model::morphToMany
* @see \Illuminate\Database\Eloquent\Model::morphedByMany
*
* @var array
*/
Expand All @@ -190,6 +192,10 @@ abstract class Ardent extends Model {

const MORPH_MANY = 'morphMany';

const MORPH_TO_MANY = 'morphToMany';

const MORPHED_BY_MANY = 'morphedByMany';

/**
* Array of relations used to verify arguments used in the {@link $relationsData}
*
Expand All @@ -198,7 +204,8 @@ abstract class Ardent extends Model {
protected static $relationTypes = array(
self::HAS_ONE, self::HAS_MANY,
self::BELONGS_TO, self::BELONGS_TO_MANY,
self::MORPH_TO, self::MORPH_ONE, self::MORPH_MANY
self::MORPH_TO, self::MORPH_ONE, self::MORPH_MANY,
self::MORPH_TO_MANY, self::MORPHED_BY_MANY
);

/**
Expand All @@ -208,7 +215,6 @@ abstract class Ardent extends Model {
* @return \LaravelBook\Ardent\Ardent
*/
public function __construct(array $attributes = array()) {

parent::__construct($attributes);
$this->validationErrors = new MessageBag;
}
Expand Down Expand Up @@ -328,12 +334,14 @@ protected function handleRelationalArray($relationName) {
return $this->$relationType($relation[1], $relation['foreignKey'], $relation['otherKey'], $relation['relation']);

case self::BELONGS_TO_MANY:
$verifyArgs(array('table', 'foreignKey', 'otherKey'));
$relationship = $this->$relationType($relation[1], $relation['table'], $relation['foreignKey'], $relation['otherKey']);
if(isset($relation['pivotKeys']) && is_array($relation['pivotKeys']))
$verifyArgs(array('table', 'foreignKey', 'otherKey', 'relation'));
$relationship = $this->$relationType($relation[1], $relation['table'], $relation['foreignKey'], $relation['otherKey'], $relation['relation']);
if(isset($relation['pivotKeys']) && is_array($relation['pivotKeys'])) {
$relationship->withPivot($relation['pivotKeys']);
if(isset($relation['timestamps']) && $relation['timestamps']==true)
}
if(isset($relation['timestamps']) && $relation['timestamps']) {
$relationship->withTimestamps();
}
return $relationship;

case self::MORPH_TO:
Expand All @@ -342,8 +350,16 @@ protected function handleRelationalArray($relationName) {

case self::MORPH_ONE:
case self::MORPH_MANY:
$verifyArgs(array('type', 'id'), array('name'));
return $this->$relationType($relation[1], $relation['name'], $relation['type'], $relation['id']);
$verifyArgs(array('type', 'id', 'localKey'), array('name'));
return $this->$relationType($relation[1], $relation['name'], $relation['type'], $relation['id'], $relation['localKey']);

case self::MORPH_TO_MANY:
$verifyArgs(array('table', 'foreignKey', 'otherKey', 'inverse'), array('name'));
return $this->$relationType($relation[1], $relation['name'], $relation['table'], $relation['foreignKey'], $relation['otherKey'], $relation['inverse']);

case self::MORPHED_BY_MANY:
$verifyArgs(array('table', 'foreignKey', 'otherKey'), array('name'));
return $this->$relationType($relation[1], $relation['name'], $relation['table'], $relation['foreignKey'], $relation['otherKey']);
}
}

Expand Down

0 comments on commit 20fc03a

Please sign in to comment.