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

时间:2014-05-04 14:46:18

标签: laravel eloquent

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

用户有多个字符,一个字符属于用户。

当我尝试:

Character::whereHas('User', function($q) { $q->where('gm', 1); });

似乎使用与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;
    }
}

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');
    }

}

3 个答案:

答案 0 :(得分:7)

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

protected $table = 'seven_ora.userinfo';

答案 1 :(得分:1)

您也可以通过构造函数中的配置变量添加它以适应不同的环境

  public function __construct($attributes = array())  {
        parent::__construct($attributes); 

        $this->table = Config::get('database.connections.connection.database').'.model_table_name';
  }

答案 2 :(得分:0)

这可能会有所帮助:

  1. 当您使用Character :: (“用户”,...)时,查询构建器会创建单个数据库查询,因此只能使用一个连接(及其凭据)。该查询类似于:
    • SELECT * FROM characters WHERE (SELECT COUNT(*) FROM users WHERE characters.user_id = user.id AND gm = 1) >= 1; [使用在角色模型上定义的连接(如您所注意到的)]
  2. 当您分别引用一个关系或使用诸如Character :: with ('user')之类的急切加载时,将执行两个单独的数据库查询,每个查询使用其各自的连接(和连接凭据):
    • SELECT * FROM characters; [使用在角色模型上定义的连接]
    • SELECT * FROM users WHERE id IN (1, 2, 3, …); [使用在用户模型上定义的连接]
如解决方案所示,

将数据库名称添加到表名称中,仅在Character模型连接具有访问其他数据库的权限时有效,因为您仍在使用Character模型连接。