Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more posts authors #478

Merged
merged 4 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/AppBundle/DataFixtures/ORM/PostFixtures.php
Expand Up @@ -49,11 +49,13 @@ public function load(ObjectManager $manager)
$post->setSummary($this->getRandomPostSummary());
$post->setSlug($this->container->get('slugger')->slugify($post->getTitle()));
$post->setContent($this->getPostContent());
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));

// Ensure that the first post is written by Jane Doe to simplify tests
// "References" are the way to share objects between fixtures defined
// in different files. This reference has been added in the UserFixtures
// file and it contains an instance of the User entity.
$post->setAuthor($this->getReference('jane-admin'));
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
$post->setAuthor(0 === $i ? $this->getReference('jane-admin') : $this->getRandomUser());

// for aesthetic reasons, the first blog post always has 2 tags
foreach ($this->getRandomTags($i > 0 ? mt_rand(0, 3) : 2) as $tag) {
Expand Down Expand Up @@ -93,6 +95,14 @@ public function getDependencies()
];
}

private function getRandomUser()
{
$admins = ['jane-admin', 'tom-admin'];
$index = array_rand($admins);

return $this->getReference($admins[$index]);
}

private function getRandomTags($numTags = 0)
{
$tags = [];
Expand Down
10 changes: 10 additions & 0 deletions src/AppBundle/DataFixtures/ORM/UserFixtures.php
Expand Up @@ -53,6 +53,16 @@ public function load(ObjectManager $manager)
// See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
$this->addReference('jane-admin', $janeAdmin);

$tomAdmin = new User();
$tomAdmin->setFullName('Tom Doe');
$tomAdmin->setUsername('tom_admin');
$tomAdmin->setEmail('tom_admin@symfony.com');
$tomAdmin->setRoles(['ROLE_ADMIN']);
$encodedPassword = $passwordEncoder->encodePassword($tomAdmin, 'kitten');
$tomAdmin->setPassword($encodedPassword);
$manager->persist($tomAdmin);
$this->addReference('tom-admin', $tomAdmin);

$johnUser = new User();
$johnUser->setFullName('John Doe');
$johnUser->setUsername('john_user');
Expand Down
3 changes: 2 additions & 1 deletion src/AppBundle/Repository/PostRepository.php
Expand Up @@ -38,8 +38,9 @@ public function findLatest($page = 1)
{
$query = $this->getEntityManager()
->createQuery('
SELECT p, t
SELECT p, a, t
FROM AppBundle:Post p
JOIN p.author a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this shouldn't improved nothing, Doctrine hydrates the author without an extra query.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yceruto, I think you're wrong. See http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#joins

A join (be it an inner or outer join) becomes a “fetch join” as soon as fields of the joined entity appear in the SELECT part of the DQL query outside of an aggregate function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think it's necessary for multiple users then 👍

LEFT JOIN p.tags t
WHERE p.publishedAt <= :now
ORDER BY p.publishedAt DESC
Expand Down
6 changes: 3 additions & 3 deletions tests/AppBundle/Controller/Admin/BlogControllerTest.php
Expand Up @@ -67,9 +67,9 @@ public function testAdminBackendHomePage()
$crawler = $client->request('GET', '/en/admin/post/');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());

$this->assertCount(
30,
$crawler->filter('body#admin_post_index #main tbody tr'),
$this->assertGreaterThanOrEqual(
1,
$crawler->filter('body#admin_post_index #main tbody tr')->count(),
'The backend homepage displays all the available posts.'
);
}
Expand Down
Binary file modified var/data/blog.sqlite
Binary file not shown.