PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to fetch entities in db while using doctrine fixtures?

 May 12, 2022     doctrine, doctrine-orm, php, symfony     No comments   

Issue

I have been the ContainerAwareInterface on my LoadAdvert class so I can call and use the EntityManager to retrieve users in the database.

However, when executing php bin/console doctrine:fixtures:load, the entity manager retrieves only an empty array, as if there was no user in the db.

<?php
// src/OC/PlatformBundle/DataFixtures/ORM/LoadCategory.php

namespace OC\PlatformBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use OC\PlatformBundle\Entity\Advert;
use OC\UserBundle\Entity\User;

   class LoadAdvert implements FixtureInterface, ContainerAwareInterface
    {
      /**
       * @var ContainerInterface
       */
      private $container;

      public function setContainer(ContainerInterface $container = null)
      {
        $this->container = $container;
      }

      // Dans l'argument de la méthode load, l'objet $manager est l'EntityManager
      public function load(ObjectManager $manager)
      {
        $em = $this->container->get('doctrine.orm.entity_manager');
        $author = $em->getRepository('OCUserBundle:User')->findAll();

        die(var_dump($author));

Solution

Here it is how I do it. The main thing is to set reference for user objects that you create and then call it in other fixture file and assign them to related objects. Also its important the order of execution of fixture data. You cant load related object before you load users.

namespace AcmeBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    /** @var  ContainerInterface */
    private $container;

    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager)
    {
        $userManager = $this->container->get('fos_user.user_manager');

        $user1 = $userManager->createUser();
        $user1->setUsername('username1');
        $user1->setPlainPassword('123');
        $user1->setEmail('example1@gmail.com');
        $user1->setEnabled(true);


        $user2 = $userManager->createUser();
        $user2->setUsername('username2');
        $user2->setPlainPassword('123');
        $user1->setEmail('example2@gmail.com');
        $user2->setEnabled(true);

        $manager->persist($user1);
        $manager->persist($user2);

        $this->setReference('user1', $user1);
        $this->setReference('user2', $user2);

        $manager->flush();
    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    public function getOrder()
    {
        return 1;
    }

    /**
     * Sets the Container. Need it to create new User from fos_user.user_manager service
     *
     * @param ContainerInterface|null $container A ContainerInterface instance or null
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
}

Then in other fixture file you dont call user as you did from database, you do it like this:

 $author = $this->getReference('user1');

 $article = new Article();
 $article->addAuthor($author);

and then you add this $author object to related object.

Keep in mind to implement OrderedFixtureInterface in this article fixture and set getOrder() to be higher number than the one for users.



Answered By - Lord Zed
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing