Laravel Eloquent没有连接到关系的正确数据库?

时间:2017-04-20 07:33:32

标签: php mysql laravel laravel-5 eloquent

我有一个看起来像这样的收件箱模型:

<?php

namespace Uppdragshuset\AO\Tenant\Models;

use Illuminate\Database\Eloquent\Model;
use Uppdragshuset\AO\Tenant\Models\PM\TaskUser;
use Uppdragshuset\AO\Tenant\Models\PM\User;

class Inbox extends Model
{

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'case_id',
        'index',
        'workflow_id',
        'task_id',
        'user_id',
        'group_id',
        'patent_id',
        'workflow_title',
        'task_title',
        'task_description',
        'group_title',
        'inactive_percentage',
        'priority',
        'due_at',
        'created_at',
        'updated_at'
    ];

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

    public function task_users()
    {
        return $this->hasMany(TaskUser::class, 'TAS_UID', 'task_id');
    }

    public function getTasksForUser($with = [], $user_id = null){
        if ( ! $user_id) {
            $user_id = request()->get('auth_user_id');
        }

        return $this->with($with)
            ->whereHas('task_users', function ($query) use ($user_id) {
                $query->where('USR_UID', $user_id)
                    ->orWhere('TU_RELATION', 2)->whereHas('group', function ($query) use ($user_id) {
                        $query->whereHas('users', function ($query) use ($user_id) {
                            $query->where('USERS.USR_UID', $user_id);
                        });
                    });
            })
            ->where('USR_UID', '')
            ->orWhere('USR_UID', $user_id)->get();
    }
}

TaskUser模型如下所示:

<?php

namespace Uppdragshuset\AO\Tenant\Models\PM;

use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;

class TaskUser extends Model
{

    use Eloquence, Mappable;

    protected $connection = 'processmaker';

    protected $table = 'TASK_USER';

    protected $maps = [
        'task_id'  => 'TAS_UID',
        'user_id'  => 'USR_UID',
        'type'     => 'TU_TYPE',
        'relation' => 'TU_RELATION',
    ];

    public function userOrGroup()
    {
        if ($this->relation == 1){
            return $this->belongsTo(User::class, 'USR_UID', 'USR_UID');
        }else{
            return $this->belongsTo(Group::class, 'USR_UID', 'GRP_UID');
        }
    }

    public function group()
    {
        return $this->belongsTo(Group::class, 'USR_UID', 'GRP_UID');
    }
}

当我在收件箱模型中运行getTasksForUser时,它会给我这个错误:

  

SQLSTATE [42S02]:找不到基表或视图:1146表   'tenant.TASK_USER'不存在(SQL:select * from inbox where   存在(选择*来自TASK_USER TASK_USERTAS_UID =   inboxtask_id和(USR_UID = 00000000000000000000000000000001或   TU_RELATION = 2并且存在(从GROUPWF中选择*   TASK_USERUSR_UID = GROUPWFGRP_UID并且存在(select * from   USERS上的GROUP_USER内部加入USERSUSR_UID =   GROUP_USERUSR_UID其中GROUP_USERGRP_UID =   GROUPWFGRP_UIDUSERSUSR_UID =   00000000000000000000000000000001))))和USR_UID =或USR_UID =   00000000000000000000000000000001)

所以它抱怨我没有连接到TASK_USER表的正确数据库。但是TaskUser模型的$connection属性是使用另一个db指定的。那么我做错了什么?

0 个答案:

没有答案