为什么我手动更新的内容在 Symfony/Doctrine 中没有显示为最新?

问题描述 投票:0回答:1

在我的 Symfony Web 应用程序(版本 7)中,我正在获取数据库中托管的信息。

我直接在数据库中更新了一篇文章,在控制台中运行此查询:

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

但是当我导航到相应的 URL 时,其内容没有更新。

这是我的控制器,问题出现在方法上:

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]);
    }
}

这是我的实体:

<?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;
    }

}

如果我在控制器中添加

dd($post);
,我可以在屏幕上看到:

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: ""

如果我在控制台中执行此查询:

mysql > SELECT post_content FROM xyz_posts WHERE ID=56;  

我可以看到:

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

如您所见,数据库表中的内容已更新,但在控制器中仍然显示旧内容。

我已尝试以下命令来清除 Doctrine 缓存:

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

为了清除应用程序缓存,我也尝试过:

rm -rf var/cache/*

和:

bin/console cache:clear --env=prod

我也尝试过这个:

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

结果如下:

 // 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                     

但是问题依然存在。

我需要做什么才能看到我的更新内容?

php symfony doctrine-orm
1个回答
0
投票

更新

posts_xyz
- 在你的sql中你更新了名为“posts_xyz”的表,但是在你的实体中你有不同的表名#[ORM\Table(name:"xyz_posts")] 更新:我有点困惑,因为在选择查询中你有“xyz_posts”表的名称。

© www.soinside.com 2019 - 2024. All rights reserved.