orm icon indicating copy to clipboard operation
orm copied to clipboard

🐛❓ Relations without fk, MultiReation of same Class

Open andrelec1 opened this issue 1 month ago • 3 comments

No duplicates 🥲.

  • [x] I have searched for a similar issue in our bug tracker and didn't find any solutions.

What happened?

I try to link the same class twice in another. But the orm didn't let me do.

How can i do something like this :

#[Entity]
class Game extends AbstractModel
{
    #[HasOne(target: User::class, innerKey: 'player_1_id', nullable: true, fkCreate: false, fkOnDelete: 'NO ACTION')]
    private ?User $player1;
    
    #[HasOne(target: User::class, innerKey: 'player_2_id', nullable: true, fkCreate: false, fkOnDelete: 'NO ACTION')]
    private ?User $player2;
    
#[Entity(table: 'users')]
class User extends AbstractModel
{
    #[hasOne(target: Game::class, innerKey: 'current_game_id', nullable: true, fkCreate: false, fkAction: 'NO ACTION')]
    private ?Game $currentGame = null;

A game have 2 user ( player_1 and player_2 ) , the user have a reference to the game... but in the game class he can be the player 1 or the player 2 ...

Version

ORM 2.3.4
PHP 8.4

andrelec1 avatar Dec 07 '25 05:12 andrelec1

We use HasOne relation when we want to link inner PK with an outer field. Looks like BelongsTo or RefersTo should be used in all the relations there.

roxblnfk avatar Dec 07 '25 12:12 roxblnfk

but how can you tel currentGame can refers player_1 and player_2 ?

andrelec1 avatar Dec 07 '25 17:12 andrelec1

what do you mean?

roxblnfk avatar Dec 07 '25 17:12 roxblnfk