瀏覽代碼

ORM: multi-class fixtures with dependency-based ordering.

Frederic G. MARAND 7 年之前
父節點
當前提交
afc59ff6c4
共有 3 個文件被更改,包括 79 次插入38 次删除
  1. 0 38
      src/DataFixtures/CatalogFixture.php
  2. 41 0
      src/DataFixtures/ProductFixture.php
  3. 38 0
      src/DataFixtures/ZCategoryFixture.php

+ 0 - 38
src/DataFixtures/CatalogFixture.php

@@ -1,38 +0,0 @@
-<?php
-
-namespace App\DataFixtures;
-
-use App\Entity\Category;
-use App\Entity\Product;
-use Doctrine\Bundle\FixturesBundle\Fixture;
-use Doctrine\Common\Persistence\ObjectManager;
-
-class CatalogFixture extends Fixture {
-
-  /**
-   * Load data fixtures with the passed EntityManager
-   *
-   * @param ObjectManager $manager
-   */
-  public function load(ObjectManager $manager) {
-    $nCat = 3;
-
-    $cats = [];
-    for ($i = 0; $i < $nCat; $i++) {
-      $cat = new Category();
-      $cat->setName("category{$i}");
-      $manager->persist($cat);
-      $cats[$i] = $cat;
-    }
-
-    for ($i = 0; $i < 7 * $nCat; $i++) {
-      $prod = new Product();
-      $prod->setName("product{$i}");
-      $prod->setPrice(mt_rand(100 * $i, 100 * ($i + 1)) / 100);
-      $prod->setCategory($cats[$i % $nCat]);
-      $manager->persist($prod);
-    }
-
-    $manager->flush();
-  }
-}

+ 41 - 0
src/DataFixtures/ProductFixture.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\DataFixtures;
+
+use App\Entity\Product;
+use Doctrine\Bundle\FixturesBundle\Fixture;
+use Doctrine\Common\DataFixtures\DependentFixtureInterface;
+use Doctrine\Common\Persistence\ObjectManager;
+
+class ProductFixture extends Fixture implements DependentFixtureInterface {
+
+  /**
+   * Load data fixtures with the passed EntityManager
+   *
+   * @param ObjectManager $manager
+   */
+  public function load(ObjectManager $manager) {
+    for ($i = 0; $i < 7 * ZCategoryFixture::NCAT; $i++) {
+      $prod = new Product();
+      $prod->setName("product-{$i}");
+      $prod->setPrice(mt_rand(100 * $i, 100 * ($i + 1)) / 100);
+      $catId = "category-" . $i % ZCategoryFixture::NCAT;
+      $catReference = $this->getReference($catId);
+      $prod->setCategory($catReference);
+      $manager->persist($prod);
+    }
+
+    $manager->flush();
+  }
+
+  /**
+   * Override the alphabetical ordering of fixtures loading.
+   *
+   * @return array
+   */
+  public function getDependencies() {
+    return [
+      ZCategoryFixture::class,
+    ];
+  }
+}

+ 38 - 0
src/DataFixtures/ZCategoryFixture.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\DataFixtures;
+
+use App\Entity\Category;
+use Doctrine\Bundle\FixturesBundle\Fixture;
+use Doctrine\Common\Persistence\ObjectManager;
+
+/**
+ * Class ZCategoryFixture is an out-of-alphabetical-order CategoryFixture
+ *
+ * @see \App\DataFixtures\ProductFixture::getDependencies()
+ *
+ * @package App\DataFixtures
+ */
+class ZCategoryFixture extends Fixture {
+  const NCAT = 3;
+
+  /**
+   * Load data fixtures with the passed EntityManager.
+   *
+   * @param \Doctrine\Common\Persistence\ObjectManager $manager
+   *
+   * @throws \Doctrine\Common\DataFixtures\BadMethodCallException
+   */
+  public function load(ObjectManager $manager) {
+    for ($i = 0; $i < self::NCAT; $i++) {
+      $cat = new Category();
+      $id = "category-{$i}";
+      $cat->setName($id);
+      $manager->persist($cat);
+      $this->addReference($id, $cat);
+    }
+
+    $manager->flush();
+  }
+
+}