Никак не могу нормально обновить данные в Entity: Затирает поле items
.
Вот Entity:
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Class Receipt
* @ORM\Entity(repositoryClass="AppBundle\Repository\ReceiptRepository")
* @ORM\Table(name="receipt")
*
* @package AppBundle\Entity
*/
class Receipt
{
/**
* @var int
*
* @ORM\Id()
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\Column(name="shop", type="string", nullable=true)
*/
private $shop;
/**
* @var File
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\File", fetch="EAGER")
* @ORM\JoinColumn(name="photo_id", nullable=true, onDelete="SET NULL")
*/
private $photoId;
/**
* @var \DateTime
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* @var \DateTime
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var string
* @ORM\Column(name="amount", type="string")
*/
private $amount;
/**
* @var ListItem
* @ORM\OneToMany(targetEntity="AppBundle\Entity\ListItem", mappedBy="receipt")
*/
private $items;
/**
* @var User
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", fetch="EAGER")
* @ORM\JoinColumn(name="user_holder", onDelete="SET NULL")
*/
private $user;
public function __construct()
{
$this->items = new ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getShop()
{
return $this->shop;
}
/**
* @param string $shop
*/
public function setShop($shop)
{
$this->shop = $shop;
}
/**
* @return File
*/
public function getPhotoId()
{
return $this->photoId;
}
/**
* @param File $photoId
*/
public function setPhotoId($photoId)
{
$this->photoId = $photoId;
}
/**
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* @param \DateTime $date
*/
public function setDate($date)
{
$this->date = $date;
}
/**
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
/**
* @return string
*/
public function getAmount()
{
return $this->amount;
}
/**
* @param string $amount
*/
public function setAmount($amount)
{
$this->amount = $amount;
}
/**
* @param ListItem $item
*/
public function addItems(ListItem $item)
{
$this->items->add($item);
}
/**
* @param ListItem $item
*/
public function removeItems(ListItem $item)
{
$this->items->remove($item);
}
/**
* @return array
*/
public function getItems()
{
return $this->items;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param User $user
*/
public function setUser($user)
{
$this->user = $user;
}
}
а вот, собственно, и способ Добавить или Обновить данные:
public function fillReceipt(User $user,ParamFetcherInterface $fetcher,$listsItems,Receipt $receipt = null)
{
if($receipt == null) {
$receipt = new Receipt();
}
if($fetcher->get('amount') !== null){
$receipt->setAmount($fetcher->get('amount'));
}elseif($receipt->getAmount() !== null){
$receipt->setAmount($receipt->getAmount());
}else{
$receipt->setAmount(null);
}
if($fetcher->get('date') !== null){
$receipt->setDate(new \DateTime($fetcher->get('date'), $a = new \DateTimeZone('GMT')));
}elseif($receipt->getDate() !== null){
$receipt->setDate($receipt->getDate());
}else{
$receipt->setDate(null);
}
if($fetcher->get('shopName') !== null){
$receipt->setShop($fetcher->get('shopName'));
}elseif($receipt->getShop() !== null){
$receipt->setShop($receipt->getShop());
}else{
$receipt->setShop(null);
}
$flag = 0;
foreach($listsItems as $item)
{
if($item !== null){
$flag = 1;
}
}
if($flag == 1) {
$receipt = $this->fillReceiptItems($receipt, $listsItems);
}
$receipt->setUser($user);
$receipt->setCreatedAt(new \DateTime('now', new \DateTimeZone('GMT')));
$this->_em->persist($receipt);
$this->_em->flush();
return $receipt->getId();
}
На обновление из контроллера вызываю так:
/**
* Update receipt
* ("X-Auth-Token", "id" - are required parameters)
*
* @Route("/receipt/{id}", methods={"PUT"})
* @ApiDoc(
* resource=true,
* section="Receipt"
* )
* @RequestParam(name="amount",nullable=true)
* @RequestParam(name="shopName",nullable=true)
* @RequestParam(name="date", description="YYYY-mm-dd",nullable=true)
* @RequestParam(name="notes",nullable=true)
* @RequestParam(name="items",array=true,nullable=true)
*
* @return Receipt
*/
public function putUpdateReceiptAction($id,Request $request, ParamFetcherInterface $fetcher)
{
try {
$apiKey = $request->headers->get('X-Auth-Token');
$token = $this->get('app.repository.api_token')->findNotExpiredApiTokenByApiKey($apiKey);
/** @var User $user */
$user = $this->get('app.repository.user')->find($token->getUser()->getId());
$items[] = null;
$k = 0;
foreach($fetcher->get('items') as $ids) {
$items[$k] = $this->get('app.repository.listitem')->find($ids);
$k++;
}
$receipt = $this->get('app.repository.receipt')->find($id);
$this->get('app.repository.receipt')->fillReceipt($user,$fetcher,$items,$receipt);
return $this->get('app.repository.receipt')->find($id);
} catch (Exception $e) {
return $this->get('app.service.response')->ExceptionHandler($e);
} catch (BadRequestHttpException $e) {
return $this->get('app.service.response')->BadRequestHttpExceptionHandler($e);
}
}
но при обновлении items
- пустые, даже если я их передаю методом PUT
.