Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to filter a getter in doctrine2 entity?

I have an entity that has a one-to-many association (many-to-many with extra fields):

class Game {
    /**
    /* @OneToMany(targetEntity="GamePlayer", mappedBy="game", cascade={"persist"})
    /* @JoinColumn(name="id", referencedColumnName="game_id", onDelete="cascade")
     */
    private $gamePlayer;
}

The class has automated getter for all the authors: getGamePlayers()

I would like to add a filter to it, so it would query the database only for the relevant details in the most efficient way:

public function getGamePlayersWithScoreHigherThan($score){
    //what to write here? (return array)
}

What is the best way to achieve such a getter from within the entity (not using the repository)?

Thank you very much!

like image 490
Koby Avatar asked Sep 24 '12 15:09

Koby


1 Answers

You could try creating a separate method on your entity that uses the Doctrine\Common\Collections\Criteria to filter the associated collection. See this link for details.

like image 168
Exander Avatar answered Sep 20 '22 02:09

Exander