“Symfony Datafixtures” Kode Jawaban

Fixtures Symfony

php bin/console doctrine:fixtures:load
Hakamate

Symfony Datafixtures

<?php

namespace App\DataFixtures;

use App\Entity\Article;
use App\Entity\Category;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Faker;

class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        $faker = Faker\Factory::create('fr-FR');
        for ($count = 0; $count < 10; $count++) {
            // Category
            $category = new Category();
            $category->setName($faker->word());
            $manager->persist($category);
    
            // Article
            $article = new Article();
            $article->setTitle($faker->word());
            $article->setDate($faker->dateTime());
            $article->setVisible($faker->boolean());
            $article->setContent($faker->paragraph());
            $article->setIdCat($category);
            $manager->persist($article);

        }

        $manager->flush();
    }
}
Testy Termite

Jawaban yang mirip dengan “Symfony Datafixtures”

Pertanyaan yang mirip dengan “Symfony Datafixtures”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya