1

I've created a fixtures class inside my bundle's DataFixtures/ORM folder. The actual class looks like this:

use Doctrine\Common\DataFixtures\FixtureInterface,
    Company\ShoppingBundle\Entity\Category;

class CategoryFixtures implements FixtureInterface
{
    public function load($em)
    {
        $category1 = new Category()->setName("category1");
        $category2 = new Category()->setName("category2");
        $em->persist($category1);
        $em->persist($category2);
        $em->flush();
    }
}

I'm not using the AbstractFixture base class, since I don't need references. I've also tried specifying the fixtures path when running the doctrine:fixtures:load console command. I'm following the official docs here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eno
  • 10,730
  • 18
  • 53
  • 86
  • 2
    What's the error you are getting that doesn't allow you to load the fixture? At an extreme guess its because you've done `$category1 = new Category()->setName("category1");` so unless the function `setName` returns a `Category` (most likely `$this` for your code to make sense) `$category1` will be getting assigned to null, you then try to persist null and flush it. – Kasheen Nov 17 '11 at 19:24
  • You're right about the incorrect assumption about what the setters return. I have since changed my code to call the setter after creating the object first. But this doesn't help me with the main problem. The error Im seeing is something like: [InvalidArgumentException] Could not find any fixtures to load in: Followed by the list of folders its looking in (the list includes the folder where my fixtures class is :-) – Eno Nov 17 '11 at 20:21
  • In your code paste above you've not got a namespace on the fixture class, was that just an omission in your question? If not try to add it into `namespace Acme\WhateverBundle\DataFixtures\ORM;` – Kasheen Nov 17 '11 at 20:54
  • also, make sure the bundle is initialized in your `AppKernel.php` – JamesHalsall Nov 18 '11 at 13:03
  • Yes, I did have a namespace and missed it when I copy and pasted that code. And yes, the bundle is initialized in AppKernel too. Weird thing today: I updated to Symfony 2.0.5 and then later on retried my load and it worked! Perhaps I had to clear out cache (I thought I had already done that). Also switched to using the AbstractFixture class and OrderedFixtureInterface. Wish I knew what "fixed" it :-) – Eno Nov 18 '11 at 19:42
  • AH, figured it out. My class did not have – Eno Nov 19 '11 at 00:58
  • I resolved the same problem just now try this https://stackoverflow.com/a/47623728/1093932 :) – zatamine Dec 03 '17 at 22:01

1 Answers1

3

Nothing wrong with the file naming, or my configuration: I simply forgot to put

<?php

at the top of my file :-D

Eno
  • 10,730
  • 18
  • 53
  • 86