Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

returning null from repo on fixtures, but method works on controller #294

Open
BernardA opened this issue Sep 5, 2019 · 5 comments
Open

Comments

@BernardA
Copy link

BernardA commented Sep 5, 2019

Not sure if I am going about this the proper way but, in any case, something seems to be amiss.
On fixtures I am attempting to access a random category entity to add to an Ad entity. First I attempted to do that directly on fixtures, but as it did not work I put the method on a regular controller and injected the controller as service on fixtures just to check it.

While the method works as expected on the controller, it returns NULL on fixtures.

The Category entity is a GEDMO tree.

This is what I believe to be the relevant portions of:

AppFixtures

  .....
 use App\Controller\DefaultController;
  .........

 private $em;
private $defaultController;

public function __construct(
    UserPasswordEncoderInterface $passwordEncoder,
    TokenGenerator $tokenGenerator,
    EntityManagerInterface $entityManager,
    DefaultController $defaultController
){
    $this->passwordEncoder = $passwordEncoder;
    $this->faker = \Faker\Factory::create();
    $this->tokenGenerator = $tokenGenerator;
    $this->em = $entityManager;
    $this->defaultController = $defaultController;
}
     /**
 * Load data fixtures with the passed EntityManager
 * @param ObjectManager $manager
 */
public function load(ObjectManager $manager)
{
    $this->loadUsers($manager);
    $this->loadAds($manager);
}

  .......
   private const CATEGORIES = [
    'house cleaning',
    'garden cleaning',
    'sports coach',
    'tax return',
    'english teacher',
    'spanish teacher',
    'french teacher',
    'chinese teacher',
    'car pooling'
  ];
  
...........

 public function loadAds(ObjectManager $manager)
  {
    for ($i = 0; $i < 100; $i++) {
        $ad = new Ad();
        $ad->setDescription($this->faker->realText(100));
        $ad->setCreatedAt($this->faker->dateTimeThisYear);

        $userReference = $this->getRandomUserReference($ad);
        $ad->setUser($userReference);
        
        $title = self::CATEGORIES[array_rand(self::CATEGORIES)];
var_dump($title); // outputs a random category title
        $category = $this->defaultController->getRandomCategory($title);
var_dump($category->getTitle()); // outputs error Call to a member function on null.
        $ad->setCategory($category); // expects entity

        $ad->setIsActive(true);

        $this->setReference("ad_$i", $ad);

        $manager->persist($ad);
    }
    $manager->flush();
}
..........

On App\DefaultController things works as expected an it outputs the JSON with the
correct info.

public function index()
{
    $title = self::CATEGORIES[array_rand(self::CATEGORIES)];
    $cat = $this->getRandomCategory($title);
   // outputs something like 
               {
                  "categoryId":25,
                  "categoryTitle":"garden cleaning"
              }
    return $this->json(array(
        'categoryId' => $cat->getId(),
        'categoryTitle' => $cat->getTitle()
    ));  
}

public function getRandomCategory($title)
{
    $this->repo = $this->em->getRepository(Category::class);
    return $this->repo->findOneByTitle($title);
}
@SenseException
Copy link
Member

Could you please put the fixture class in here where null happens? Also please check if there is something in your configuration that might inject a different ObjectManager instance.

@BernardA
Copy link
Author

BernardA commented Sep 10, 2019

Thanks but not sure I understand your comment. What do you mean by put the fixture class in here where null happens? Here where?
Note that the getRandomCategory method was initially in App\Fixtures. I just moved it out to within a regular controller to make sure it was working, which it was and is ( on the controller, that is)

Also note that in the section below from App\Fixtures, if I comment out $this->loadAds($manager);, it will generate the users in the db - $this->loadUsers($manager); . So the App\Fixture as such is working.

 /**
 * Load data fixtures with the passed EntityManager
 * @param ObjectManager $manager
 */
public function load(ObjectManager $manager)
{
    $this->loadUsers($manager);
    $this->loadAds($manager); 

The issue remains why can't I fetch the random Category entity from within App\Fixture.

To be clear, the Category table is pre populated in the database.

@BernardA
Copy link
Author

As I said above, the table Category was pre populated in the db. And as you know Fixtures is supposed to purge the db before generating new data.

I had previously thought that that was causing the error, as the table Category was also purged and when getRandomCategory run it found an empty table.

But I checked on PhpMyAdmin and the table was still there after running fixtures and DefaultController portion was working even after clearing the cache.

Also, another table migration_versions was not purged either.

As it happens, when I came back to this today, I found that DefaultController was throwing the same error as Fixtures, ie, Call to member function getId() on null and that the table Category was empty.

So, there seems to be a discrepancy in-between what I was seeing on PhpMyAdmin and what Fixtures was getting.

Bottom line is that the error is due to the fact that, for Fixtures, the Category table was empty, while, for some reason, DefaultController and PhpMyAdmin were still finding data there. But the data was eventually purged from Category.

The issue will become how to exclude Category table from purge. And I know you have this PR in process.

Otherwise I will need to see how to have Fixtures also populate Category table, which does not look trivial given the specific methodology of Gedmo trees. I may get back to you on that.

@SenseException
Copy link
Member

Thanks but not sure I understand your comment. What do you mean by put the fixture class in here where null happens? Here where?

I was asking about the full fixture without any cut code. Or maybe a smaller example were return null can be reproduced. If this is a bug in the fixture bundle, this should be possible to reproduce without the injected dependencies, but as you mentioned before the reason of your problems might be connected with data purge.

@BernardA
Copy link
Author

Thanks. I do believe the issue is with the purge, so no reason to pursue this here.
On the other hand I was wondering when can one expect to have the PR 289 'Allow the exclusion of certain tables when purging #289` merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants