Doctrine 2.1 - Obtaining Entity Constraints

I have two classes:
The game

/** @Entity @Table(name="games") */ class Game { /** @Id @GeneratedValue @Column(type="integer") */ protected $id; /** @Column(type="string", length=100) */ protected $title; /** @ManyToMany(targetEntity="News", mappedBy="games") */ protected $news; public function __construct() { $this->news = new \Doctrine\Common\Collections\ArrayCollection(); } public function getId() { return $this->id; } public function setTitle($val) { $this->title = trim($val); } public function getTitle() { return $this->title; } public function getNews() { return $this->news; } public function setNews($value) { $except_txt = 'Jedna z przesłanych wartości nie jest instancją klasy News!'; if(is_array($value)) { foreach($value as $v) { if($v instanceof News) $this->news->add($v); else throw new Exception($except_txt); } } else { if($value instanceof News) $this->news->add($value); else throw new Exception($except_txt); } } } 

news

 /** @Entity @Table(name="news") */ class News { /** @Id @GeneratedValue @Column(type="integer") */ protected $id; /** @Column(type="string", length=100) */ protected $title; /** @Column(type="text") */ protected $content; /** * @ManyToOne(targetEntity="User", inversedBy="news") * @JoinColumn(referencedColumnName="id") */ protected $author; /** @Column(type="datetime") */ protected $add_date; /** * @ManyToMany(targetEntity="Game", inversedBy="news") * @JoinTable(name="news_game", * joinColumns={@JoinColumn(name="news_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="game_id", referencedColumnName="id")} * ) */ protected $games; public function __construct() { $this->add_date = new DateTime(); $this->games = new \Doctrine\Common\Collections\ArrayCollection(); } # ID methods public function getId() { return $this->id; } # TITLE methods public function setTitle($val) { $this->title = $val; } public function getTitle() { return $this->title; } # CONTENT methods public function setContent($val) { $this->content = $val; } public function getContent() { return $this->content; } # AUTHOR methods public function setAuthor($val) { if($val instanceof User) $this->author = $val; } public function getAuthor() { return $this->author; } # ADD DATE methods public function getAddDate() { return $this->add_date; } # GAMES methods public function setGames($value) { $except_txt = 'Jedna z przesłanych wartości nie jest instancją klasy Game!'; if(is_array($value)) { foreach($value as $v) { if($v instanceof Game) $this->games->add($v); else throw new Exception($except_txt); } } else { if($value instanceof Game) $this->games->add($value); else throw new Exception($except_txt); } } public function getGames() { return $this->games; } } 

And this code:

 $i = 1; if($game->getNews()->count() > 0) { foreach($game->getNews()->getValues() as $v) { $news_list.= '<p>News '.$i.'</p>'; $i++; if($i == 6) break; } } 

1st question : Does Doctrine will download from the database all the news related to a particular game, or only the 5 that I need?

Second question : How can I download 5 news with a separate Doctrine doctor without the NewsGames class?

+4
source share
1 answer

Preliminary answer: Before I begin to answer, you do not need getValues ​​() from the News ArrayCollection. All you can do is:

 foreach ($game->getNews() as $news) { // Do whatever you want with associated News object. } 

Answer No. 1: Doctrine 2 will receive only news related to this game, and nothing more.

Answer # 2: You do not need a NewsGames object. Keep in mind that what you mapped is a join table, so in the long run an OO object never exists.

Greetings

Guillerme Blanco

+5
source

Source: https://habr.com/ru/post/1392948/


All Articles