【发布时间】:2015-03-04 09:05:42
【问题描述】:
试图让一些多态关系在 laravel 4 中工作。
我有一个如下所示的 CoreUserAccount 表:
core_user_account:
身份证
profile_id
其他栏目
然后我还有另外 2 个表 core_user_client_profile 和 core_user_consultant_profile。它们都有 id 字段。
我正在尝试像这样链接它们:
在 CoreUserAccount 模型中:
public function profile()
{
return $this->morphTo();
}
在我拥有的其他两个表中:
public function user()
{
return $this->morphOne('CoreUserAccount', 'profile');
}
但我似乎无法通过用户对象获取任何配置文件数据。有什么想法吗?
干杯
编辑
这是我的 core_user_account 表:
这是我的 2 个不同的配置文件类型表 core_user_client_profile 和 core_user_consultant_profile:
这是我的 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';
}
}
还有我的 core_user_client_profile 模型:
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);
}
}
还有我的 core_user_consultant_profile 模型:
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');
}
}
我已阅读下面 cmets 中指出的文档,并了解我的数据库中似乎必须有一个类型列。但是这里需要存储哪些数据呢?它会自动存储还是我必须存储它?
干杯
【问题讨论】:
-
core_user_account 表中是否有 profile_id 和 profile_type 列?你能粘贴你所有的模型和表结构吗?
-
嗨,帕维尔。明天我回来工作时,我将能够发布所有内容。我没有个人资料类型列吗?它有什么用,我为什么需要它?干杯
-
查看此文档laravel.com/docs/4.2/eloquent#polymorphic-relations 这是允许 ORM 在访问可成像关系时确定返回哪种类型的拥有模型的原因。
-
我已经更新了我的问题 Pawel。干杯
标签: laravel laravel-4 polymorphism relationship