Skip to content

Commit 5f55a69

Browse files
committed
Bug fixes
Allow Persist Resource to use updatesOnlyDirty for relations HasOne to use newRelation function and return a clean instance
1 parent 42d828e commit 5f55a69

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/Support/PersistResource.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function fill(object $object, $type="insert")
3131
return $this;
3232
}
3333

34-
protected function fillForInsert(object $object)
34+
protected function fillForInsert(object $object)
3535
{
3636
foreach ($object->getAttributes() as $key => $value) {
3737
if(Arr::exists($this->getPersistAttributes(), $key)){
@@ -56,7 +56,7 @@ protected function fillForInsert(object $object)
5656
}
5757
}
5858

59-
protected function fillForUpdate(object $object)
59+
protected function fillForUpdate(object $object)
6060
{
6161
if($object->updatesOnlyDirty()){
6262
$data = $object->getDirty();
@@ -86,12 +86,12 @@ protected function fillForUpdate(object $object)
8686
}
8787
}
8888

89-
public function processMutate($key, $value)
89+
public function processMutate($key, $value)
9090
{
9191
data_set($this->attributes, $this->getMutateKey($key), $value);
9292
}
9393

94-
public function processManyFill($key, $value)
94+
public function processManyFill($key, $value)
9595
{
9696
$temp = [];
9797
foreach($value as $object){
@@ -105,7 +105,7 @@ public function processRelation($key, $object, $type = 'insert')
105105
if(is_array($this->getRelatedClass($key))){
106106
$attributes = $this->getRelatedClass($key);
107107
$temp = [];
108-
$array = $type == 'insert' ? $object->getAttributes() : $object->getDirty();
108+
$array = $type == 'insert' || !$object->updatesOnlyDirty() ? $object->getAttributes() : $object->getDirty();
109109
foreach($array as $subKey => $subValue){
110110
if(Arr::exists($attributes, $subKey)){
111111
$temp[$subKey] = $subValue;
@@ -120,7 +120,7 @@ public function processRelation($key, $object, $type = 'insert')
120120
}
121121
}
122122

123-
public function recursiveFill($key, $value)
123+
public function recursiveFill($key, $value)
124124
{
125125
$array = [];
126126
foreach($value as $childKey => $childValue){
@@ -152,7 +152,7 @@ public function getValidationAttributes()
152152
$relation = $this->object->{lcfirst($key)}()->type;
153153
} else {
154154
$relation = 'HasOne';
155-
}
155+
}
156156
if($relation == 'HasOne'){
157157
$attributes[$key] = 'array';
158158
$attributes[$key.'.'.$field] = $rules;
@@ -166,26 +166,26 @@ public function getValidationAttributes()
166166
return $attributes;
167167
}
168168

169-
public function processEmpties()
169+
public function processEmpties()
170170
{
171171
foreach($this->getAttributes() as $key => $value){
172172
if(is_array($value)){
173173
$this->attributes[$key] = $this->processRecursiveEmpty($value);
174174
if(empty($this->attributes[$key])) {
175175
unset($this->attributes[$key]);
176-
}
176+
}
177177
}
178178
}
179179
}
180180

181-
public function processRecursiveEmpty(array $array)
181+
public function processRecursiveEmpty(array $array)
182182
{
183183
foreach($array as $key => $value) {
184184
if(is_array($value)){
185185
$value = $this->processRecursiveEmpty($value);
186186
if(empty($value)) {
187187
unset($array[$key]);
188-
}
188+
}
189189
}
190190
}
191191
return $array;
@@ -216,22 +216,22 @@ public function getRelatedClass($key)
216216
return $this->relatedResource[$key];
217217
}
218218

219-
public function setRelation($name, $relation)
219+
public function setRelation($name, $relation)
220220
{
221221
$this->relations[$name] = $relation;
222222
}
223223

224-
public function hasRelation($name)
224+
public function hasRelation($name)
225225
{
226226
return isset($this->relations[$name]);
227227
}
228228

229-
public function getRelation($name)
229+
public function getRelation($name)
230230
{
231231
return $this->relations[$name];
232232
}
233233

234-
public function getAttributes()
234+
public function getAttributes()
235235
{
236236
return $this->attributes;
237237
}
@@ -241,8 +241,8 @@ public function validate()
241241
return Validator::make($this->getAttributes(), $this->getValidationAttributes());
242242
}
243243

244-
public function updateRelatedResource()
244+
public function updateRelatedResource()
245245
{
246-
246+
247247
}
248-
}
248+
}

src/Support/Relations/HasOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function getResults()
8989

9090
public function getRelationFromApi()
9191
{
92-
$this->relation = $this->related->newInstance([$this->field => $this->owner->getKey()])->getOne();
92+
$this->relation = $this->newRelation([$this->field => $this->owner->getKey()]);
9393
if($this->field != null && $this->owner->hasKey() && $this->relation != null){
9494
$this->relation->{$this->field} = $this->owner->getKey();
9595
}

0 commit comments

Comments
 (0)