Laravel / Illuminate的useWritePdo在加载关系时不被尊重

时间:2018-06-21 19:05:49

标签: php mysql laravel laravel-5 illuminate-container

由于运行更新和刷新数据的竞争条件,我试图在运行select时强制我的应用程序使用写连接。我已经确认正在调用useWritePdo()函数,但是我可以说它没有使用写连接,因为竞争条件错误仍然存​​在。我不知道如何通过var转储确定是否正在使用写连接。

这是我正在运行的代码,然后是涉及的模型。请注意,如果我调用$ portfolio-> items($ useWriteConn)而不是$ user-> portfolios($ useWriteConn)-> with('items'),则强制执行写连接。

return $this->user->portfolios($useWriteConn)->with('items')->find($id);

User.php模型:

namespace App;

use App\Notifications\ResetPassword;
use App\Support\Auth\RetrievesUser;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

/**
 * Class User
 * @package App
 * @property integer $subscription
 * @property integer $id
 */
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, UserTrait, Notifiable, HasApiTokens, RetrievesUser


    public function portfolios($useWriteConn = false)
    {
        return ($useWriteConn) 
            ? $this->hasMany(UserPortfolio::class, 'user')->writeConn()
            : $this->hasMany(UserPortfolio::class, 'user');
    }
}

UserPortfolio.php模型

namespace App;

// use App\UserCustomViews;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserPortfolio extends Model 
{
    use SoftDeletes;

    public function user()
    {
        return $this->belongsTo(User::class, 'user');
    }

    public function items($useWriteConn = false, $showClosed = true)
    {
        $items = $this->hasMany(UserPortfolioItem::class, 'portfolio');

        if ($useWriteConn)
            $items->writeConn();
        if (!$showClosed)
            $items->open();

        return $items;
    }

    public function scopeWriteConn($query)
    {
        return $query->useWritePdo();
    }
}

模型UserPortfolioItem.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserPortfolioItem extends Model 
{

    public function portfolio()
    {
        return $this->belongsTo(UserPortfolio::class, 'portfolio');
    }

    public function scopeWriteConn($query)
    {
        return $query->useWritePdo();
    }
}

1 个答案:

答案 0 :(得分:0)

对于以后遇到此问题的任何人,我都在另一个线程中找到了答案。

Eloquent pre-loading relationships against DB write connection

Model::onWriteConnection()->with(['relationship'=>function($query){
   $query->useWritePdo();
}])->find($id)