Laravel 4.1 Eloquent - 使用whereHas的自定义连接

srp*_*rph 2 laravel eloquent

我正在使用名为seven_ora的2个连接的MSSQL(用户模型的表位于其中)和sho(字符模型的表所在的连接).

用户拥有多个字符,而一个字符属于用户.

当我尝试:

Character::whereHas('User', function($q) { $q->where('gm', 1); });
Run Code Online (Sandbox Code Playgroud)

它似乎使用与Character相同的连接.似乎whereHas中的闭包是Query\Builder?我期望whereHas使用它所指的任何模型的相应集合连接.

有没有办法在我的whereHas闭包上使用不同的连接?

Characters.php(模型)

class Character extends Base {

    /**
     * Connection used by the model
     *
     * @var string
     */
    protected $connection = 'sho';

    /**
     * Table used by the model
     *
     * @var string
     */
    protected $table = 'tblgs_avatar';

    /**
     * Fields fillable by the model
     *
     * @var array
     */
    protected $guarded = array('*');

    /**
     * Checks whether the model uses timestamps
     *
     * @var boolean
     */
    public $timestamps = false;

    /**
     * Decode the job name of the given ID
     *
     * @return  string
     */
    public function getJobName()
    {
        switch( $this->job ) {
            case 0:
                $job = 'Visitor';
                break;
            case 111:
                $job = 'Solider';
                break;
            case 221:
                $job = 'Muse';
                break;
            case 311:
                $job = 'Hawker';
                break;
            case 411:
                $job = 'Dealer';
                break;
            case 121:
                $job =  'Knight';
                break;
            case 122:
                $job = 'Champ';
                break;
            case 221:
                $job = 'Mage';
                break;
            case 222:
                $job = 'Cleric';
                break;
            case 321:
                $job = 'Raider';
                break;
            case 322:
                $job = 'Scout';
                break;
            case 421:
                $job = 'Bourg';
                break;
            case 422:
                $job = 'Artisan';
                break;
            default:
                $job = 'Untitled';
                break;
        }

        return $job;
    }

    /**
     * Rank the characters according to provided details
     *
     * @param   integer     $offset
     * @param   string      $field
     * @return  Character
     */
    public static function byTop($offset = 10, $field = null)
    {
        $characters = new static();

        if ( !is_null($field) ) $characters->orderBy($field, 'desc');

        return $characters->take($offset);
    }

    /**
     * A shortcut to access the name of the character
     *
     * @return  string
     */
    public function getNameAttribute()
    {
        return $this->txtNAME;
    }

    /**
     * A shortcut to access the level of the character
     *
     * @return  int
     */
    public function getLevelAttribute()
    {
        return $this->btLEVEL;
    }

    /**
     * A shortcut to access the job of the character
     *
     * @return  integer
     */
    public function getJobAttribute()
    {
        return $this->intJOB;
    }

    /*
    |--------------------------------------------------------------------------
    | ORM
    |--------------------------------------------------------------------------
    */

    /**
     * ORM with the [User] table
     *
     * @return  User 
     */
    public function user()
    {
        return $this->belongsTo('User', 'Account', 'txtACCOUNT');
        //$user = User::where('Account', $this->txtACCOUNT)->first();

        //return $user;
    }
}
Run Code Online (Sandbox Code Playgroud)

User.php(型号)

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

class User extends Base implements UserInterface, RemindableInterface {

    /**
     * Connection used by the model
     *
     * @var string
     */
    protected $connection = 'seven_ora';

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'userinfo';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('MD5PassWord', 'password');

    /**
     * Fields guarded by the model
     *
     * @var array
     */
    protected $guarded = array();

    /**
     * Fields fillable by the model
     *
     * @var array
     */
    protected $fillable = array(
        'Account',
        'Email',
        'MD5PassWord',
        'FirstName',
        'LastName',
        'MotherLName'
    );

    /**
     * Checks whether the model uses timestamps
     *
     * @var boolean
     */
    public $timestamps = false;

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->MD5PassWord;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

    /**
     * Validate input
     *
     * @param   array       $input
     * @param   integer     $id
     * @return  Validator
     */
    public function validate(array $input = array(), $id = null)
    {
        // Rules
        $username   = 'required|alpha_num|between:4,32|unique:userinfo,Account';
        $password   = 'required|between:4,48';
        $email      = 'required|email|unique:userinfo,Email';
        $mname      = 'required';

        // Unique rules
        if(!is_null($id)) {
            $unique      = ',' . $id;
            $username   .= $unique;
            $email      .= $unique;
        }

        $rules = array(
            'username'  =>  $username,
            'password'  =>  $password,
            'email'     =>  $email,
            'mname'     =>  $mname
        );

        $messages = array('mname.required' => "The security question field is required");

        Config::set('database.default', 'seven_ora'); // Set config
        return Validator::make($input, $rules, $messages);
    }

    /**
     * Change password of the user
     *
     * @param   string  $old
     * @param   string  $new
     * @return  boolean
     */
    public function changePassword($old, $new)
    {
        if(Hash::check($old, $this->MD5PassWord)) {
            $this->MD5PassWord = Hash::make($new);

            if($this->save()) return true;
        }

        return false;
    }

    /**
     * Checks if the user is a GM
     *
     * @return  boolean
     */
    public function isGM()
    {
        return ( $this->Right > 1 )
            ? true
            : false;
    }

    public function addVP($points)
    {
        $vp = $this->votePoint;
        $count = $vp->count;
        $vp->count = $count + $points;
        $vp->save();
    }

    /**
     * Since our server files does not use the typical
     * field names, this sets our username to whatever is being used
     *
     * @return  void
     */
    public function getUsernameAttribute()
    {
        return $this->Account;
    }

    /**
     * Since our server files does not use the typical
     * field names, this sets our password to whatever is being used
     *
     * @return  void
     */
    public function getPasswordAttribute()
    {
        return $this->MD5PassWord;
    }

    /**
     * A shortcut to the user's vote point count
     *
     * @return  void
     */
    public function getVpAttribute()
    {
        return $this->votePoint->count;
    }

    public function getDpAttribute()
    {
        return $this->donationPoint->count;
    }


    /*
    |--------------------------------------------------------------------------
    | ORM
    |--------------------------------------------------------------------------
    */

    /**
     * ORM with the Character model
     *
     * @return  Character
     */
    public function characters()
    {
        return $this->hasMany('Character', 'txtACCOUNT', 'Account');
        //$characters = Character::where('txtACCOUNT', $this->username)->get();

        //return $characters;
    }

    /**
     * ORM with the VotePoint model
     *
     * @return  VotePoint
     */
    public function votePoint()
    {
        return $this->hasOne('VotePoint');
    }

    /**
     * ROWM with the DonationPointmodel
     *
     * @return  DonationPoint
     */
    public function donationPoint()
    {
        return $this->hasOne('DonationPoint');
    }

    /**
     * ORM with the News model
     *
     * @return  News
     */
    public function news()
    {
        return $this->hasMany('News');
    }

    /**
     * ORM with the Slide model
     *
     * @return  Slide
     */
    public function slides()
    {
        return $this->hasMany('Slide');
    }

    /**
     * ORM with the VoteLog model
     *
     * @return  VoteLog
     */
    public function logs()
    {
        return $this->hasMany('VoteLog');
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 7

将数据库名称添加到User类中的$ table值.所以假设seven_ora也是数据库的名称:

protected $table = 'seven_ora.userinfo';
Run Code Online (Sandbox Code Playgroud)

  • 这确实可以作为一个解决方案,但很可能它应该可以工作,而不必这样做...... (2认同)