ManyToMany Symfony2: Вывод данных с разных таблиц
Здравствуйте.
В общем, использую symfony2 и doctrine2.
Вопрос следующего характера.
Есть 2 сущности Price и Suppliers. Между ними есть связь ManyToMany.
Логика такова:
Есть позиция в табле Price, эта позиция связана с Табло Suppliers, в частности с полем id. Так вот нужно выводить всю информацию на сайте с таблицы Price, и значение поля supname с таблицы Suppliers. Вот исходники.
P.S. Буду очень благодарен за помощь, так как документация мне не помогла в моей вопросе.
Price.php
<?php
namespace Vega\VegaBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="Vega\VegaBundle\Entity\PriceRepository")
* @ORM\Table(name="Price")
*/
class Price
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
*/
protected $name;
/**
* @ORM\Column(type="string", length=255)
*/
protected $brand;
/**
* @ORM\Column(type="string", length=255)
*/
protected $decription;
/**
* @ORM\Column(type="string", length=255)
*/
protected $cost;
/**
* @ORM\ManyToMany(targetEntity="Suppliers", mappedBy="Price")
*/
protected $sup;
public function __construct()
{
$this->sup = new ArrayCollection();
}
Suppliers.php
<?php
namespace Vega\VegaBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="Vega\VegaBundle\Entity\SuppliersRepository")
* @ORM\Table(name="Suppliers")
*/
class Suppliers
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
*/
protected $supname;
/**
* @ORM\ManyToMany(targetEntity="Price", inversedBy="Suppliers")
* @ORM\JoinTable(name="Price_sup")
*/
protected $supid;
public function __construct()
{
$this->supid = new ArrayCollection();
}
Controller.php
<?php
namespace Vega\VegaBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Vega\VegaBundle\Entity\Price;
use Vega\VegaBundle\Entity\Suppliers;
class DefaultController extends Controller
{
public function indexAction()
{
$em=$this->getDoctrine()->getManager()->getRepository('VegaBundle:Price');
$products=$em->findBy(array(),array('id'=>'ASC'));
if (!$products) {
throw $this->createNotFoundException('Не найдено деталей');
}
else{
return $this->render('VegaBundle:Default:index.html.twig', array('price' => $products));
}
}
}
Шаблон Twig
{% block body %}
<div style="text-align:center;">
Вывод деталей
<table style="width: 800px;margin: 0 auto;">
<thead>
<th>Ид</th>
<th>Название</th>
<th>Цена</th>
<th>Бренд</th>
<th>Описание</th>
<th>Поставщик</th>
</thead>
<tbody>
{% for price in price %}
<tr>
<td>{{ price.id }}</td>
<td>{{ price.name }}</td>
<td>{{ price.cost }}</td>
<td>{{ price.brand }}</td>
<td>{{ price.decription }}</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Источник: Stack Overflow на русском