Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/AppBundle/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace AppBundle\Controller\Admin;

use AppBundle\Entity\Post;
use AppBundle\Entity\Tag;
use AppBundle\Form\PostType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Expand Down Expand Up @@ -146,7 +147,10 @@ public function editAction(Post $post, Request $request)

if ($form->isSubmitted() && $form->isValid()) {
$post->setSlug($this->get('slugger')->slugify($post->getTitle()));
$entityManager->flush();
$entityManager->transactional(function ($entityManager) {
Copy link
Member

Choose a reason for hiding this comment

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

I appreciate your contribution ... but using $entityManager->transactional is "light years" ahead of what we want to teach in the Symfony Demo app. I'm sorry but we can't use this obscure feature. We'll need to find a much simpler alternative solution. Thanks for understanding it.

$entityManager->flush();
$this->getDoctrine()->getRepository(Tag::class)->cleanUnusedTags();
});

$this->addFlash('success', 'post.updated_successfully');

Expand Down Expand Up @@ -176,14 +180,14 @@ public function deleteAction(Request $request, Post $post)
}

$entityManager = $this->getDoctrine()->getManager();

// Delete the tags associated with this blog post. This is done automatically
// by Doctrine, except for SQLite (the database used in this application)
// because foreign key support is not enabled by default in SQLite
$post->getTags()->clear();

$entityManager->remove($post);
$entityManager->flush();
$entityManager->transactional(function ($entityManager) use ($post) {
// Delete the tags associated with this blog post. This is done automatically
// by Doctrine, except for SQLite (the database used in this application)
// because foreign key support is not enabled by default in SQLite
$post->getTags()->clear();
$entityManager->remove($post);
$this->getDoctrine()->getRepository(Tag::class)->cleanUnusedTags();
});

$this->addFlash('success', 'post.deleted_successfully');

Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository")
* @ORM\Table(name="symfony_demo_tag")
*
* Defines the properties of the Tag entity to represent the post tags.
Expand Down
42 changes: 42 additions & 0 deletions src/AppBundle/Repository/TagRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Repository;

use AppBundle\Entity\Post;
use AppBundle\Entity\Tag;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;

/**
* This custom Doctrine repository contains some methods to about tags.
*
* See http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes
*/
class TagRepository extends EntityRepository
{
/**
* Remove unused Tag from the Database.
*/
public function cleanUnusedTags()
{
$joinTable = $this->getEntityManager()->getClassMetadata(Post::class)->getAssociationMapping('tags')['joinTable']['name'];
$tagTable = $this->getClassMetadata()->getTableName();
$unused_tags = $this->getEntityManager()->createNativeQuery("
DELETE FROM `$tagTable` WHERE id IN
Copy link
Contributor

Choose a reason for hiding this comment

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

@Grafikart , try to use:

DELETE FROM tags t
LEFT JOIN tags_posts j ON j.tag_id = t.id
WHERE j.tag_id IS NULL

(
SELECT t.id FROM `$tagTable` t
LEFT JOIN `$joinTable` j ON j.tag_id = t.id
WHERE j.tag_id IS NULL
)
", new ResultSetMapping())->execute();
}
}