2016-04-19 82 views
1

保存表單數據在我的博客的形式我有場作者:的Symfony 3從實體沒有關聯

<?php 
$this->add(
    'user', 
    EntityType::class, 
    [ 
     'label'  => 'Author', 
     'class'  => 'Hitso\Bundle\CommonBundle\Entity\User', 
     'placeholder' => 'Select post author', 
     'mapped'  => false, 
     'empty_data' => null, 
    ] 
) 

但在我的博客實體我沒有與用戶表用戶協會,我有別的表 - '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('...'); 
    } 

但現在我文提交表單我有以下錯誤:

屬性用戶沒有在課堂上BlogBu​​ndle \實體\博客

請幫助存在!

回答

0

我認爲你需要做博客用戶

/** 
* @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; 
+0

的用戶屬性正確標註targetEntity不,這不是幫助。這只是我的錯誤。 –