Я пытаюсь объединить две таблицы с отношением Один-ко-многим. Существует две таблицы Product и ProductPhoto
Product. php:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
....
/**
* @ORM\Column(type="integer")
*/
protected $price;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="ProductPhoto", mappedBy="product", orphanRemoval=true, cascade={"ALL"})
*/
protected $photos;
public function __construct() {
$this->photos = new ArrayCollection();
}
/**
* @return ArrayCollection
*/
public function getPhotos() : ArrayCollection
{
return $this->photos;
}
}
ProductPhoto. php:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class ProductPhoto
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $url;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="photos")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
protected $product;
}
Index. php:
<?php
require_once "../config/bootstrap.php";
$productRepository = $entityManager->getRepository('\App\Entity\Product');
$products = $productRepository->findAll();
foreach ($products as $product) {
/**
* @var \App\Entity\Product $product
*/
$product->getPhotos()->toArray();
}
Таблицы были успешно созданы, и я ввел данные:
Таблица продуктов
id|name |description|price|
1 |iPhone| | |
Таблица ProductPhoto
id|product_id|url |
1 |1 |t.jpg|
Я могу ' фотографии, где ошибка? Свойство photos содержит Doctrine \ ORM \ PersistentCollection