flat3 / lodata

The OData v4.01 Producer for Laravel
https://lodata.io/
MIT License
80 stars 27 forks source link

$expand pivot table of polymorphic many to many relationship #838

Closed mgerzabek closed 2 months ago

mgerzabek commented 3 months ago

I have a problem getting the pivot attributes of a polymorphic many to many relationship. Is this possible with this library?

Let's assume whe have four tables, Students, Teachers, Courses, and Coursables, where the last one can hold either a relation to a student or a teacher.

CREATE TABLE Students (
    StudentID INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL
);
CREATE TABLE Teachers (
    TeacherID INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL
);
CREATE TABLE Coursable (
    CoursableID INT,
    CoursableType VARCHAR(50) NOT NULL, -- 'Student' or 'Teacher'
    CourseID INT,
    Year INT NOT NULL,
    Semester VARCHAR(10) NOT NULL, -- 'Spring' or 'Fall'
    PRIMARY KEY (CoursableID, CoursableType, CourseID),
    FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
CREATE TABLE Courses (
    CourseID INT PRIMARY KEY,
    Title VARCHAR(100) NOT NULL
);

And in the model classes we have the relations setup like

(i) Course


    #[LodataRelationship]
    public function students(): MorphToMany
    {
        return $this->morphedByMany(Student::class, 'coursable')
            ->using(Coursable::class)
            ->withPivot(['semester', 'year']);
    }

(ii) Student


    #[LodataRelationship]
    public function courses(): MorphMany
    {
        return $this->morphMany(Course::class, 'coursable')
            ->using(Coursable::class)
            ->withPivot(['semester', 'year']);
    }

Is it possible with this library the query a course with all related students and teachers and with the pivot attributes?

When I try with a request like /odata/Courses(1)?$expand=teachers,students I get correctly all enrolled teachers and students, but without the pivot attributes.

Any hints welcomed!