Commit c9c0ddf5 authored by Taylor Otwell's avatar Taylor Otwell

Fixing bugs in Eq2.

Signed-off-by: 's avatarTaylor Otwell <taylorotwell@gmail.com>
parent e857505e
<?php namespace Laravel\Database\Eloquent; <?php namespace Laravel\Database\Eloquent;
use Laravel\Database; use Laravel\Database;
use Eloquent\Relationships\Has_Many_And_Belongs_To; use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To;
abstract class Model { abstract class Model {
...@@ -320,7 +320,7 @@ abstract class Model { ...@@ -320,7 +320,7 @@ abstract class Model {
*/ */
protected function timestamp() protected function timestamp()
{ {
$this->updated_at = $this->get_timestamp(); $this->updated_at = static::get_timestamp();
if ( ! $this->exists) $this->created_at = $this->updated_at; if ( ! $this->exists) $this->created_at = $this->updated_at;
} }
...@@ -330,7 +330,7 @@ abstract class Model { ...@@ -330,7 +330,7 @@ abstract class Model {
* *
* @return mixed * @return mixed
*/ */
protected function get_timestamp() protected static function get_timestamp()
{ {
return date('Y-m-d H:i:s'); return date('Y-m-d H:i:s');
} }
...@@ -406,7 +406,7 @@ abstract class Model { ...@@ -406,7 +406,7 @@ abstract class Model {
*/ */
public function get_attribute($key) public function get_attribute($key)
{ {
return $this->attributes[$key]; return array_get($this->attributes, $key);
} }
/** /**
......
...@@ -76,7 +76,7 @@ class Query { ...@@ -76,7 +76,7 @@ class Query {
{ {
// If the relationship is nested, we will skip laoding it here and let // If the relationship is nested, we will skip laoding it here and let
// the load method parse and set the nested eager loads on the right // the load method parse and set the nested eager loads on the right
// relationship when it is getting ready to eager laod it. // relationship when it is getting ready to eager laod.
if (str_contains($relationship, '.')) if (str_contains($relationship, '.'))
{ {
continue; continue;
...@@ -86,6 +86,14 @@ class Query { ...@@ -86,6 +86,14 @@ class Query {
} }
} }
// The many to many relationships may have pivot table column on them
// so we will call the "clean" method on the relationship to remove
// any pivot columns that are on the model.
if ($this instanceof Relationships\Has_Many_And_Belongs_To)
{
$this->clean($results);
}
return $results; return $results;
} }
......
...@@ -133,7 +133,7 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -133,7 +133,7 @@ class Has_Many_And_Belongs_To extends Relationship {
*/ */
protected function set_select($foreign) protected function set_select($foreign)
{ {
$foreign = $this->joining.'.'.$foreign.' as eloquent_foreign_key'; $foreign = $this->joining.'.'.$foreign.' as pivot_foreign_key';
$this->table->select(array($this->model->table().'.*', $foreign)); $this->table->select(array($this->model->table().'.*', $foreign));
...@@ -201,7 +201,7 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -201,7 +201,7 @@ class Has_Many_And_Belongs_To extends Relationship {
*/ */
public function match($relationship, &$parents, $children) public function match($relationship, &$parents, $children)
{ {
$foreign = 'eloquent_foreign_key'; $foreign = 'pivot_foreign_key';
foreach ($children as $key => $child) foreach ($children as $key => $child)
{ {
...@@ -216,6 +216,20 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -216,6 +216,20 @@ class Has_Many_And_Belongs_To extends Relationship {
} }
} }
/**
* Clean-up any pivot columns that are on the results.
*
* @param array $results
* @return void
*/
protected function clean(&$results)
{
foreach ($results as &$result)
{
}
}
/** /**
* Get the other or associated key for the relationship. * Get the other or associated key for the relationship.
* *
......
<?php namespace Laravel\Database\Eloquent\Relationships; use Eloquent\Model; <?php namespace Laravel\Database\Eloquent\Relationships;
use Laravel\Database\Eloquent\Model;
class Has_One_Or_Many extends Relationship { class Has_One_Or_Many extends Relationship {
......
<?php namespace Laravel\Database\Eloquent\Relationships; use Eloquent\Model, Eloquent\Query; <?php namespace Laravel\Database\Eloquent\Relationships;
use Laravel\Database\Eloquent\Model;
use Laravel\Database\Eloquent\Query;
abstract class Relationship extends Query { abstract class Relationship extends Query {
......
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