PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – 如何确定Eloquent关系的范围(或执行传统连接)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试构建一组Eloquent模型,它们代表现有的硬件设备数据库(两者都不能更改).我知道如何在sql中执行此操作,但我正在努力构建一个使用第三个表的模型关系,类似于关系/联结表,但是与复合键一对一的关系.

有三个实体(简化):

>设备
>会议
> device_user

用户可以同时在许多设备中,并且具有与这些设备相关联的会话日志.用户确实拥有唯一的ID,但从设备的角度来看,它们只有一个用户号”,它只是短(3个字节),因此不能代表整个用户范围,因此它映射在device_user表中. (它实际上比这更复杂,但出于这个问题的目的,我已经将其剥离了)

设备表:

d_id                PK
[data fields...]

device_user表:

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

会话表:

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

场景:我有一个会话,我想查找特定的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(datetiR_373_11845@e $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形式出现.但是,如果我对属性中的值进行硬编码,我可以看到它“正常工作”.

对此进行了相当多的研究,但我发现很难找到类似的结构.

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

以上基于范围的方法是否有效?
或者我应该看一个不同的方法

我刚刚遇到了这个有趣的问题:
我厌倦了如下所示在laravel中模拟你的桌子如下:
public function up(){
    scheR_373_11845@a::create('session',function (Blueprint $tablE) {
        $table->increments('id');
        $table->Integer('s_device_id');
        $table->String('s_user_number',20);
        $table->timestamps();
    });
    scheR_373_11845@a::create('device',function (Blueprint $tablE) {
        $table->increments('d_id');
        $table->String('blah',20);
        $table->timestamps();
    });
    scheR_373_11845@a::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,'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关系的范围(或执行传统连接)全部内容,希望文章能够帮你解决php – 如何确定Eloquent关系的范围(或执行传统连接)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。