在我的博客表格中,我与作者合作:
<?php
$this->add(
'user',
EntityType::class,
[
'label' => 'Author',
'class' => 'Hitso\Bundle\CommonBundle\Entity\User',
'placeholder' => 'Select post author',
'mapped' => false,
'empty_data' => null,
]
)
但是在我的Blog实体中我没有用户关联用户表,我在其他表中有这个 - 'blogs_users':
<?php
/**
* Blog
*
* @ORM\Table(name="blogs")
* @ORM\Entity(repositoryClass="Repository\BlogRepository")
* @codeCoverageIgnore
*/
class Blog
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", length=10, options={"unsigned"=true})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
* @Gedmo\Slug(fields={"title"})
*/
private $slug;
}
我的BlogUser实体类:
<?php
/**
* BlogUsers
*
* @ORM\Table(name="blogs_users")
* @ORM\Entity(repositoryClass="BlogBundle\Repository\BlogUserRepository")
* @codeCoverageIgnore
*/
class BlogUser implements SiteIdAwareInterface
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="BlogBundle\Entity\Blog")
* @ORM\JoinColumn(onDelete="CASCADE")
* @var \Hitso\Bundle\BlogBundle\Entity\Blog
*/
private $blog;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="BlogBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* @var CommonBundle\Entity\User
*/
private $user;
}
我的BlogController :: 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
中不存在属性用户请帮忙!
答案 0 :(得分:0)
我认为您需要对博客用户
的用户属性进行正确的注释targetEntity /**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="CommonBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* @var CommonBundle\Entity\User
*/
private $user;