Bob
Bob

Reputation: 2510

Symfony2: Repository's findAll() returns multiple instances of same entity instead of all entities

I have the following entity with four fields: $id, $name, $flagFileName and $inUse.

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Language
 *
 * @ORM\Table(name="language", uniqueConstraints={@ORM\UniqueConstraint(name="name_UNIQUE", columns={"name"})}, indexes={@ORM\Index(name="key_in_use", columns={"in_use"})})
 * @ORM\Entity
 */
class Language
{
    /**
     * @var boolean
     *
     * @ORM\Column(name="id", type="boolean", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="in_use", type="integer", nullable=true)
     */
    private $inUse;

    /**
     * @return string
     */
    public function getFlagFileName()
    {
        return $this->flagFileName;
    }

    /**
     * @param string $flagFileName
     * @return Language
     */
    public function setFlagFileName($flagFileName)
    {
        $this->flagFileName = $flagFileName;
        return $this;
    }

    /**
     * @return boolean
     */
    public function isId()
    {
        return $this->id;
    }

    /**
     * @param boolean $id
     * @return Language
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getInUse()
    {
        return $this->inUse;
    }

    /**
     * @param int $inUse
     * @return Language
     */
    public function setInUse($inUse)
    {
        $this->inUse = $inUse;
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return Language
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    function __toString()
    {
        return $this->getName();
    }

}

And this is what I currently have in the mapped table (language): description

That table has the following structure in my MySQL database:

CREATE TABLE `language` (
  `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(60) NOT NULL,
  `flag_file_name` char(6) NOT NULL,
  `in_use` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `name_UNIQUE` (`name`),
  KEY `key_in_use` (`in_use`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

My problem is that when I try to fetch all the languages from the repository, I get N instances of the first record, instead of all the records (where N is the number of records in the "language" table).

To make it clear, when I run the following as the first instruction in my controller's action:

  $this->getDoctrine()->getRepository("AppBundle:Language")->findAll()

I get the following :

enter image description here

What I expected to receive, was an array filled with all the languages, not just the first language duplicated 6 times! Why does it happens?

Upvotes: 2

Views: 789

Answers (2)

Jason Roman
Jason Roman

Reputation: 8276

If you auto-generated your entity, it thought that your id field was a boolean because you defined it as a tinyint(3). If not, you defined it incorrectly, and it should be @ORM\Column(name="id", type="integer", nullable=false).

Furthermore the isId() function should be getId() and your setId() function should be removed. So you should see this instead:

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

Upvotes: 2

metfan
metfan

Reputation: 98

There is a typo in your $id annotation. Type must be "integer" not "boolean"

Upvotes: 1

Related Questions