|
@@ -44,7 +44,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
$this->joining = $table ?: $this->joining($model, $associated);
|
|
$this->joining = $table ?: $this->joining($model, $associated);
|
|
|
|
|
|
// If the Pivot table is timestamped, we'll set the timestamp columns to be
|
|
// If the Pivot table is timestamped, we'll set the timestamp columns to be
|
|
- // fetched when the pivot table models are fetched by the developer, or else
|
|
|
|
|
|
+ // fetched when the pivot table models are fetched by the developer else
|
|
// the ID will be the only "extra" column fetched in by default.
|
|
// the ID will be the only "extra" column fetched in by default.
|
|
if (Pivot::$timestamps)
|
|
if (Pivot::$timestamps)
|
|
{
|
|
{
|
|
@@ -61,8 +61,6 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
*
|
|
*
|
|
* By default, the name is the models sorted and joined with underscores.
|
|
* By default, the name is the models sorted and joined with underscores.
|
|
*
|
|
*
|
|
- * @param Model $model
|
|
|
|
- * @param string $associated
|
|
|
|
* @return string
|
|
* @return string
|
|
*/
|
|
*/
|
|
protected function joining($model, $associated)
|
|
protected function joining($model, $associated)
|
|
@@ -88,7 +86,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
* Insert a new record into the joining table of the association.
|
|
* Insert a new record into the joining table of the association.
|
|
*
|
|
*
|
|
* @param int $id
|
|
* @param int $id
|
|
- * @param array $attributes
|
|
|
|
|
|
+ * @param array $joining
|
|
* @return bool
|
|
* @return bool
|
|
*/
|
|
*/
|
|
public function attach($id, $attributes = array())
|
|
public function attach($id, $attributes = array())
|
|
@@ -133,7 +131,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
}
|
|
}
|
|
|
|
|
|
// Next we will take the difference of the current and given IDs and detach
|
|
// Next we will take the difference of the current and given IDs and detach
|
|
- // all of the entities that exist in the current array but are not in
|
|
|
|
|
|
+ // all of the entities that exists in the current array but are not in
|
|
// the array of IDs given to the method, finishing the sync.
|
|
// the array of IDs given to the method, finishing the sync.
|
|
$detach = array_diff($current, $ids);
|
|
$detach = array_diff($current, $ids);
|
|
|
|
|
|
@@ -319,30 +317,22 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
/**
|
|
/**
|
|
* Match eagerly loaded child models to their parent models.
|
|
* Match eagerly loaded child models to their parent models.
|
|
*
|
|
*
|
|
- * @param string $relationship
|
|
|
|
- * @param array $parents
|
|
|
|
- * @param array $children
|
|
|
|
|
|
+ * @param array $parents
|
|
|
|
+ * @param array $children
|
|
* @return void
|
|
* @return void
|
|
*/
|
|
*/
|
|
public function match($relationship, &$parents, $children)
|
|
public function match($relationship, &$parents, $children)
|
|
{
|
|
{
|
|
$foreign = $this->foreign_key();
|
|
$foreign = $this->foreign_key();
|
|
|
|
|
|
- $dictionary = array();
|
|
|
|
-
|
|
|
|
- foreach ($children as $child)
|
|
|
|
- {
|
|
|
|
- $dictionary[$child->pivot->$foreign][] = $child;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
foreach ($parents as &$parent)
|
|
foreach ($parents as &$parent)
|
|
{
|
|
{
|
|
- $parent_key = $parent->get_key();
|
|
|
|
-
|
|
|
|
- if (isset($dictionary[$parent_key]))
|
|
|
|
|
|
+ $matching = array_filter($children, function($v) use (&$parent, $foreign)
|
|
{
|
|
{
|
|
- $parent->relationships[$relationship] = $dictionary[$parent_key];
|
|
|
|
- }
|
|
|
|
|
|
+ return $v->pivot->$foreign == $parent->get_key();
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ $parent->relationships[$relationship] = array_values($matching);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -386,7 +376,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
/**
|
|
/**
|
|
* Set the columns on the joining table that should be fetched.
|
|
* Set the columns on the joining table that should be fetched.
|
|
*
|
|
*
|
|
- * @param array $columns
|
|
|
|
|
|
+ * @param array $column
|
|
* @return Relationship
|
|
* @return Relationship
|
|
*/
|
|
*/
|
|
public function with($columns)
|
|
public function with($columns)
|