I have two enity user
and post
. I am trying to dump dummy data to post table using fixtures. Now, problem is that I could not insert data to author
column which holds ManyToOne relation with User entity.
I tried to insert dummy data by passing id directly as follows:
for($i=0;$i<20;$i++)
{
$post = new Post();
$post->setTitle($this->faker->realText(20));
$post->setContent($this->faker->text);
$post->setIsPublished(true);
$post->setAuthor('1');
$post->setPublishedAt(new \DateTime());
$post->setCreatedAt(new \DateTime());
$post->setUpdatedAt(new \DateTime());
$manager->persist($post);
$manager->flush();
}
But, it is generating error:
Argument 1 passed to App\Entity\Post::setAuthor() must be an instance of App\Entity\User or null, string given
Is there any way to pick one of user from User entity and insert it into the column randomly.
Update:
After looking at this documentation, I am able to provide reference
UserFixtures:
public const ADMIN_USER_REFERENCE = 'admin-user';
public function load(ObjectManager $manager)
{
......
$this->addReference(self::ADMIN_USER_REFERENCE, $userAdmin);
}
PostFixtures:
$post->setAuthor($this->getReference(UserFixtures::ADMIN_USER_REFERENCE ));
Following, this way it is setting all the posts to the same author. However, I am looking to insert different author randomly throughout different posts.