Laravel - 在关系急切负载中集成大的原始查询

问题描述 投票:0回答:1

我已经使用laravel大约4年了,但现在我正面临着我最大的挑战。我正在重构和扩展旧的PHP应用程序,用于映射我工作的办公室。有一个巨大的SQL查询,我需要以某种方式集成到Laravel的QueryBuilder中以提供一个急切的加载关系。

流程是这样的

Building => hasMany: Floor => hasMany: Seat => hasMany: BookedSeat => belongsTo: User

其中BuildingFloorSeatBookedSeatEloquent模型。

我的巨大查询根据许多其他条件从BookedSeat选择当前日期的Seat预订,例如预订座位的人是在家庭办公室,度假等(这些存储在其他一些表中)并设置属性BookedSeat实例称为Status,以了解是否在当天采取了Seat

现在我正在尝试将这个原始查询集成到构建一个JSON层次结构中,然后我将其发送到在前端运行的Vue.js应用程序。

层次结构类似于:

 {
   "buildings": [
     {
        // properties
        "floors" : [
           {
              //properties
              "seats": [
                  {
                     //properties
                     "booked": [
                        {
                           "user": "some user model",
                           "Status": "some booked status"
                        }
                      ]
                  },
                  // other seats
              ] 
           }, 
           // other floors
        ]
     },
     //other buildings
   ]
}

巨大的查询返回一个对象数组,然后我可以使用它来水合BookedSeat集合,但我不知道我怎么可以使用这个集合或直接使用巨大的查询,以便为每个BookedSeat急切加载每个SeatFloor为每个Building,让框架为我做繁重的工作。

我尝试的是构建如下方法:

public static function bookedSeatsForFloor(Relation $seatQuery, Relation $bookedQuery, Carbon $forDate)
    {
        $format = $forDate->format('Y-m-d H:m:i');
        $bindings = $seatQuery->getBindings();

        /** @var AvailableSeatsQuerySimple $availableSeats */
        $availableSeats = new AvailableSeatsQuerySimple($bindings, $format); // bindings are my floor id's and I'm feeding them to my big query in order to have control over which floors to load depending on the user's rights 

        return DB::raw($availableSeats->getRawQuery());
    }

并称之为:

Floor::where('id', $someId)->with(['seats' => static function ($seatQuery) use ($_that) {

    /**
     * Add to each seat the manager and the active booking
     */
    $seatQuery->with(['booked' => static function ($bookedQuery) use ($seatQuery, $_that) {
        return self::bookedSeatsForFloor($seatQuery, $bookedQuery, $_that->forDate);
    }, 'manager'])->orderBy('seat_cir');
}]);

但我需要以某种方式用$bookedQuerybookedSeatsForFloor修改$bookedQuery->select('something')方法中的$bookedQuery->setQuery('some query builder instance'),但我不知道如何将巨大的查询转换为Builder实例。

谢谢!

PS:由于复杂性,我最好想跳过将大量查询重写成雄辩的语法

增加细节:

因此,根据要求,这是我的原始查询,其中我更改了每个公司策略的一些数据库/表名称

    SET NOCOUNT ON;
DECLARE
    @the_date DATETIME;
SET @the_date = (SELECT CONVERT(DATETIME, ?, 120));

SELECT seat_identifier,
       user_id,
       FromDate,
       ToDate,
       Status
FROM (
         SELECT d.seat_identifier,
                d.user_id,
                d.FromDate,
                d.ToDate,
                CASE
                    WHEN d.Status IS NULL
                        THEN 0
                    WHEN d.Status = 2
                        THEN 2
                    WHEN d.Status != 0
                        THEN CASE
                                 WHEN -- New, OnGoing Request in Main_DB_Name
                                             ho.status = 1 -- New StatusType in Main_DB_Name
                                         OR
                                             ho.status = 4 -- Pending StatusType in Main_DB_Name
                                         OR
                                             twl.status = 1 -- New StatusType in Main_DB_Name
                                         OR
                                             twl.status = 4 -- Pending StatusType in Main_DB_Name
                                         OR
                                             li.status = 1 -- New StatusType in Main_DB_Name
                                         OR
                                             li.status = 4 -- Pending StatusType in Main_DB_Name
                                         OR
                                             ctaf.status = 1 -- New StatusType2 in Main_DB_Name
                                         OR
                                             ctaf.status = 2 -- Ongoing StatusType2 in Main_DB_Name
                                     THEN
                                     2 --> Pending seat in MyApplication


                                 WHEN -- Approved Request in Main_DB_Name
                                             ho.status = 2
                                         OR
                                             twl.status = 2
                                         OR
                                             li.status = 1
                                         OR
                                             li.status = 2
                                         OR
                                             ctaf.status = 1
                                         OR
                                             ctaf.status = 2
                                     THEN 0 -- Free Seat MyApplication

                                 ELSE 1 -- Taken  Seat MyApplication
                        END
                    END as 'Status'
         FROM (
                  SELECT seats.seat_identifier as seat_identifier,
                         c.user_id,
                         c.FromDate,
                         c.ToDate,
                         c.Status
                  FROM (
                           SELECT fo_bs.seat_identifier,
                                  fo_bs.user_id,
                                  fo_bs.FromDate,
                                  fo_bs.ToDate,
                                  fo_bs.Status
                           FROM MyApplication.another_schema.BookedSeats fo_bs
                                    INNER JOIN MyApplication.another_schema.seats AS seats ON fo_bs.seat_identifier = seats.seat_identifier
                               WHERE fo_bs.FromDate <= @the_date
                                    AND fo_bs.ToDate >= @the_date
                                    AND fo_bs.Status IN (1, 2)
                                    AND seats.floor_id IN (###FLOOR_IDS###) -- will replace this from php with a list of "?,?,?" depending on how many floor_ids are in the query bindings
                       ) c
                           INNER JOIN MyApplication.another_schema.seats AS seats ON c.seat_identifier = seats.seat_identifier) d
                  LEFT JOIN (SELECT requester, status
                             from Main_DB_Name.schema.HOME_OFFICE
                                 WHERE Main_DB_Name.schema.HOME_OFFICE.from_date <= @the_date
                                      and Main_DB_Name.schema.HOME_OFFICE.to_date >= @the_date) ho ON d.user_id = ho.requester
                  LEFT JOIN (SELECT requester, status
                             from Main_DB_Name.schema.TEMPORARY_WORK_LOCATION
                                 WHERE Main_DB_Name.schema.TEMPORARY_WORK_LOCATION.from_date <= @the_date
                                      and Main_DB_Name.schema.TEMPORARY_WORK_LOCATION.to_date >= @the_date) twl
                            ON d.user_id = twl.requester
                  LEFT JOIN (SELECT employee, status
                             from Main_DB_Name.schema.LEAVE_INVOIRE
                                 WHERE Main_DB_Name.schema.LEAVE_INVOIRE.leave_date = @the_date) li ON d.user_id = li.employee
                  LEFT JOIN (SELECT requester, status
                             from Main_DB_Name.schema.TRAVEL
                                 WHERE Main_DB_Name.schema.TRAVEL.from_date <= @the_date
                                      and Main_DB_Name.schema.TRAVEL.until_date >= @the_date) ctaf
                            ON d.user_id = ctaf.requester
     ) y

我的模型/关系如下:

class Building extends Model {
  /* Properties */

   public function floors()
    {
        return $this->hasMany(Floor::class);
    }
}

class Floor extends Model {
  /* Properties */
   public function building()
    {
        return $this->belongsTo(Building::class);
    }

   public function seats()
    {
        return $this->hasMany(Seat::class);
    }
}

class Seat extends Model {
  /* Properties */
   public function floor()
    {
        return $this->belongsTo(Floor::class);
    }

   public function booked()
    {
        return $this->hasMany(BookedSeat::class);
    }
}

class BookedSeat extends Model {
  /* Properties */
   public function user()
    {
        return $this->belongsTo(User::class);
    }

   public function seat()
    {
        return $this->belongsTo(Seat::class);
    }
}
php laravel eloquent eager-loading laravel-query-builder
1个回答
0
投票

这个问题非常困难。我一直坚持尝试不同的东西超过一个星期,但找不到任何好办法。

我最后使用@Jonas Staudenmeir的建议,通过手动映射我所有的嵌套关系,然后在我的booked模型实例上设置相应的Seat关系,该关系来自使用BookedSeat::hydrate()获得的集合,原始查询的结果作为参数。

$availableSeats = new AvailableSeatsQuerySimple($format);

        // Map through all the Buildings
        $this->template['buildings'] = $this->template['buildings']
            ->map(static function (Building $building) use ($availableSeats) {

                // Map through all the Floors in a Building
                $floors = $building->floors->map(static function (Floor $floor) use ($availableSeats) {
                    /** @var BookedSeat|Collection $booked */
                    $booked = $availableSeats->execute($floor->id); // execute the raw query and get the results

                    if(count($booked) > 0) {

                        // Map through all the Seats in a Floor
                        $seats = $floor->seats->map(static function (Seat $seat) use ($booked) {

                            // Select the BookedSeat for the corresponding Seat
                            /** @var BookedSeat $bookedSeatForRelation */
                            $bookedSeatForRelation = $booked->filter(static function (BookedSeat $bookedSeat) use ($seat) {
                                return $bookedSeat->seat_identifier === $seat->id;
                            })->first();

                            // Attach the BookedSeat to the Seat only if the Status IS NOT 0
                            if($bookedSeatForRelation !== null && $bookedSeatForRelation->Status !== 0) {
                                return $seat->setRelation('booked', $bookedSeatForRelation);
                            }

                            return $seat->setRelation('booked', null);
                        });

                        return $floor->setRelation('seats', $seats);
                    }

                    return $floor;
                });

                return $building->setRelation('floors', $floors);
            });
© www.soinside.com 2019 - 2024. All rights reserved.