We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem with update and relationship

Hi I have a form with only few properties of a model, and I want that model only update these fields.

I made this update call

return $chapter->update($chapter, [
                'name', 'number', 'subNumber', 'visibility', 'teams'
]);

to update only these listed fields (teams is the alias of a relationship)

My problem is, when I update the model, I have a message saying createdAt is required. This createdAt is not in the Form, so there is no value for this, okay, but I don't want to update it, I didn't list it in the whitelist.

To test I dleted the NotNull on the column and It "worked", but the field is now NULL ...

Other problem is, I have another column deleted is set to NULL too.

I don't understand this problem...

edit : After some tests, I saw that the relationships teams recreate a new line in the joint table each updates. ..

Here is how the relationship is declared

In Chapter.php

$this->hasManyToMany(
            'id',
            ChapterTeams::class,
            'chapterId',
            'teamsId',
            Teams::class,
            'id',
            [
                'alias' => 'Teams'
            ]
);

in Teams.php

$this->hasManyToMany(
            'id',
            Teams::class,
            'teamsId',
            'chapterId',
            ChapterTeams::class,
            'id'
        );

in ChapterTeams.php

$this->setSource('chapter_teams');

        $this->belongsTo(
            'chapterId',
            Chapter::class,
            'id'
        );
        $this->belongsTo(
            'teamsId',
            Teams::class,
            'id'
        );

I don't understand how this can work like that ...

Edit 2 : Now I try to delete the existing relationship with $chapter->teams->delete() but it doesn't do anything (return false and no messages in $chapter->teams->getMessages()) So I'm completely stuck, the relation is duplicated each update (I added a unique constraint on the both fields to prevent that and I now have a fatal error).

I don't understand how to handle the relations in Phalcon and the docs is pretty poor about that.

Phalcon seems poor with its built-in ORM/ODM, If every request need to be done in pure SQL to be correct and/or to be working, There is no interest in that.

Edit 3 : So to resume all my problems how to handle update model with relationship (with a "relation table") (the relation is mangaed with a multi select in a Form) :

  • Do I need to delete the relationship (in 3rd table) and then re add them ? in this case, how to delete the relation ?
  • is there a way that automatically handle addition/deletion in relationship ? Like, I get my object (Chapter) that have one or more Teams. in the Form, the multiselect is filled with existing teams in Chapter. When I submit the form, automatically watch the ChapterTeams relationship and add/delete if needed (if the relation already exist, nothing to do).

Thanks in advance to help me with this, it's completely blocking for my app ^^"



145.0k
Accepted
answer
edited Dec '17

I never liked how phalcon handles all relation creation/update stuff - just do it yourself, like create transaction, create ChapterTeams instances yourself and commit transaction or rollback if any creation fails.



8.2k

Hmm, Ok, I'll see how to do that by myself. I assume I'll need to do "plain SQL" but I don't really have a choice there.

I'll just do a manual deletion of relationships in joint table, as when I save Phalcon try to save all the relation.

Thanks a lot, but I think ORM and ODM need a bot of rework maybe, the only problems I got are related to these two ^^"