Answer the question
In order to leave comments, you need to log in
What is the correct way to work with doctrine fetch objects in Symfony?
There is Symfony3 , respectively doctrine2 and a few incomprehensible moments.
The following code was written for the test:
$product = $this->getDoctrine()
->getRepository('AppBundle:Product')
->findById(1);
$products = $this->getDoctrine()
->getRepository('AppBundle:Product')
->findAll();
dump($product);
foreach ($product->getImages() as $image) {
dump($image);
}
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="products")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
*/
class Product
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="status", type="integer")
*/
private $status = 0;
/**
* @var int
*
* @ORM\Column(name="is_visible", type="integer")
*/
private $is_visible = 1;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="desc", type="string")
*/
private $desc;
/**
* @ORM\OneToMany(targetEntity="Image", mappedBy="product")
*/
protected $images;
public function __construct()
{
$this->images = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set status
*
* @param integer $status
*
* @return Product
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set isVisible
*
* @param integer $isVisible
*
* @return Product
*/
public function setIsVisible($isVisible)
{
$this->is_visible = $isVisible;
return $this;
}
/**
* Get isVisible
*
* @return integer
*/
public function getIsVisible()
{
return $this->is_visible;
}
/**
* Set name
*
* @param string $name
*
* @return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set desc
*
* @param string $desc
*
* @return Product
*/
public function setDesc($desc)
{
$this->desc = $desc;
return $this;
}
/**
* Get desc
*
* @return string
*/
public function getDesc()
{
return $this->desc;
}
/**
* Add image
*
* @param \AppBundle\Entity\Image $image
*
* @return Product
*/
public function addImage(\AppBundle\Entity\Image $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*
* @param \AppBundle\Entity\Image $image
*/
public function removeImage(\AppBundle\Entity\Image $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* @ORM\Table(name="products_images")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ImageRepository")
*/
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="product_id", type="integer")
*/
private $product_id;
/**
* @var string
*
* @ORM\Column(name="preview_url", type="string")
*/
private $preview_url;
/**
* @var string
*
* @ORM\Column(name="review_url", type="string")
*/
private $review_url;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set productId
*
* @param integer $productId
*
* @return Image
*/
public function setProductId($productId)
{
$this->product_id = $productId;
return $this;
}
/**
* Get productId
*
* @return integer
*/
public function getProductId()
{
return $this->product_id;
}
/**
* Set previewUrl
*
* @param string $previewUrl
*
* @return Image
*/
public function setPreviewUrl($previewUrl)
{
$this->preview_url = $previewUrl;
return $this;
}
/**
* Get previewUrl
*
* @return string
*/
public function getPreviewUrl()
{
return $this->preview_url;
}
/**
* Set reviewUrl
*
* @param string $reviewUrl
*
* @return Image
*/
public function setReviewUrl($reviewUrl)
{
$this->review_url = $reviewUrl;
return $this;
}
/**
* Get reviewUrl
*
* @return string
*/
public function getReviewUrl()
{
return $this->review_url;
}
}
Answer the question
In order to leave comments, you need to log in
I will add Yuri
's answer
All entities are wrapped in proxy objects so that "magic" like lazy loading, etc. would work. That is why in essences there is "more" than there really is.
Regarding collections, Doctrine has such a thing as Collection. You must understand that in the doctrine you operate not with tables in the database, but with objects. Build exactly the object model of your system. In this vein, you can read what an "aggregate of entities" is. In your case, your aggregate will consist of two entities. Product and its Image. For example, if you want to add pictures, you can do this:
/**
* usage: $product->addImage($image);
*/
public function addImage(Image $image)
{
$this->images->add($image);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question