Why is my manually updated content not appearing up to date in Symfony/Doctrine?

45 Views Asked by At

In my Symfony web application (version 7), I am taking information hosted in a database.

I have updated a post directly in the database, running this query in console:

UPDATE `posts_xyz` 
    SET `post_content`='<p>New content ...</p>' 
WHERE ID=56; 

But its content is not updated when I navigate to the corresponding URL.

This is my controller, the problem occurs in the method: showByPostName()

<?php

namespace App\Controller;

use App\Entity\Post;
use App\Repository\PostRepository;
use App\Entity\Subscribe;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class HomeController extends AbstractController
{
    #[Route('/', name: 'homepage')]
    public function index(PostRepository $postRepository): Response
    {
        return $this->render('home/index.html.twig');
    }

    #[Route('/{post_name}', name: 'post_name')]
    public function showByPostName(string $post_name, PostRepository $postRepository): Response
    {
        $post = $postRepository->findOneBy(['post_name' => $post_name, 'post_status'=>'publish', 'post_type'=>'page']);
        if (!$post) {
            throw $this->createNotFoundException(
                'No encontrado '.$post_name
            );
        }
         return $this->render('post/index.html.twig', ['post' => $post]);
    }
}

This is my Entity:

<?php

namespace App\Entity;

use App\Repository\WpDcjqPostsRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ORM\Table(name:"xyz_posts")]

class Post
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $post_content = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $post_title = null;

    #[ORM\Column(length: 200)]
    private ?string $post_name = null;

    #[ORM\Column(length: 20)]
    private ?string $post_status = null;

    #[ORM\Column(length: 20)]
    private ?string $post_type = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $post_excerpt = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setID(int $id): self
    {
        $this->id = $id;
        return $this;
    }

    public function getPostContent(): ?string
    {
        return $this->post_content;
    }

    public function setPostContent(string $post_content): self
    {
        $this->post_content = $post_content;
        return $this;
    }

    public function getPostTitle(): ?string
    {
        return $this->post_title;
    }

    public function setPostExcerpt(string $post_excerpt): self
    {
        $this->post_excerpt = $post_excerpt;
        return $this;
    }

    public function setPostTitle(string $post_title): self
    {
        $this->post_title = $post_title;
        return $this;
    }

    public function getPostName(): ?string
    {
        return $this->post_name;
    }

    public function getPostExcerpt(): ?string
    {
        return $this->post_excerpt;
    }

    public function setPostName(string $post_name): self
    {
        $this->post_name = $post_name;
        return $this;
    }

}

If I add dd($post); in the controller, I can see on screen:

HomeController.php on line 54:
App\Entity\Post {#699 ▼
  -id: 56
  -post_content: "<p>Old content ...</p>"
  -post_title: "***"
  -post_name: "***"
  -post_status: "publish"
  -post_type: "page"
  -post_excerpt: ""

If I execute this query in the console:

mysql > SELECT post_content FROM xyz_posts WHERE ID=56;  

I can see:

post_content
-------------------------
<p>New content ... </p>  

As you can see, the content is updated in the database table, but in the controller it is still showing the old content.

I have tried the following commands, to clear the Doctrine cache:

bin/console doctrine:cache:clear-query --env=prod
bin/console doctrine:cache:clear-result --env=prod
bin/console doctrine:cache:clear-metadata --env=prod

To clear the app cache, I have also tried:

rm -rf var/cache/*

and:

bin/console cache:clear --env=prod

I've also tried this:

php bin/console cache:pool:clear --all

With the following result:

 // Clearing all cache pools...                                                 

 // Clearing cache pool: cache.app                                              

 // Clearing cache pool: cache.system                                           

 // Clearing cache pool: cache.validator                                        

 // Clearing cache pool: cache.serializer                                       

 // Clearing cache pool: cache.property_info                                    

 // Clearing cache pool: cache.asset_mapper                                     

 // Clearing cache pool: cache.messenger.restart_workers_signal                 

 // Clearing cache pool: cache.validator_expression_language                    

 // Clearing cache pool: cache.doctrine.orm.default.result                      

 // Clearing cache pool: cache.doctrine.orm.default.query                       

 // Clearing cache pool: cache.security_expression_language                     

 // Clearing cache pool:                                                        
 // cache.security_is_granted_attribute_expression_language                     

But the problem persists.

What do I need to do to see my updated content?

1

There are 1 best solutions below

0
Radomyr Kovsher On

UPDATE posts_xyz - In your sql you update table with name "posts_xyz", but in your entity you have different table name #[ORM\Table(name:"xyz_posts")] Upd: I am little bit confused because in select query you have "xyz_posts" name of the table.