Tell me who works with doctrine, as in my case correctly add entries.

There is a table Category -> id , name it is filled with categories.

 // film.php /** * @ORM\ManyToMany(targetEntity="Category") * @ORM\JoinTable(name="films_categories", * joinColumns={@ORM\JoinColumn(name="film_id", referencedColumnName = "id")}, * inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}) */ private $categories; public function __construct(){ $this->categories = new ArrayCollection(); } public function addCategories(ArrayCollection $categories){ dump($categories); die; foreach($categories as $category){ $this->categories->add($category); } } 

I have an array with data:

 // Π² Ρ†ΠΈΠΊΠ»Π΅ заполняю ΡΡƒΡ‰Π½ΠΎΡΡ‚ΡŒ Ρ„ΠΈΠ»ΡŒΠΌ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠ΅ΠΉ. foreach($data as $key => $info) { $film = new Film(); $film->SetTitle($info['title']); // массив с ΠΊΠ»ΡŽΡ‡Π°ΠΌΠΈ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΉ $info['genres'] = ['array key' => 'category key'] 

The categories were initially entered into the Database in the Category table, about 20 of them, the table with films is empty. I have an array of data about each film, as well as what categories it belongs to. How to fill in the movie with information is understandable. But how to add a link to the categories? Sketches of code that counted something to tell you, threw off. Write if you need any other information.

I get

Type error: Argument 1 passed to AppBundle \ Entity \ Film :: addCategories () must be an instance of Doctrine \ Common \ Collections \ ArrayCollection, instance o

  • Do you call addCategories () anywhere else? - Zhukov Roman
  • In the cycle, it is foreach where I add fields to the essence of the film - Arturas
  • give the code that calls the addCategories () method - Igor Karpenko

2 answers 2

Here is the whole function that adds to the database:

 private function InsertFilmsAndRelations($data) { $em = $this->getDoctrine()->getEntityManager(); foreach($data as $key => $info) { $film = new Film(); $film->SetTitle($info['title']); $film->SetSummary(trim($info['summary'])); $film->SetRating($info['rating']); $film->setTrailerUri($info['trailer_uri']); $film->setStoryLine(trim($info['story_line'])); $genres = []; for($i = 0; $i < count($info['genres']); $i++){ $genres[] = $em->getRepository('AppBundle:Category')->findOneBy(['id' => $info['genres'][$i]]); } $film->setCategories($genres); $em->persist($film); } $em->flush(); return true; } 

Here is the essence of the film. The part that adds the category.

  /** * @ORM\ManyToMany(targetEntity="Category") * @ORM\JoinTable(name="films_categories", * joinColumns={@ORM\JoinColumn(name="film_id", referencedColumnName = "id")}, * inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}) */ private $categories; public function __construct(){ $this->categories = new ArrayCollection(); } /** * Set categories * * @return category */ public function setCategories($categories) { foreach($categories as $category) { $this->addCategory($category); } return $this; } public function addCategory(Category $category) { $this->categories->add($category); } 

Everything seems to work as it should but is not sure whether this is the best option. Also faced with the fact that when in the controller I try to get all the movies.

  $films = $em->getRepository('AppBundle:Film')->findAll(); dump($films);die; 

I would like to get all the categories to it. And instead I get:

 -categories: PersistentCollection {#488 β–Ό -snapshot: [] -owner: Film {#474} -association: array:19 [ …19] -em: EntityManager {#414 …11} -backRefFieldName: null -typeClass: ClassMetadata {#476 …} -isDirty: false #collection: ArrayCollection {#489 β–Ό -elements: [] } #initialized: false 

    You need to remove the type check in the addCategories method.

      public function addCategories($categories) 

    Or transfer to it an ArrayCollection and not an array, as required by type hinting.