Skip to content

Commit

Permalink
infer first_key and second_key for has_many through relationships. #145
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed May 6, 2015
1 parent e1cb42d commit 9051430
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
21 changes: 20 additions & 1 deletion src/Database/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
<?php namespace Hook\Database\Relations;

use Illuminate\Database\Eloquent\Relations\HasManyThrough as EloquentHasManyThrough;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

class HasManyThrough extends EloquentHasManyThrough
{

protected function getQualifiedParentKeyName()
{
return $this->parent->getTable().'.'.$this->secondKey;
// return $this->parent->getQualifiedKeyName();
return $this->parent->getTable().'.'.$this->secondKey;
}

protected function setJoin(EloquentBuilder $query = null)
{
$query = $query ?: $this->query;

// $foreignKey = $this->related->getTable().'.'.$this->secondKey;
$foreignKey = $this->related->getQualifiedKeyName();

$query->join($this->parent->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey);
}

// public function get($columns = array('*'))
// {
// $r = parent::get($columns);
// file_put_contents('php://stdout', $r->toJson() . "\n");
// file_put_contents('php://stdout', get_class($r[0]) . "\n");
// return $r;
// }

}
16 changes: 3 additions & 13 deletions src/Database/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,17 @@ public static function getRelationInstance($model, $relation_type, $field, $conf

switch ($relation_type) {
case "belongs_to":
// $model->belongsTo($related_klass, $foreign_key, '_id', $field);
return new BelongsTo($related_query, $model, $foreign_key, $primary_key, $field);

case "belongs_to_many":
return new BelongsToMany($related_query, $model, $related_table, $foreign_key, $primary_key, $field);

case "has_many":
// hasMany($related, $foreignKey = null, $localKey = null)
// $model->hasMany($related_klass, $model_table . '_id', '_id');

if (isset($config['through'])) {
$through = App::collection($config['through'])->getModel();

file_put_contents('php://stdout', $through->getTable() . "\n");
file_put_contents('php://stdout', $through->getQualifiedKeyName() . "\n");
file_put_contents('php://stdout', $model->getTable() . "\n");
file_put_contents('php://stdout', $model->getQualifiedKeyName() . "\n");

// __construct(Builder $query, Model $farParent, Model $parent, $firstKey, $secondKey)
// return new HasManyThrough(with(new $related)->newQuery(), $this, $through, $firstKey, $secondKey);
return new HasManyThrough($related_query, $model, $through, 'book_id', 'author_id');
$first_key = $foreign_key;
$second_key = (isset($config['far_key'])) ? $config['far_key'] : str_singular($config['collection']) . '_id';
return new HasManyThrough($related_query, $model, $through, $first_key, $second_key);

} else {
return new HasMany($related_query, $model, $foreign_key, $primary_key);
Expand Down

0 comments on commit 9051430

Please sign in to comment.