Nguyen Nhut Tien
Nguyen Nhut Tien

Reputation: 129

How to get object from database in Fixtures Symfony

I want to create fake posts with PostFixtures class . I use fake data with "title, slug, content, pushlish_at". But with "author" , i want use data from database (user table). I don't know how to get it. Please help me!

Here is my code :

<?php

namespace App\DataFixtures;

use Faker\Factory;
use App\Entity\Post;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;


class PostFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $faker = Factory::create();

        for ($i = 0; $i< 10; $i++) {
            $post = new Post();

            $post->setTitle($faker->title());
            $post->setSlug($faker->slug());
            $post->setContent($faker->paragraph());
            $post->setPublishAt($faker->dateTimeBetween('-100 days', '-1 days'));

            $userRepository = $manager->getRepository(User::class);
            $user = $userRepository->find(rand(1, 3));
            $post->setAuthor($user);

            $manager->persist($post);
        }

        $manager->flush();
    }

}

In database, I just have three users (id from 1 to 3). And I created ManyToOne relation between Post and User class. But I don't know why $user is null.

UPDATE

UserFixtures class:

<?php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserFixtures extends Fixture
{
    public const USER_REFERENCE = 'users';
    private $passwordEncoder;

    /**
     * UserFixtures constructor.
     */
    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }


    public function load(ObjectManager $manager)
    {
        $user1 = new User();
        $user1->setEmail('[email protected]');
        $user1->setPassword(
            $this->passwordEncoder->encodePassword(
                $user1,
                '123456'
            )
        );
        $user1->setRoles(['ROLE_ADMIN']);
        $manager->persist($user1);
        $manager->flush();
        dump($user1); // it still has value
        $this->addReference(self::USER_REFERENCE, $user1);
    }
}

PostFixtures class:

<?php

namespace App\DataFixtures;

use Faker\Factory;
use App\Entity\Post;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;

class PostFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $faker = Factory::create('en_US');

        $user =  $this->getReference(UserFixtures::USER_REFERENCE);
        dd($user);  // I want to check it: $user still has id . But 
                   // email,role and password are null

        for ($i = 0; $i < 10; $i++) {
            $post = new Post();
            $post->setTitle($faker->title());
            $post->setSlug($faker->slug());
            $post->setContent($faker->paragraph());
            $post->setPublishAt($faker->dateTimeBetween('-100 days', '-1 days'));
            $post->setAuthor($user);
            $manager->persist($post);
        }
        $manager->flush();
    }

    public function getDependencies()
    {
        return array(
            UserFixtures::class,
        );
    }

}

Upvotes: 2

Views: 2423

Answers (1)

Majesty
Majesty

Reputation: 1909

You are definitely looking for reference API which is defined under Doctrine\Common\DataFixtures\AbstractFixture namespace.

In your UserFixtures definition do this:

$user = new User();
$user->setName(...);

$manager->persist($user);
$this->addReference('user_john', $user);
...
$manager->flush();

And then in your PostFixtures

$post = new Post();
$post->setAuthor($this-getReference('user_john')); 

and don't forget to add dependencies, so Doctrine will know an exact order (upload users before articles)

public function getDependencies()
{
    return [UserFixtures::class];
}

This solution implies that you are loading users using fixtures as well, which is a recommended approach.

Upvotes: 1

Related Questions