问题描述
早上好,
我有两个链接的数据库,表分别是User
和Theme
,请记住,我对PHP和symfony框架并不熟悉。
主题链接到用户:
/src/Entity/Theme.PHP
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User",inversedBy="published")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
我正在尝试设置一个功能,该功能可以显示该用户根据其姓氏编写的所有主题,据我了解,@ORM\ManyToOne(targetEntity="App\Entity\User",inversedBy="published")
确保我的主题不仅由user_id
链接,但用户实体。
在我的ThemeController.PHP
中,我的功能是这样设置的:
/**
* @Route("/theme/show/{lastname}",name="theme_created_by")
* 0@param User $ThemeByUser
*/
public function userThemes(User $ThemeByUser){
$html = $this->twig->render('theme/index.html.twig',[
'themes' => $this->themeRepository->findBy(
['User' => $ThemeByUser],['created_at' => 'DESC']),]);
return new Response($html);
}
An exception occurred while executing 'SELECT t0.id AS id_1,t0.name AS name_2,t0.created_at AS created_at_3,t0.updated_at AS updated_at_4,t0.user_id AS user_id_5 FROM theme t0 WHERE t0.id = ?' with params ["Roland"]:
sqlSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input Syntax for type integer: "Roland"
这意味着Doctrine期望将int作为参数,但是它正在接收字符串。在阅读文档时,似乎已转换参数以匹配数据中的任何内容。也许我不太了解它是如何工作的,只需要一点指导即可。 谢谢
解决方法
不知道如何以及为什么,但是在我的树枝文件中呈现了该功能:
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<strong class="text-gray-dark">Autheur : </strong>
<a href="{{ path('theme_created_by',{'lastname': theme.user.lastname}) }}">
{{ theme.user.lastname }}
</a>
<br/>
<a href="{{ path( 'theme_show',{'id' : theme.id} ) }}">
<strong>{{ theme.name }}</strong><br/>
</a>
我将路径行替换为:
<a href="{{ path('theme_created_by',{'username': theme.user.username}) }}">
{{ theme.user.lastname }}
</a>
也更改了在我的路线中通过的参数:username
@Route("/themes/by/{username}",name="theme_created_by")
现在可以了。