【问题标题】:Doctrine 2 findAll returns just 1 resultDoctrine 2 findAll 仅返回 1 个结果
【发布时间】:2016-02-29 05:15:53
【问题描述】:

findAll() 在我的存储库中找到一个结果:

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

实体类:

<?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;
    }
}

有人可以帮忙吗?

【问题讨论】:

  • 因为 findAll 方法调用看起来是正确的,只是一些非常基本的东西:您可能在“subtipo”表中有多个条目?您如何检查结果集合的长度?

标签: php symfony orm doctrine-orm doctrine


【解决方案1】:

如果你这样做

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

您正在创建一个数组数组(或ArrayCollection 的数组),因此,如果您检查$retorno[] 的长度,这将产生一个包含真实结果的元素。

类似

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

只要使用这个语法

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

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2017-08-25
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多