0

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

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

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

模型用户.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();
    }
}
4

1 回答 1

0

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

针对数据库写入连接的雄辩的预加载关系

Model::onWriteConnection()->with(['relationship'=>function($query){
   $query->useWritePdo();
}])->find($id)
于 2018-06-22T16:26:23.627 回答