Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
Allow Persist Resource to use updatesOnlyDirty for relations

HasOne to use newRelation function and return a clean instance
  • Loading branch information
colinhall17 committed Jun 22, 2020
1 parent 42d828e commit 5f55a69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions src/Support/PersistResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function fill(object $object, $type="insert")
return $this;
}

protected function fillForInsert(object $object)
protected function fillForInsert(object $object)
{
foreach ($object->getAttributes() as $key => $value) {
if(Arr::exists($this->getPersistAttributes(), $key)){
Expand All @@ -56,7 +56,7 @@ protected function fillForInsert(object $object)
}
}

protected function fillForUpdate(object $object)
protected function fillForUpdate(object $object)
{
if($object->updatesOnlyDirty()){
$data = $object->getDirty();
Expand Down Expand Up @@ -86,12 +86,12 @@ protected function fillForUpdate(object $object)
}
}

public function processMutate($key, $value)
public function processMutate($key, $value)
{
data_set($this->attributes, $this->getMutateKey($key), $value);
}

public function processManyFill($key, $value)
public function processManyFill($key, $value)
{
$temp = [];
foreach($value as $object){
Expand All @@ -105,7 +105,7 @@ public function processRelation($key, $object, $type = 'insert')
if(is_array($this->getRelatedClass($key))){
$attributes = $this->getRelatedClass($key);
$temp = [];
$array = $type == 'insert' ? $object->getAttributes() : $object->getDirty();
$array = $type == 'insert' || !$object->updatesOnlyDirty() ? $object->getAttributes() : $object->getDirty();
foreach($array as $subKey => $subValue){
if(Arr::exists($attributes, $subKey)){
$temp[$subKey] = $subValue;
Expand All @@ -120,7 +120,7 @@ public function processRelation($key, $object, $type = 'insert')
}
}

public function recursiveFill($key, $value)
public function recursiveFill($key, $value)
{
$array = [];
foreach($value as $childKey => $childValue){
Expand Down Expand Up @@ -152,7 +152,7 @@ public function getValidationAttributes()
$relation = $this->object->{lcfirst($key)}()->type;
} else {
$relation = 'HasOne';
}
}
if($relation == 'HasOne'){
$attributes[$key] = 'array';
$attributes[$key.'.'.$field] = $rules;
Expand All @@ -166,26 +166,26 @@ public function getValidationAttributes()
return $attributes;
}

public function processEmpties()
public function processEmpties()
{
foreach($this->getAttributes() as $key => $value){
if(is_array($value)){
$this->attributes[$key] = $this->processRecursiveEmpty($value);
if(empty($this->attributes[$key])) {
unset($this->attributes[$key]);
}
}
}
}
}

public function processRecursiveEmpty(array $array)
public function processRecursiveEmpty(array $array)
{
foreach($array as $key => $value) {
if(is_array($value)){
$value = $this->processRecursiveEmpty($value);
if(empty($value)) {
unset($array[$key]);
}
}
}
}
return $array;
Expand Down Expand Up @@ -216,22 +216,22 @@ public function getRelatedClass($key)
return $this->relatedResource[$key];
}

public function setRelation($name, $relation)
public function setRelation($name, $relation)
{
$this->relations[$name] = $relation;
}

public function hasRelation($name)
public function hasRelation($name)
{
return isset($this->relations[$name]);
}

public function getRelation($name)
public function getRelation($name)
{
return $this->relations[$name];
}

public function getAttributes()
public function getAttributes()
{
return $this->attributes;
}
Expand All @@ -241,8 +241,8 @@ public function validate()
return Validator::make($this->getAttributes(), $this->getValidationAttributes());
}

public function updateRelatedResource()
public function updateRelatedResource()
{

}
}
}
2 changes: 1 addition & 1 deletion src/Support/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getResults()

public function getRelationFromApi()
{
$this->relation = $this->related->newInstance([$this->field => $this->owner->getKey()])->getOne();
$this->relation = $this->newRelation([$this->field => $this->owner->getKey()]);
if($this->field != null && $this->owner->hasKey() && $this->relation != null){
$this->relation->{$this->field} = $this->owner->getKey();
}
Expand Down

0 comments on commit 5f55a69

Please sign in to comment.