私はSymfonyとDoctrineを使用しています。ManyToOne 1人の多数の投稿 - >投稿を作成して新しいユーザーを作成する
私は2つのエンティティ、ユーザーとポットを持っています。 ログに記録されたすべてのユーザーは、できるだけ多くの投稿を作成できます。 すべての投稿は1人のユーザーに関連付けられています。
それはワークフローです:私はユーザーとログインし、このユーザーは投稿を作成します。新しい投稿はデータベースに保存され、ユーザーの作成時に作成されます。
問題:ユーザーが投稿を作成すると投稿は作成されませんが、新しいユーザーも作成されます。新しい投稿は新しいユーザーに関連付けられ、ログインしたユーザーには関連付けられません。
ユーザエンティティ:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Post;
/**
* @ORM\Entity
* @ORM\Table(name= "User")
*/
class User
{
/**
* @ORM\Column(type = "integer")
* @ORM\Id
* @ORM\GeneratedValue("AUTO")
*/
private $id;
/**
* @ORM\Column(type = "string", length = 50)
*/
private $account;
/**
* @ORM\Column(type = "string", length = 22)
*/
private $password;
/**
* @ORM\Column(type = "string", length = 100)
*/
private $email;
/**
* @ORM\Column(type = "integer", length = 1)
*/
private $type;
/**
*
* @ORM\OneToMany(targetEntity="Post", mappedBy="user")
*/
private $posts;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set account
*
* @param string $account
*
* @return User
*/
public function setAccount($account)
{
$this->account = $account;
return $this;
}
/**
* Get account
*
* @return string
*/
public function getAccount()
{
return $this->account;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set mail
*
* @param string $mail
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get mail
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set type
*
* @param integer $type
*
* @return User
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return integer
*/
public function getType()
{
return $this->type;
}
/**
* Constructor
*/
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add post
*
* @param \AppBundle\Entity\Post $post
*
* @return User
*/
public function addPost(\AppBundle\Entity\Post $post)
{
$this->posts[] = $post;
return $this;
}
/**
* Remove post
*
* @param \AppBundle\Entity\Post $post
*/
public function removePost(\AppBundle\Entity\Post $post)
{
$this->posts->removeElement($post);
}
/**
* Get posts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
POST ENTITY:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\User;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name = "Post")
* @Vich\Uploadable
*/
class Post
{
/**
* @ORM\Column(type = "integer")
* @ORM\Id
* @ORM\GeneratedValue("AUTO")
*/
private $id;
/**
* @ORM\Column(type = "string", length = 25)
*/
private $title;
/**
* @ORM\Column(type = "string", length = 255)
*/
private $text;
/**
* @ORM\Column(type= "string", length = 250)
*/
private $pic;
/**
* @Vich\UploadableField(mapping="post_file", fileNameProperty="pic")
*
*/
private $picFile;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="posts", cascade={"persist"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
public function getPicFile(){
return $this->picFile;
}
public function setPicFile(File $picFile = null){
$this->picFile = $picFile;
return $this;
}
/**
* Set user
*
* @param \AppBundle\Entity\User $user
*
* @return Coach
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set title
*
* @param string $title
*
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set text
*
* @param string $text
*
* @return Post
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Set pic
*
* @param string $pic
*
* @return Post
*/
public function setPic($pic)
{
$this->pic = $pic;
return $this;
}
/**
* Get pic
*
* @return string
*/
public function getPic()
{
return $this->pic;
}
/**
* Set id
*
* @param integer $id
*
* @return Post
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
CONTROLLER: 注:ここではセッションからログインしているユーザーを取ることです。これは、私は使用するユーザーからのIDを出力し、それは正しいIDだった。
public function FrameCoachNewAction(Request $request)
{
$session = $request->getSession();
$session->start();
$user = $session->get('user');
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$post = $form->getData();
$post->setUser($user);
$doct = $this->getDoctrine()->getManager();
$doct->persist($post);
$doct->flush();
//return New Response($post->getUser()->getId());
return $this->RedirectToRoute('app_frame_coach');
}else{
return $this->render('/frame/frame_coach_new.html.twig', array('form' => $form->createView(), 'user' => $user));
}
}
'$ session = $ request-> getSession(); $ session-> start(); $ user = $ session-> get( 'user'); '$ user = $ this-> getUser()はあなたのsfアプリケーションに接続している現在のユーザを返します。この部分を除いてあなたのコードにエラーはありません – Mcsky
ユーザの属性投稿にカスケード= {"persist"}を移動しようとしました – Mocrates
あなたは$ this-> get( 'security.token_storage')を使用するべきです - > getToken() - > getUser()参照:https://symfony.com/doc/current/security.html – Mocrates