Symfony2 cuenta los campos de entidad que se relacionan con otra entidad
Tengo 2 entidades: Autores y Libros, 1 autor puede tener muchos libros. Quiero mostrar en una tabla cuántos libros tiene CADA autor (número diferente por autor). Ya he vistoesta pregunta, tambiénestayesta e intenté esto, ya que pensé que sería una solución más elegante:
<td>{{books|length}}</td>
pero cada vez que obtengo el número total de libros para TODOS los autores. En mi controlador obtengo los libros así:
$query = $em->createQuery('SELECT b FROM AB\ProjectBundle\Entity\Books u WHERE b.authorid in (:authorids)');
$query->setParameter('authorid',$authorids);
$books = $query->getResult();
y los autores se seleccionan así:
$query = $em->createQuery('SELECT a FROM AB\ProjectBundle\Entity\Authors a');
$authorids = $query->getResult();
EDITAR: Mi bucle de ramita
<tbody>
{% for authors in author %}
<tr>
<td>{{ authors.name }}</td>
<td>{{ authors.isactive }}</td>
<td>{{ books.authorid|length}}</td>
</tr>
{% endfor %}
</tbody>
EDITAR 2 Mi entidad de autor
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;
}
}
EDITAR 3 entidad de libros
<?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;
}
}
No hay anotaciones, las entidades se asignan en archivos * .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: { }