-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] Potential issue with withDefault() method on HasOne relationship #16614
Comments
@sileence what do you think? |
@themsaid I like the option 3. Right now if you return |
BTW I'm glad you find this tiny feature useful @patrickcarlohickman |
@sileence Is there any chance this feature will be implemented for BelongsTo? |
@decadence: @taylorotwell @themsaid do you think adding withDefault to belongsTo could be useful? If so I can create the PR. |
@sileence I think it will. But there is one problem: you can't set foreign key for current model because model from withDefault will not exist by that time. $instance = $this->related->newInstance()->setAttribute(
$this->getForeignKeyName(), $model->getAttribute($this->localKey)
); But we call save and associate anyway later. |
Any updates on this? |
Well, withDefault has now being implemented for belongsTo and morphOne relations. I think it's responsibility of the developer to return a model (or null) when using this feature with a Closure. I think it's out of the scope of Laravel to add checks for this. cc @themsaid |
Closing as " think it's out of the scope of Laravel to add checks for this" |
Description:
The new
withDefault()
method on theHasOne
relationship is very useful. However, the Closure functionality of the method may need to be slightly restricted.There is existing code that depends on the result of any Eloquent relationship to either be
null
, aModel
instance, or aCollection
ofModel
instances. However, the current functionality of thewithDefault()
method opens up the potential for returning an object that is not one of those three expected values.Now, not only do some existing applications and packages depend on this set of return types, but some of the core framework code does, as well. For example, if you supply a Closure that just returns a new
stdClass
instance, and then callpush()
to save all the relationships, the parent model will save fine, but when it attempts to save the relationship, it will generate the errorCall to undefined method stdClass::push()
.Steps To Reproduce:
In
tests/Database/DatabaseEloquentIntegrationTest.php
, add two new relationships to theEloquentTestUser
class:Add the following two tests:
Suggested Resolution
Three potential options:
Remove the Closure functionality altogether. I would imagine this is not desirable, as the Closure functionality does add some nice flexibility.
Throw an exception if the Closure returns a value that is not
null
or aModel
instance.Ignore the return value of the Closure completely. The Closure already receives a
Model
instance as an argument. This instance should be able to be modified inside the Closure without having to return it from the Closure.For example, the below relationship will work just fine (problems arise with non-falsey, non-Model return values).
The text was updated successfully, but these errors were encountered: