-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add tags to Post #447
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
Closed
Closed
Add tags to Post #447
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| {% if not post.tags.empty %} | ||
| <p class="post-tags"> | ||
| {% for tag in post.tags %} | ||
| <span class="label label-success"> | ||
| <i class="fa fa-tag"></i> {{ tag.name }} | ||
| </span> | ||
| {% endfor %} | ||
| </p> | ||
| {% endif %} | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,9 @@ | |
|
|
||
| use AppBundle\Entity\Comment; | ||
| use AppBundle\Entity\Post; | ||
| use AppBundle\Entity\User; | ||
| use AppBundle\Entity\Tag; | ||
| use Doctrine\Common\DataFixtures\AbstractFixture; | ||
| use Doctrine\Common\DataFixtures\DependentFixtureInterface; | ||
| use Doctrine\Common\Persistence\ObjectManager; | ||
| use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerAwareTrait; | ||
|
|
@@ -31,8 +32,9 @@ | |
| * | ||
| * @author Ryan Weaver <[email protected]> | ||
| * @author Javier Eguiluz <[email protected]> | ||
| * @author Yonel Ceruto <[email protected]> | ||
| */ | ||
| class LoadFixtures extends AbstractFixture implements ContainerAwareInterface | ||
| class PostFixtures extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface | ||
| { | ||
| use ContainerAwareTrait; | ||
|
|
||
|
|
@@ -41,46 +43,23 @@ class LoadFixtures extends AbstractFixture implements ContainerAwareInterface | |
| */ | ||
| public function load(ObjectManager $manager) | ||
| { | ||
| $this->loadUsers($manager); | ||
| $this->loadPosts($manager); | ||
| } | ||
|
|
||
| private function loadUsers(ObjectManager $manager) | ||
| { | ||
| $passwordEncoder = $this->container->get('security.password_encoder'); | ||
|
|
||
| $johnUser = new User(); | ||
| $johnUser->setUsername('john_user'); | ||
| $johnUser->setEmail('[email protected]'); | ||
| $encodedPassword = $passwordEncoder->encodePassword($johnUser, 'kitten'); | ||
| $johnUser->setPassword($encodedPassword); | ||
| $manager->persist($johnUser); | ||
| $this->addReference('john-user', $johnUser); | ||
|
|
||
| $annaAdmin = new User(); | ||
| $annaAdmin->setUsername('anna_admin'); | ||
| $annaAdmin->setEmail('[email protected]'); | ||
| $annaAdmin->setRoles(['ROLE_ADMIN']); | ||
| $encodedPassword = $passwordEncoder->encodePassword($annaAdmin, 'kitten'); | ||
| $annaAdmin->setPassword($encodedPassword); | ||
| $manager->persist($annaAdmin); | ||
| $this->addReference('anna-admin', $annaAdmin); | ||
|
|
||
| $manager->flush(); | ||
| } | ||
| $phrases = $this->getPhrases(); | ||
| shuffle($phrases); | ||
|
|
||
| private function loadPosts(ObjectManager $manager) | ||
| { | ||
| foreach (range(1, 30) as $i) { | ||
| foreach ($phrases as $i => $title) { | ||
| $post = new Post(); | ||
|
|
||
| $post->setTitle($this->getRandomPostTitle()); | ||
| $post->setTitle($title); | ||
| $post->setSummary($this->getRandomPostSummary()); | ||
| $post->setSlug($this->container->get('slugger')->slugify($post->getTitle())); | ||
| $post->setContent($this->getPostContent()); | ||
| // This reference has been added in UserFixtures class and contains | ||
| // an instance of User entity. | ||
| $post->setAuthor($this->getReference('anna-admin')); | ||
| $post->setPublishedAt(new \DateTime('now - '.$i.'days')); | ||
|
|
||
| $this->addRandomTags($post); | ||
|
|
||
| foreach (range(1, 5) as $j) { | ||
| $comment = new Comment(); | ||
|
|
||
|
|
@@ -99,6 +78,34 @@ private function loadPosts(ObjectManager $manager) | |
| $manager->flush(); | ||
| } | ||
|
|
||
| /** | ||
| * This method must return an array of fixtures classes | ||
| * on which the implementing class depends on. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function getDependencies() | ||
| { | ||
| return [ | ||
| TagFixtures::class, | ||
| UserFixtures::class, | ||
| ]; | ||
| } | ||
|
|
||
| private function addRandomTags(Post $post) | ||
| { | ||
| if (0 === $count = mt_rand(0, 3)) { | ||
| return; | ||
| } | ||
|
|
||
| $indexes = (array) array_rand(TagFixtures::$names, $count); | ||
| foreach ($indexes as $index) { | ||
| /** @var Tag $tag */ | ||
| $tag = $this->getReference('tag-'.$index); | ||
| $post->addTag($tag); | ||
| } | ||
| } | ||
|
|
||
| private function getPostContent() | ||
| { | ||
| return <<<'MARKDOWN' | ||
|
|
@@ -157,16 +164,24 @@ private function getPhrases() | |
| 'Sed varius a risus eget aliquam', | ||
| 'Nunc viverra elit ac laoreet suscipit', | ||
| 'Pellentesque et sapien pulvinar consectetur', | ||
| 'Ubi est barbatus nix', | ||
| 'Abnobas sunt hilotaes de placidus vita', | ||
| 'Ubi est audax amicitia', | ||
| 'Eposs sunt solems de superbus fortis', | ||
| 'Vae humani generis', | ||
| 'Diatrias tolerare tanquam noster caesium', | ||
| 'Teres talis orgias saepe tractare de camerarius flavum sensorem', | ||
| 'Silva de secundus galatae demitto quadra', | ||
| 'Sunt accentores vitare salvus flavum parses', | ||
| 'Potus sensim ducunt ad ferox abnoba', | ||
| 'Sunt seculaes transferre talis camerarius fluctuies', | ||
| 'Era brevis ratione est', | ||
| 'Sunt torquises imitari velox mirabilis medicinaes', | ||
| 'Cum mineralis persuadere omnes finises desiderium bi-color', | ||
| 'Bassus fatalis classiss virtualiter transferre de flavum', | ||
| ]; | ||
| } | ||
|
|
||
| private function getRandomPostTitle() | ||
| { | ||
| $titles = $this->getPhrases(); | ||
|
|
||
| return $titles[array_rand($titles)]; | ||
| } | ||
|
|
||
| private function getRandomPostSummary($maxLength = 255) | ||
| { | ||
| $phrases = $this->getPhrases(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace AppBundle\DataFixtures\ORM; | ||
|
|
||
| use AppBundle\Entity\Tag; | ||
| use Doctrine\Common\DataFixtures\AbstractFixture; | ||
| use Doctrine\Common\Persistence\ObjectManager; | ||
|
|
||
| /** | ||
| * Defines the sample data to load in the database when running the unit and | ||
| * functional tests. | ||
| * | ||
| * Execute this command to load the data: | ||
| * | ||
| * $ php bin/console doctrine:fixtures:load | ||
| * | ||
| * See http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html | ||
| * | ||
| * @author Yonel Ceruto <[email protected]> | ||
| */ | ||
| class TagFixtures extends AbstractFixture | ||
| { | ||
| public static $names = [ | ||
| 'Lorem', | ||
| 'ipsum', | ||
| 'consectetur', | ||
| 'adipiscing', | ||
| 'incididunt', | ||
| 'labore', | ||
| 'voluptate', | ||
| 'dolore', | ||
| 'pariatur', | ||
| ]; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function load(ObjectManager $manager) | ||
| { | ||
| foreach (self::$names as $index => $name) { | ||
| $tag = new Tag(); | ||
| $tag->setName($name); | ||
|
|
||
| $manager->persist($tag); | ||
| $this->addReference('tag-'.$index, $tag); | ||
| } | ||
|
|
||
| $manager->flush(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added 15 phrases more to avoid duplicate post title and slug issue.