1

Никак не могу понять один момент в Symfony:

Есть две такие сущности:

Receipt:

namespace AppBundle\Entity;

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", fetch="EAGER")
     * @ORM\Column(name="items", type="json_array", nullable=true)
     */
    private $items;

    /**
     * @var User
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", fetch="EAGER")
     * @ORM\JoinColumn(name="user_holder", onDelete="SET NULL")
     */
     private $user;

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

    /**
     * @return string
     */
    public function getItems()
    {
        return $this->items;
    }

    /**
     * @param array $items
     */
    public function setItems($items)
    {
        $this->items = $items;
    }

    /**
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param User $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }


}

ListItem:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Class ListItem
 *
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ListItemRepository")
 * @ORM\Table(name="`list_item`")
 *
 * @package AppBundle\Entity
 */
class ListItem
{
    /**
     * @var int
     *
     * @ORM\Id()
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     * @ORM\Column(name="name", type="string", length=64, nullable=true)
     */
    private $name;
    /**
     * @var string
     * @ORM\Column(name="shopName", type="string", length=64, nullable=true)
     */
    private $shopName;
    /**
     * @var string
     * @ORM\Column(name="unit", type="string", length=64, nullable=true)
     */
    private $unit;
    /**
     * @var int
     * @ORM\Column(name="quantity", type="float", nullable=true, columnDefinition="FLOAT")
     */
    private $quantity;
    /**
     * @var int
     * @ORM\Column(name="price", type="string", nullable=true, nullable=true)
     */
    private $price;
    /**
     * @var \DateTime
     * @ORM\Column(name="date", type="datetime", nullable=true)
     */
    private $date;
    /**
     * @var string
     * @ORM\Column(name="notes", type="string", length=64, nullable=true)
     */
    private $notes;
    /**
     * @var string
     * @ORM\Column(name="favorite", type="integer", length=64)
     */
    private $favorite = 0;
    /**
     * @var string
     * @ORM\Column(name="checked", type="integer", length=64)
     */
    private $checked = 0;
    /**
     * @var File
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\File", fetch="EAGER")
     * @ORM\JoinColumn(name="photo_id", nullable=true, onDelete="SET NULL")
     *
     */
    private $photoId;
    /**
     * @var Lists
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Lists", fetch="EAGER")
     * @ORM\JoinColumn(name="list_id", nullable=true, onDelete="SET NULL")
     *
     */
    private $list;

    /**
     * @var \DateTime
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * @var \DateTime
     * @ORM\Column(name="updated_at", type="datetime", nullable=true,)
     */
    private $updatedAt;
    /**
     * @var \DateTime
     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
     */
    private $deletedAt;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return ListItem
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set unit
     *
     * @param string $unit
     * @return ListItem
     */
    public function setUnit($unit)
    {
        $this->unit = $unit;

        return $this;
    }

    /**
     * Get unit
     *
     * @return string 
     */
    public function getUnit()
    {
        return $this->unit;
    }

    /**
     * Set quantity
     *
     * @param integer $quantity
     * @return ListItem
     */
    public function setQuantity($quantity)
    {
        $this->quantity = $quantity;

        return $this;
    }

    /**
     * Get quantity
     *
     * @return integer 
     */
    public function getQuantity()
    {
        return $this->quantity;
    }

    /**
     * Set price
     *
     * @param integer $price
     * @return ListItem
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return integer 
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set notes
     *
     * @param string $notes
     * @return ListItem
     */
    public function setNotes($notes)
    {
        $this->notes = $notes;

        return $this;
    }

    /**
     * Get notes
     *
     * @return string 
     */
    public function getNotes()
    {
        return $this->notes;
    }

    /**
     * Set checked
     *
     * @param string $checked
     * @return ListItem
     */
    public function setChecked($checked)
    {
        $this->checked = $checked;

        return $this;
    }

    /**
     * Get checked
     *
     * @return string 
     */
    public function getChecked()
    {
        return $this->checked;
    }

    /**
     * Set photoId
     *
     * @param \AppBundle\Entity\File $photoId
     * @return ListItem
     */
    public function setPhotoId(\AppBundle\Entity\File $photoId = null)
    {
        $this->photoId = $photoId;

        return $this;
    }

    /**
     * Get photoId
     *
     * @return \AppBundle\Entity\File 
     */
    public function getPhotoId()
    {
        return $this->photoId;
    }

    /**
     * Set list
     *
     * @param \AppBundle\Entity\Lists $list
     * @return ListItem
     */
    public function setList(\AppBundle\Entity\Lists $list = null)
    {
        $this->list = $list;

        return $this;
    }

    /**
     * Get list
     *
     * @return \AppBundle\Entity\Lists 
     */
    public function getList()
    {
        return $this->list;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     *
     * @return ListItem
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set deletedAt
     *
     * @param \DateTime $deletedAt
     *
     * @return ListItem
     */
    public function setDeletedAt($deletedAt)
    {
        $this->deletedAt = $deletedAt;

        return $this;
    }

    /**
     * Get deletedAt
     *
     * @return \DateTime
     */
    public function getDeletedAt()
    {
        return $this->deletedAt;
    }

    /**
     * Set favorite
     *
     * @param integer $favorite
     *
     * @return ListItem
     */
    public function setFavorite($favorite)
    {
        $this->favorite = $favorite;

        return $this;
    }

    /**
     * Get favorite
     *
     * @return integer
     */
    public function getFavorite()
    {
        return $this->favorite;
    }

    /**
     * Set shopName
     *
     * @param string $shopName
     *
     * @return ListItem
     */
    public function setShopName($shopName)
    {
        $this->shopName = $shopName;

        return $this;
    }

    /**
     * Get shopName
     *
     * @return string
     */
    public function getShopName()
    {
        return $this->shopName;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return ListItem
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return ListItem
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}

В сущности Receipt в поле items надо положить несколько обьектов типа ListItem.

Каким образом можно этого добиться?

1

1 ответ 1

1

Обратите внимание на данный класс, который реализует связь "один ко многим"

<?php
namespace Test\TestBundle\Entity;

use Tpg\ExtjsBundle\Annotation as Extjs;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use \Test\TestBundle\Entity\Car;

/**
 * @Extjs\Model
 * @Extjs\ModelProxy("/mycarowners")
 * @ORM\Entity
 * @ORM\Table(name="car_owner")
 */
class CarOwner {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMS\Type("integer")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Test\TestBundle\Entity\Car", mappedBy="carOwner")
     * @JMS\Type("ArrayCollection<Test\TestBundle\Entity\Car>")
     */
    protected $cars;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->cars = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add cars
     *
     * @param Car $cars
     * @return CarOwner
     */
    public function addCar(Car $cars)
    {
        $this->cars[] = $cars;
        $cars->setCarOwner($this);
        return $this;
    }

    /**
     * Remove cars
     *
     * @param Car $cars
     */
    public function removeCar(Car $car)
    {
        $this->cars->removeElement($car);
        $cars->setCarOwner(null);
    }

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

}

источник

0

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.