Skip to content

Commit

Permalink
Fix relation caching
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyBJacobs committed Feb 22, 2020
1 parent 9851187 commit 7ab90cd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ protected function save_has_foreign_relations() {
protected function save_loaded_relations( array $exclude = array() ) {
foreach ( $this->_relations as $relation => $values ) {

if ( in_array( $relation, $exclude ) ) {
if ( in_array( $relation, $exclude ) || $values === null ) {
continue;
}

Expand Down
7 changes: 5 additions & 2 deletions src/Relations/HasForeign.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ public function eager_load( array $models, $callback = null ) {
$related_models = $this->fetch_results_for_eager_load( $pks );

foreach ( $related_models as $related_model ) {
if ( ! empty( $map[ $this->saver->get_pk( $related_model ) ] ) ) {
foreach ( $map[ $this->saver->get_pk( $related_model ) ] as $mapped ) {
$pk = $this->saver->get_pk( $related_model );

if ( ! empty( $map[ $pk ] ) ) {
foreach ( $map[ $pk ] as $mapped ) {
$mapped->set_raw_attribute( $this->attribute, $related_model );
$mapped->sync_original_attribute( $this->attribute );
$this->cache_results( $this->saver->get_model( $pk ), $mapped );
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,13 @@ public function eager_load( array $models, $callback = null ) {
if ( isset( $map[ $model->get_pk() ] ) ) {
$related = $map[ $model->get_pk() ];

$model->set_relation_value( $this->attribute, $this->wrap_eager_loaded_results( $related ) );
$result = $this->wrap_eager_loaded_results( $related );
} else {
$model->set_relation_value( $this->attribute, $this->wrap_eager_loaded_results( null ) );
$result = $this->wrap_eager_loaded_results( null );
}

$model->set_relation_value( $this->attribute, $result );
$this->cache_results( $result, $model );
}

return $results;
Expand All @@ -215,4 +218,4 @@ public function eager_load( array $models, $callback = null ) {
* @return mixed
*/
protected abstract function wrap_eager_loaded_results( $results );
}
}
6 changes: 4 additions & 2 deletions src/Relations/ManyToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ public function eager_load( array $models, $callback = null ) {

foreach ( $models as $model ) {
$data = isset( $relationship_map[ $model->get_pk() ] ) ? $relationship_map[ $model->get_pk() ] : array();
$model->set_relation_value( $attribute, new Collection( $data, $memory, $this->saver ) );
$model->set_relation_value( $attribute, $value = new Collection( $data, $memory, $this->saver ) );
$this->cache_results( $value, $model );
}

return new Collection( $related, true, $this->saver );
Expand All @@ -366,6 +367,7 @@ public function persist( $values ) {
$added = $this->persist_do_save( $values );

$this->persist_added( new ArrayCollection( $values->get_added()->toArray() + $added ) );
$values->clear_memory();

return $values;
}
Expand Down Expand Up @@ -496,4 +498,4 @@ public function on_delete( Model $model ) {
$this->other_column => $model->get_pk()
) );
}
}
}
2 changes: 1 addition & 1 deletion src/Relations/ManyToManyUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ public function eager_load( array $models, $callback = null ) {

return $loaded;
}
}
}

0 comments on commit 7ab90cd

Please sign in to comment.