Symfony2 считает поля сущности, которые относятся к другой сущности
У меня есть 2 лица - Авторы и Книги, у одного автора может быть много книг. Я хочу показать в таблице, сколько книг имеет КАЖДЫЙ автор (разное количество для каждого автора). Я уже виделаэтот вопрос, такжеэтот, а такжеэтот и попробовал это, так как я думал, что это будет более элегантное решение:
<td>{{books|length}}</td>
но каждый раз я получаю общее количество книг для ВСЕХ авторов. В моем контроллере я получаю такие книги:
$query = $em->createQuery('SELECT b FROM AB\ProjectBundle\Entity\Books u WHERE b.authorid in (:authorids)');
$query->setParameter('authorid',$authorids);
$books = $query->getResult();
и авторы выбираются так:
$query = $em->createQuery('SELECT a FROM AB\ProjectBundle\Entity\Authors a');
$authorids = $query->getResult();
РЕДАКТИРОВАТЬ: Моя веточка петли
<tbody>
{% for authors in author %}
<tr>
<td>{{ authors.name }}</td>
<td>{{ authors.isactive }}</td>
<td>{{ books.authorid|length}}</td>
</tr>
{% endfor %}
</tbody>
РЕДАКТИРОВАТЬ 2 My Author лица
class Author
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* Set name
*
* @param string $name
* @return string
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
РЕДАКТИРОВАТЬ 3 Книги лица
<?php
namespace AB\ProjectBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
*/
class Books
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var \AB\ProjectBundle\Entity\Author
*/
private $authorid;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set Authorid
*
* @param \AB\ProjectBundle\Entity\Author $authorid
* @return Author
*/
public function setAuthorid(\AB\ProjectBundle\Entity\Author $authorid = null)
{
$this->authorid = $authorid;
return $this;
}
/**
* Get Authorid
*
* @return \AB\ProjectBundle\Entity\Author
*/
public function getAuthorid()
{
return $this->authorid;
}
/**
* Set name
*
* @param string $name
* @return string
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
Аннотаций нет, сущности отображаются в * .orm.yml файлах. Book.orm.yml:
AB\ProjectBundle\Entity\Books:
type: entity
table: Books
id:
id:
type: integer
nullable: false
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
name:
type: text
nullable: false
manyToOne:
authorid:
targetEntity: Author
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
authorid:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
Author.orm.yml
AB\ProjectBundle\Entity\Author:
type: entity
table: Author
id:
id:
type: integer
nullable: false
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
name:
type: text
nullable: false
lifecycleCallbacks: { }