Skip to content
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.0] Fix empty morph to relationship #8586

Merged
merged 1 commit into from
Apr 29, 2015
Merged

[5.0] Fix empty morph to relationship #8586

merged 1 commit into from
Apr 29, 2015

Conversation

lukasgeiter
Copy link
Contributor

This fixes issue #8499

*/
public function getResults()
{
if ( ! $this->otherKey) return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return; please

@GrahamCampbell GrahamCampbell changed the title Fix empty morph to relationship [5.0] Fix empty morph to relationship Apr 28, 2015
taylorotwell added a commit that referenced this pull request Apr 29, 2015
[5.0] Fix empty morph to relationship
@taylorotwell taylorotwell merged commit 69793ac into laravel:5.0 Apr 29, 2015
@lukasgeiter lukasgeiter deleted the fix-empty-morph branch April 29, 2015 05:29
@lukasgeiter lukasgeiter mentioned this pull request May 31, 2015
@mtvs
Copy link
Contributor

mtvs commented Jun 8, 2015

It doesn't work for me (5.0)

FatalErrorException in Model.php line 873: Class '' not found

1. in Model.php line 873
2. at FatalErrorException->__construct('message' => '', 'code' => '', 'severity' => '', 'filename' => '', 'lineno' => '', 'traceOffset' => '', 'traceArgs' => '') in HandleExceptions.php line 131
3. at HandleExceptions->fatalExceptionFromError('error' => '', 'traceOffset' => '') in HandleExceptions.php line 116
4. at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
5. at Model->morphTo('name' => '', 'type' => '', 'id' => '') in Code.php line 38

even when I set the morph fields to null, still it causes a QueryException but when I provide the right values for the fields the relation works as expected.

@lukasgeiter
Copy link
Contributor Author

I'd need to see some more of your code to help you... Also that part Class '' not found looks a lot like your type field is not null but rather an empty string. Please check that first.

@mtvs
Copy link
Contributor

mtvs commented Jun 9, 2015

Thanks for your willingness to help.

I think it should work with empty strings too, because the Blueprint::morphs() doesn't create nullable columns.

Anyway as I said I tried it with null morph columns too, but this doesn't work for me.

I have the following methods in my Code model:

// App\Code.php
/**
 * @return \Illuminate\Database\Eloquent\Relations\MorphTo
 */
public function input()
{
    return $this->morphTo('input');
}

/**
 * @return bool
 */
public function hasBeenUsed()
{
    return $this->input()->count() ? true : false;
}

when the hasBeenUsed() is called, it should count the number of the related inputs but the error log shows that the query is executed against the codes table.

QueryException in Connection.php line 620: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'codes.' in 'where clause' (SQL: select count as aggregate from `codes` where `codes`.`` is null)

in Connection.php line 620
at Connection->runQueryCallback('select count as aggregate from `codes` where `codes`.`` is null', array(), object(Closure)) in Connection.php line 576
at Connection->run('select count as aggregate from `codes` where `codes`.`` is null', array(), object(Closure)) in Connection.php line 293
at Connection->select('select count as aggregate from `codes` where `codes`.`` is null', array(), true) in Builder.php line 1377
at Builder->runSelect() in Builder.php line 1367
at Builder->getFresh(array('*')) in Builder.php line 1354
at Builder->get(array('*')) in Builder.php line 1636
at Builder->aggregate('count', array('*')) in Builder.php line 1574
at Builder->count()
at call_user_func_array(array(object(Builder), 'count'), array()) in Builder.php line 933
at Builder->__call('count', array())
at Builder->count()
at call_user_func_array(array(object(Builder), 'count'), array()) in Relation.php line 283
at Relation->__call('count', array()) in Code.php line 46
at MorphTo->count() in Code.php line 46
at Code->hasBeenUsed() in InputHandler.php line 36
at InputHandler->handle(object(WebInput)) in ParticipantsController.php line 29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants