雄辩的枢轴模型关系

时间:2014-02-04 19:26:58

标签: php laravel-4 pivot eloquent

我对如何使用额外字段在Laravel枢轴模型中定义关系感到有些困惑。以下是表格迁移...

Schema::create('users', function($table) {
    $table->increments('id');
    $table->string('password', 60);
    $table->string('first_name', 50);
    $table->string('last_name', 50);
    $table->string('email', 255)->unique();
    $table->timestamps();
});

Schema::create('accounts', function($table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned()->unique();
    $table->boolean('active')->default(1);
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict')->onUpdate('cascade');
});

Schema::create('roles', function($table) {
    $table->increments('id');
    $table->string('name');
});

Schema::create('role_user', function($table) {
    $table->integer('role_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});

Schema::create('account_role', function($table) {
    $table->integer('account_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->integer('role_id')->unsigned();
    $table->timestamps();
    $table->unique(array('account_id','user_id'));
    $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade')->onUpdate('cascade');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
});

现在你可能想知道“到底是什么,为什么有一个role_user表和一个account_role表?”我会解释一下。

我有用户(免费)和帐户(付费)。每个帐户都归用户所有。我还在RolesUsers表中拥有像“admin”这样的站点范围内的角色。我们的业务逻辑要求帐户持有者可以将角色分配给其他用户以使用该帐户拥有的资产。换句话说,如果我有一个付费帐户,并且我希望我的朋友Joe能够帮助管理我的帐户,我作为帐户所有者可以通过为我的帐户分配一个角色来授予他访问权限。这就是AccountRoles模型的用途。因此,account_role表有三个字段,user_id,role_id,account_id。

我被困的地方是我似乎无法从枢轴访问帐户模型。我可以访问帐户ID。以下是相关模型:

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'users';

    protected $hidden = array('password');
    protected $guarded = array('id');

    public function Account() { return $this->hasOne('Account', 'user_id'); }
    public function AccountRoles() { return $this->belongsToMany('Role', 'account_role')->withPivot('account_id')->withTimestamps(); }
    public function Roles() { return $this->belongsToMany('Role', 'role_user')->withTimestamps(); }
}

class Account extends Eloquent {

    protected $table = 'accounts';

    public function User() { return $this->belongsTo('User', 'user_id'); }
    public function AccountRoles() { return $this->belongsToMany('Role', 'account_role')->withPivot('user_id')->withTimestamps(); }
}

class AccountRole extends Eloquent {

    protected $table = 'account_role';
    protected $fillable = array();

    public function Account() { return $this->belongsTo('Account', 'account_id'); }
}

这是有效的:

$user = User::find($id);

foreach($user->AccountRoles as $account_role)
{
    echo 'Account ID: ' . $account_role->pivot->account_id . '<br/>';
    echo 'Role: ' . $account_role->name . '<br/>';
}

当我......

var_dump($account_role->pivot->Account);

......我得到“null”。

我也试过......

$account_role->pivot->Account;
$account_role->pivot->Account();
$account_role->Account;
$account_role->Account();

我也尝试过hasOne()而不是belongsTo()。我完全迷失了。

我需要$ account_role-&gt; pivot-&gt;帐户才能返回帐户对象。很明显,我并没有完全搞砸,因为$ account_role-&gt; pivot-&gt; account_id会返回正确的帐户ID。

我错过了什么或者我在哪里陷入困境?如何在枢轴模型中正确定义关系以返回帐户模型???

提前感谢您提供的任何帮助!

0 个答案:

没有答案