简体   繁体   中英

Laravel Polymorphic Relation not working

trying to get some polymorphic relations working in laravel 4.

I have a CoreUserAccount table that looks like so:

core_user_account:

id

profile_id

other columns

I then have 2 other tables core_user_client_profile and core_user_consultant_profile. They both have id fields.

i am trying to link them like so:

In the CoreUserAccount model:

public function profile()
{
    return $this->morphTo();
}

And in both the other tables i have:

public function user()
{
    return $this->morphOne('CoreUserAccount', 'profile');
}

But i don't seem to be able to get any of the profile data through the user object. Any ideas?

Cheers

EDIT

Here is my core_user_account table:

core_user_account

And here are my 2 different profile type tables core_user_client_profile and core_user_consultant_profile:

顾问

客户

Here is my model for core_user_account:

    <?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class CoreUserAccount
    extends Eloquent
    implements UserInterface, RemindableInterface
{
    protected $table = 'core_user_account';
    protected $hidden = array('password');
    protected $guarded = array('id', 'password');

    public function profileType()
    {
        return $this->belongsTo('CoreUserProfileType');
    }

    public function address()
    {
        return $this->belongsTo('CoreUserAddress', 'user_account_id', 'id');
    }

    public function profile()
    {
        return $this->morphTo();
    }

    public function getAuthIdentifier() {
        return $this->getKey();
    }

    public function getAuthPassword() {
        return $this->password;
    }

    public function getReminderEmail() {
        return $this->email;
    }

    public function getRememberToken() {
        return $this->remember_token;
    }

    public function setRememberToken($value) {
        $this->remember_token = $value;
    }

    public function getRememberTokenName() {
        return 'remember_token';
    }
}

And my core_user_client_profile model:

class CoreUserClientProfile
    extends Eloquent
{
    protected $table = 'core_user_client_profile';

    public function profileLanguage()
    {
        return $this->belongsTo('CoreUserProfileLanguages');
    }

    public function user()
    {
        return $this->morphOne('CoreUserAccount', 'profile',null,'profile_id',null);
    }
}

And my core_user_consultant_profile model:

class CoreUserConsultantProfile
    extends Eloquent
{
    protected $table = 'core_user_consultant_profile';

    public function profileLanguage()
    {
        return $this->belongsTo('CoreUserProfileLanguages', 'consultant_profile_id', 'id');
    }

    public function qualifications()
    {
        return $this->belongsTo('CoreUserConsultantQualifications', 'profile_id', 'id');
    }

    public function registration()
    {
        return $this->belongsTo('CoreUserConsultantRegistrations', 'profile_id', 'id');
    }

    public function specialities()
    {
        return $this->belongsTo('CoreUserConsultantProfileSpeciality', 'profile_id', 'id');
    }

    public function user()
    {
        return $this->morphOne('CoreUserAccount', 'profile');
    }
}

I have read the document pointed out in the comments below and understand i seem to have to have a type column in my DB. But what data needs to be stored here? does it automatically get stored or do i have to store it?

Cheers

You should have to have profile_type, and profile_id columns. In profile_type will be stored values: 'CoreUserConsultantProfile' or 'CoreUserClientProfile'. This fields will be populated automaticlly when you save data.

This is example how save data in polymorph relation.

$profile = CoreUserConsultantProfile::find(1);
$account = new CoreUserAccount();

$profile->profile()->save($account);

So now, profile_id=1, and profile_type = 'CoreUserConsultantProfile'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM