gpt4 book ai didi

php - 如何限定 Eloquent 关系的范围(或执行传统连接)

转载 作者:可可西里 更新时间:2023-11-01 12:39:46 26 4
gpt4 key购买 nike

我正在尝试构建一组 Eloquent 模型,它们代表现有的硬件设备数据库(两者都不能更改)。我知道如何在 SQL 中执行此操作,但正在努力构建使用第三个表的模型关系之一,类似于关系/连接表,但用于与复合键的一对一关系。

共有三个实体(简化):

  1. 设备
  2. session
  3. 设备用户

一个用户可以同时在多个设备中,并且具有与这些设备关联的 session 日志。用户确实有一个唯一的 ID,但从设备的角度来看,他们只有一个“用户编号”,很短(3 个字节),因此不能代表整个用户范围,因此它被映射到 device_user 表中。 (它实际上比这更复杂,但为了这个问题的目的,我把它剥离了)

设备表:

d_id                PK
[data fields...]

设备用户表:

du_uid              User's actual ID
du_device_id FK to device.d_id
du_number 000-999
[metadata...]

session 表:

s_device_id         device.d_id
s_user_number 000-999 (device_user.du_number)
[data fields...]

场景:我有一个 session ,我想查找具体的device_user.d_uid。在 SQL 中,我会做类似的事情:

SELECT session.blah, du_uid
FROM session
INNER JOIN device_user ON du_device_id = s_device_id AND du_number = s_user_number

所以我想这使得它实际上只是复合键上的关系。

我在 Eloquent 中试过的是这样的:

class SessionLogModel {

public function device(){
return $this->belongsTo('MyModels\\DeviceModel', 's_device_id', 'd_id');
}

public function user(){
return $this->belongsTo('MyModels\\DeviceUserModel', 's_user_number', 'du_number')

// A) I tried:
->withDevice($this->s_device_id);

// or B) I tried:
->withDevice($this->device());

}

// example usage
public static function getRecentUser(DateTime $localTime, $deviceId){
$u = null;

// get the preceding session log
$q = SessionLogModel::where('session_type', '=', 'S')
->where('session_device_id', '=', $deviceId)
->where('sesison_date', '<=', $localTime)
->orderBy('session_id', 'DESC')
->take(1)
->with('device')
->with('user');
$s = $q->get()->first();

$u = $s->user->du_uid; // use the unique user ID
...
}
}

class DeviceUserModel {
// A)
public function scopeWithDevice($query, $device_id){
return $query->where('du_device_id', '=', $device_id);
}
// OR B) I tried:
public function scopeWithDevice($query, $device){
return $query->where('du_device_id', '=', $device->d_id);
}
}

我已经尝试了多种方法来将匹配限制为具有范围或其他“where”构造的两列,但似乎总是无法通过 BelongsTo“发送”正确的值。检查 DB::getQueryLog 时,设备 ID 为 NULL。但是,如果我在 belongs 中硬编码一个值,我可以看到它“有效”。

我对此进行了大量研究,但发现很难找到类似的结构说明。

我正在使用 Laravel v4.2 中的 Eloquent 独立使用(不在 Laravel 中)。

上述基于范围的方法行得通吗?还是我应该考虑其他方法?

最佳答案

我刚刚遇到了这个有趣的问题:我厌倦了在 laravel 中模拟你的表如下:

public function up(){
Schema::create('session', function (Blueprint $table) {
$table->increments('id');
$table->integer('s_device_id');
$table->string('s_user_number',20);
$table->timestamps();
});
Schema::create('device', function (Blueprint $table) {
$table->increments('d_id');
$table->string('blah',20);
$table->timestamps();
});
Schema::create('device_user', function (Blueprint $table) {
$table->integer('du_device_id')->unsigned();
$table->integer('du_uid')->unsigned();
$table->string('du_number',20);
$table->primary(['du_device_id', 'du_uid']);//important composite key
$table->timestamps();
});
}
//then do the relations on Medels:
//User Model
public function deviceUser(){
return $this->hasOne(DeviceUser::class,'du_uid');
}
//Device Model
public function deviceUser(){
return $this->hasOne(DeviceUser::class,'du_device_id','d_id');
}
//DeviceUser Model
public function device(){
return $this->belongsTo(Device::class,'du_device_id','d_id');
}

public function user(){
return $this->belongsTo(User::class,'du_uid');
}
//Session Model //[Not the Session Facade of Laravel]
public function device(){
return $this->belongsTo(Device::class,'s_device_id');
}
//Now let us do the work in SessionController after filling your tables with demo data for e.g.
//all these relations are working fine!
$device = Device::where('d_id',1)->first();
$user = User::where('id',4)->first();

//dd($user,$user->deviceUser,$device,$device->deviceUser);//here instance objects and their relations can be fetched easily

$device_user = DeviceUser::where('du_device_id',1)->where('du_uid',4)->first();

//dd($device_user,$device_user->device);
//$session = Session::where('id',100)->first();//can get session by ID
$session = Session::where('s_device_id',1)->where('s_user_number','000-999')->first();//get session by unique composite key which what you are after. It is similar to the sql Query that you built. Then you can easily fetch the relations as follows:
dd($session,$session->device,$session->device->deviceUser);

希望这会有所帮助!

关于php - 如何限定 Eloquent 关系的范围(或执行传统连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33871337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com