在我的博客表单中,我有一个包含作者的字段:
<?php
$this->add(
'user',
EntityType::class,
[
'label' => 'Author',
'class' => 'HitsoBundleCommonBundleEntityUser',
'placeholder' => 'Select post author',
'mapped' => false,
'empty_data' => null,
]
)
但在我的博客实体中,我没有用户与用户表的关联,我在其他表中有这个-"blogs_users":
<?php
/**
* Blog
*
* @ORMTable(name="blogs")
* @ORMEntity(repositoryClass="RepositoryBlogRepository")
* @codeCoverageIgnore
*/
class Blog
{
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer", length=10, options={"unsigned"=true})
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $title;
/**
* @ORMColumn(type="string", length=255)
* @GedmoSlug(fields={"title"})
*/
private $slug;
}
我的BlogUser实体类:
<?php
/**
* BlogUsers
*
* @ORMTable(name="blogs_users")
* @ORMEntity(repositoryClass="BlogBundleRepositoryBlogUserRepository")
* @codeCoverageIgnore
*/
class BlogUser implements SiteIdAwareInterface
{
/**
* @ORMId
* @ORMManyToOne(targetEntity="BlogBundleEntityBlog")
* @ORMJoinColumn(onDelete="CASCADE")
* @var HitsoBundleBlogBundleEntityBlog
*/
private $blog;
/**
* @ORMId
* @ORMManyToOne(targetEntity="BlogBundleEntityUser")
* @ORMJoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* @var CommonBundleEntityUser
*/
private $user;
}
和我的博客控制器::newAction:
<?php
public function newAction(Request $request)
{
$blog = new Blog();
$form = $this->createForm(
BlogType::class,
$blog
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$blogUser = new BlogUser();
$em->persist($blogUser);
$em->persist($blog);
$em->flush();
return $this->redirectToRoute('...');
}
但现在我提交的表格我有以下错误:
属性用户不存在于BlogBundle\Entity\Blog类中
救命!
我认为你需要在博客用户的用户属性上做正确的targetEntity注释
/**
* @ORMId
* @ORMManyToOne(targetEntity="CommonBundleEntityUser")
* @ORMJoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* @var CommonBundleEntityUser
*/
private $user;