我有一个看起来像这样的收件箱模型:
<?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_USER
。TAS_UID
=inbox
。task_id
和(USR_UID
= 00000000000000000000000000000001或TU_RELATION
= 2并且存在(从GROUPWF
中选择*TASK_USER
。USR_UID
=GROUPWF
。GRP_UID
并且存在(select * fromUSERS
上的GROUP_USER
内部加入USERS
。USR_UID
=GROUP_USER
。USR_UID
其中GROUP_USER
。GRP_UID
=GROUPWF
。GRP_UID
和USERS
。USR_UID
= 00000000000000000000000000000001))))和USR_UID
=或USR_UID
= 00000000000000000000000000000001)
所以它抱怨我没有连接到TASK_USER表的正确数据库。但是TaskUser模型的$connection
属性是使用另一个db指定的。那么我做错了什么?