where("records.schema_id", "=", $schemaId); } public static function withSort($query, string $fieldId, string $sortDir) { return $query ->join("records_data as sortData", function ($join) use ( $fieldId, $sortDir, ) { $join ->on("records.id", "=", "sortData.record_id") ->where("sortData.mode", "=", RecordMode::DRAFT) ->where("sortData.locale", "=", "main") ->where("sortData.field_id", "=", $fieldId); }) ->orderBy("sortData.value", $sortDir) ->distinct(); } // public static function withColumns($query, array $columns) // { // return $query // ->join("records_data as includeData", function ($join) use ( // $columns, // ) { // $join // ->on("records.id", "=", "includeData.record_id") // ->where("includeData.mode", "=", RecordMode::DRAFT) // ->where("includeData.locale", "=", "main") // ->whereIn("includeData.field_id", $columns); // }) // ->distinct(); // } public static function run($query, array $schemas, array $columns) { $records = $query->get(); $recordIds = $records->pluck("id")->toArray(); $recordData = DB::table("records_data") ->whereIn("record_id", $recordIds) ->whereIn("field_id", $columns) ->where("mode", "=", RecordMode::DRAFT) ->where("locale", "=", "main") ->get() ->map(RecordFieldModule::fromDb(...)) ->toArray(); $recordEdges = DB::table("edges") ->select( "edges.*", "records.id as record_id", "records.schema_id as record_schema_id", "records_data.value as record_title", ) ->whereIn("edges.from", $recordIds) ->whereIn("edges.field_id", $columns) ->join("records", "edges.to", "=", "records.id") ->join("records_data", function ($join) { $join ->on("records.id", "=", "records_data.record_id") ->on( "records.title_field_id", "=", "records_data.field_id", ); }) ->where("edges.mode", "=", RecordMode::DRAFT) ->where("edges.locale", "=", "main") ->where("records_data.mode", "=", RecordMode::DRAFT) ->where("records_data.locale", "=", "main") ->orderBy("edges.rank", "asc") ->distinct() ->get() ->map( fn($row) => RecordModule::edgeRecordPreviewFromDb( $schemas, $row, ), ) ->toArray(); return $records->map( fn($row) => static::fromDB( $row, $schemas, $recordData, $recordEdges, ), ); } /** @return QueryRecord[] */ public static function make() { return DB::table("records") ->select("records.*", "records_data.value as record_title") ->join("records_data", function ($join) { $join ->on("records.id", "=", "records_data.record_id") ->on( "records.title_field_id", "=", "records_data.field_id", ); }) ->where("records_data.mode", "=", RecordMode::DRAFT) ->where("records_data.locale", "=", "main"); // ->leftJoin( // "records_data", // "records.id", // "=", // "records_data.record_id", // ) } /** @param array $data */ private static function fromDB( stdClass $row, $schemas, array $recordData, array $recordEdges, ): QueryRecord { return new QueryRecord( null, RecordModule::fromDb($row), new RecordPreview( id: $row->id, schemaId: $row->schema_id, schemaName: collect($schemas) ->where("id", data_get($row, "schema_id")) ->first()->name, title: $row->record_title, ), collect($recordData) ->where("recordId", $row->id) ->values() ->toArray(), collect($recordEdges) ->where("edge.from", $row->id) ->values() ->toArray(), ); } }