lorenos lorenos - 1 month ago 4
PHP Question

User id won't save to database getting null

I have one to many relationship in Symfony. So im building my own shift managament for creating events for multiple users.

When im saving an event to database im getting user_id null, and i won't save it for the logged in user i wanna save the event by multiple users.

So i wan't to save user_id for the user that i wanna save the event, on var_dump im getting the user id for that user but when it saves im getting null in my database. Anyone know how to solve that and whats the problem?

Class User:

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;
}

}


Class Shift

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;
}
}


Controller

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);
}

Answer

You should not assign IDs to an entity. Working with ORM (any, not only Doctrine) you should not operate with IDs, but with objects and models.

So, do not ever code

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

Instead, you should make

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