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?_
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?)
Most helpful comment
Looks like this was solved by requiring this method to be added:
For Google's sake.