Lucas Siqueira
Lucas Siqueira

Reputation: 47

Doctrine 2 findAll returns just 1 result

The findAll() finds one result in my repository:

$retorno[] = $bd->getEntityManager()->getRepository($classname)->findAll();

Class of entity:

<?php
use Doctrine\ORM\Mapping as ORM;

/**
 * Subtipo
 *
 * @ORM\Table(name="subtipo", indexes={@ORM\Index(name="fk_Subtipo_Tipo1_idx", columns={"Tipo_id"})})
 * @ORM\Entity
 */
class Subtipo
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nome", type="string", length=45, nullable=false)
     */
    private $nome;

    /**
     * @var \Tipo
     *
     * @ORM\ManyToOne(targetEntity="Tipo")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="Tipo_id", referencedColumnName="id")
     * })
     */
    private $tipo;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nome
     *
     * @param string $nome
     *
     * @return Subtipo
     */
    public function setNome($nome)
    {
        $this->nome = $nome;

        return $this;
    }

    /**
     * Get nome
     *
     * @return string
     */
    public function getNome()
    {
        return $this->nome;
    }

    /**
     * Set tipo
     *
     * @param \Tipo $tipo
     *
     * @return Subtipo
     */
    public function setTipo(\Tipo $tipo = null)
    {
        $this->tipo = $tipo;

        return $this;
    }

    /**
     * Get tipo
     *
     * @return \Tipo
     */
    public function getTipo()
    {
        return $this->tipo;
    }
}

Somebody can help?

Upvotes: 0

Views: 649

Answers (1)

DonCallisto
DonCallisto

Reputation: 29932

If you do this

$retorno[] = $bd->getEntityManager()->getRepository($classname)->findAll();
        ^----- this

You're creating an array of arrays (or array of ArrayCollection) so, if you inspect length of $retorno[] this will result in 1 element that will contain real results.

Something like

$retorno = [
    0 => [
        0 => 'first real result',
        1 => 'second real result',
        [...]
        n => 'nth real result'
    ]
];

Just use this syntax

$retorno = $bd->getEntityManager()->getRepository($classname)->findAll();

Upvotes: 2

Related Questions