2016-10-25 4 views
2

Symfony에서 일대 다 관계를 맺고 있습니다. 그래서 여러 사용자를위한 이벤트를 만들기위한 자신 만의 교대 관리를 구축하고 있습니다.사용자 ID가 데이터베이스에 저장되지 않습니다. null

메신저가 데이터베이스에 이벤트를 저장하는 중 im은 user_id가 null이고 로그인 한 사용자가 저장하지 않으므로 여러 사용자가 이벤트를 저장하고 싶습니다.

그래서 이벤트를 저장하려는 사용자의 user_id를 저장하지 않을 것입니다. var_dump에 해당 사용자의 사용자 ID를 얻고 있지만 내 데이터베이스에 null이 저장되는 시점을 저장합니다. 누구든지 그 문제를 해결하는 방법을 알고 그 문제는 무엇입니까?

클래스 사용자 :

class User implements AdvancedUserInterface, \Serializable 
{ 
/** 
* @ORM\OneToMany(targetEntity="App\ShiftBundle\Entity\Shift", mappedBy="users", cascade={"all"}) 
*/ 
private $shifts; 


    public function __construct() 
    { 
     $this->isActive = true; 
     $this->isBlocked = false; 

     $this->shifts = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 


    /** 
* Add shifts 
* 
* @param \App\ShiftBundle\Entity\Shift $shifts 
* @return User 
*/ 
public function addShift(\App\ShiftBundle\Entity\Shift $shifts) 
{ 
    $this->shifts[] = $shifts; 
    $shifts->setUsers($this); 
    return $this; 
} 

/** 
* Remove shifts 
* 
* @param \App\ShiftBundle\Entity\Shift $shifts 
*/ 
public function removeShift(\App\ShiftBundle\Entity\Shift $shifts) 
{ 
    $this->shifts->removeElement($shifts); 
} 

/** 
* Get shifts 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getShifts() 
{ 
    return $this->shifts; 
} 

} 

클래스 이동 당신은 엔티티 ID를 할당해서는 안됩니다

public function createShiftTimeAction(Request $request) 
{ 
     $content = $request->getContent(); 
     $json = json_decode($content); 

     $repository = $this->getDoctrine()->getRepository('AppShiftBundle:Shift'); 
     $shift = new Shift(); 

     var_dump($json->user_id); 

     $shift->setDay($json->day); 
     $shift->setMonth($json->month); 
     $shift->setYear($json->year); 
     $shift->setTimeFrom($json->time_from); 
     $shift->setTimeTo($json->time_to); 
     $shift->setUserId($json->user_id); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($shift); 
     $em->flush(); 

     return new JsonResponse($json); 
} 
+0

내가 잘못했을 수도 있지만 문제 해결을 위해 스칼라와 객체 기반 방법을 혼합 한 것 같습니다. 'setter '에서'$ shifts-> setUser ($ this)'를하지만,'$ shift-> setUserId ($ json-> user_id)'액션에서. 이 두 가지를 모두 갖고있는 특별한 이유가 있습니까? –

+0

죄송합니다. 사용자 클래스에서 해당 항목을 삭제하는 것을 잊어 버렸습니다. 내가 var_dump 할 때 나는 컨트롤러에서 user_id지고 있지만 데이터베이스에 저장되지 않습니다. 그리고 나는 로그인 한 사용자의 user_id에 id를 저장하지 않을 것입니다, 나는 선택한 사용자의 user_id에 id를 저장하고 싶습니다. – lorenos

답변

2

class Shift 
{ 

/** 
* Set userId 
* 
* @param integer $userId 
* @return Shift 
*/ 
public function setUserId($userId) 
{ 
    $this->user_id = $userId; 

    return $this; 
} 

/** 
* Get userId 
* 
* @return integer 
*/ 
public function getUserId() 
{ 
    return $this->user_id; 
} 

/** 
* @ORM\ManyToOne(targetEntity="App\AuthBundle\Entity\User", inversedBy="shifts") 
* @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
*/ 
private $users; 

/** 
* Set users 
* 
* @param \App\AuthBundle\Entity\User $users 
* @return Shift 
*/ 
public function setUsers(\App\AuthBundle\Entity\User $users) 
{ 
    $this->users = $users; 

    return $this; 
} 

/** 
* Get users 
* 
* @return \App\AuthBundle\Entity\User 
*/ 
public function getUsers() 
{ 
    return $this->users; 
} 
} 

컨트롤러. ORM (Doctrine뿐 아니라)을 사용하여 작업하면 ID로 작동하지 말고 대상과 모델을 사용해야합니다.

그래서, 당신은 개체 및 모델을 통해 작업해야 의미 객체 관계형 모델로 작업

$shift->setUser($userRepository->find($json->user_id)); 
+0

이'$ userRepostitory = $ this-> getDoctrine() -> getRepository ('AppAuthBundle : User')'와 같이'$ userRepostitory'를 얻고 있습니까? – lorenos

+0

'findById' 대신에'find'를 넣어야했습니다. 감사 :) – lorenos

1

당신이해야한다, 대신하지 적 코드를

$shift->setUserId($json->user_id); 

을한다.

$shift->setUser($userRepository->find($json->user_id)); 
관련 문제