Gareth
Gareth

Reputation: 5719

Add items to Phalcon Many-to-Many relationship

I have two models in many to may relationship - let's use lecturers and students as an example.

class Lecturer
{
    public function initialize()
    {
        $this->hasMany('studentId', 'Model\Entity\LecturerStudent', 'studentId', ['alias' => 'LecturerStudent']);
        $this->hasManyToMany(
            'lecturerId',
            'Model\Entity\LecturerStudent',
            'lecturerId',
            'studentId',
            'Model\Entity\Student',
            'studentId',
            ['alias' => 'Students']
        );
    }
}

class LecturerStudent
{
    public function initialize()
    {
        $this->belongsTo('studentId', 'Model\Entity\Student', 'studentId', ['alias' => 'Student']);
        $this->belongsTo('lecturerId', 'Model\Entity\Lecturer', 'lecturerId', ['alias' => 'Lecturer']);
    }
}

class Student
{
    public function initialize()
    {
        $this->hasMany('lecturerId', 'Model\Entity\LecturerStudent', 'lecturerId', ['alias' => 'LecturerStudent']);
    }
}

Now, when I want to add students to lecturers all I need to do is:

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$lecturerA->Students = [$studentA, $studentB];
$lecturerA->save();

This all works as I'd expect.

The problem arises in my application when I'm importing a number of records in one transaction and I need to add a second array to the relationship.

So in the example:

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$lecturerA->Students = [$studentA, $studentB];

... other code doing other things ...

$studentC = new Student();
$studentD = new Student();
$lecturerA->Students = [$studentC, $studentD];

... more code ...

$lecturerA->save();

In this case, only the item(s) added in the second assignment to Students are saved. The item(s) from the first assignment are lost. So in my example only studentC and studentD will be written to the database.

Is there a correct way to do this - to add to a previous many-to-many array - in Phalcon? I have an alternative (messier) way to do this within the class that's performing the import, but if there's a correct way I'd much prefer to use that.

Upvotes: 0

Views: 1055

Answers (1)

Zamrony P. Juhara
Zamrony P. Juhara

Reputation: 5262

You need to store students objects to temporary array, because second assignment will overwrite $lectureA->Students.

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$tmpStudents = [$studentA, $studentB];

... other code doing other things ...

$studentC = new Student();
$studentD = new Student();
$tmpStudents = $tmpStudents + [$studentC, $studentD];
$lecturerA->Students = $tmpStudents;

... more code ...

$lecturerA->save();

or you can call $lecturerA->save() method after you set students relationship.But off course this will cause higher network traffic because each time you call save(), INSERT or UPDATE query will have to be made to RDBMS.

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$lecturerA->Students = [$studentA, $studentB];
$lecturerA->save();

... other code doing other things ...

$studentC = new Student();
$studentD = new Student();
$lecturerA->Students = [$studentC, $studentD];

... more code ...

$lecturerA->save();

Upvotes: 1

Related Questions