Skip to content

Commit

Permalink
Load relations if it's saving json schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
dmongeau committed Jan 9, 2018
1 parent 46cc912 commit 5e956ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Folklore/EloquentJsonSchema/Support/HasJsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ trait HasJsonSchema

protected $disabledJsonSchemasAttributes = [];

protected $savingJsonSchemas = false;

public static function bootHasJsonSchema()
{
static::observe(JsonSchemaObserver::class);
Expand Down Expand Up @@ -67,11 +69,13 @@ public function validateJsonSchemaAttributes()
*/
public function saveJsonSchemaAttributes()
{
$this->setSavingJsonSchemas(true);
$attributes = $this->getJsonSchemaAttributes();
foreach ($attributes as $key) {
$value = $this->getAttributeValue($key);
$this->callJsonSchemaReducers($key, 'save', $value);
}
$this->setSavingJsonSchemas(false);
}

/**
Expand Down Expand Up @@ -483,4 +487,25 @@ public function isJsonSchemaAttributeDisabled($attribute)
$disabled = $this->getDisabledJsonSchemasAttributes();
return in_array($attribute, $disabled);
}

/**
* Set if it's saving json schemas
*
* @param boolean $saving
* @return void
*/
protected function setSavingJsonSchemas($saving)
{
$this->savingJsonSchemas = $saving;
}

/**
* Check if it's saving json schemas
*
* @return boolean
*/
public function isSavingJsonSchemas()
{
return $this->savingJsonSchemas;
}
}
7 changes: 6 additions & 1 deletion src/Folklore/EloquentJsonSchema/Support/RelationReducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ protected function shouldQueryRelation($model, $node, $state, $value)
{
$relationName = $this->getRelationName($model, $node, $state);
$relationClass = $model->{$relationName}();
return is_null($value) && ($relationClass instanceof HasOneOrMany || !$model->exists || sizeof($model->getDirty()));
return is_null($value) && (
$model->isSavingJsonSchemas() ||
$relationClass instanceof HasOneOrMany ||
!$model->exists ||
sizeof($model->getDirty())
);
}

protected function shouldUseReducer($model, $node, $state)
Expand Down

0 comments on commit 5e956ca

Please sign in to comment.