Framework: [Request] Allow custom morphClass to be set on morphOne or morphMany

Created on 8 Feb 2014  路  6Comments  路  Source: laravel/framework

Currently the "morphClass" is being fetched directly from the class name of the calling parent model in Illuminate\Database\Eloquent\Relations\MorphOneOrMany.php

Problem:
If you extend an extended Eloquent Model this results in issues that the polymorphic relation cannot be resolved anymore. Example:

class User extends Eloqent { ...
class UserExtension extends User { ...

If you have set-up a polymorphic relation like "address" for example, and you save it from the extended UserExtension class it will save the polymorphic relation type as "UserExtension".

This results in the fact that you cannot access

$user->address 

anymore, but only

$userextension->address 

(given that these variables are instances of their respective class).

Solution:
Extending the Illuminate relations results in many files overwriting the original framework files, so I suggest to add another parameter to the morphOne and morphMany methods which allows to manually set the polymorphic "type" instead of resolving it via $this of the calling parent class.

morphOne($related, $name, $type = null, $id = null, $localKey = null, $morphClass = null)

So the polymorphic relation would be defined like so in the User class:

class User extends Eloquent {
    public function address()
    {
        return $this->morphOne('Address', 'owner', null, null, null, 'User');
    }
}  

And the polymorphic relation could be resolved no matter if called directly via $user->address or the extended $userextension->address

_Would that be feasible?_

Most helpful comment

Looks like this was solved by requiring this method to be added:

public function getMorphClass()
{
    return 'ClassNameInMorphTypeField';
}

For Google's sake.

All 6 comments

This would be helpful to me as well, since I have both namespaced models and existing data in my tables. For example: would be helpful if I could have 'user' as my type instead of 'Models\User'

In addition, namespaced models used with mysql database are never found because there are stored with double backslashes : "path\to\model".
(see http://stackoverflow.com/questions/22254285/laravel-polymorphic-many-to-many-returns-empty)

This is now available.

Looks like this was solved by requiring this method to be added:

public function getMorphClass()
{
    return 'ClassNameInMorphTypeField';
}

For Google's sake.

@dankuck thanks! Very nice

Looks like this was solved by requiring this method to be added:

public function getMorphClass()
{
    return 'ClassNameInMorphTypeField';
}

For Google's sake.

so, why adding to model
protected $morphClass = 'asdasd';
do not work?)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iivanov2 picture iivanov2  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

felixsanz picture felixsanz  路  3Comments