title: list cities by country with Symfony2
body: <p>I'm using symfony2, I have two tables Country and City.
  I want to list all cities by country 
  but you have always this display. Please help me.</p>
  
  <p>This is my country entity </p>
  
  <pre><code>    &lt;?php
  
  namespace Admin\ManagementBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Country
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Admin\ManagementBundle\Entity\CountryRepository")
   */
  class Country
  {
  /**
   * @ORM\OneToMany(targetEntity="Admin\ManagementBundle\Entity\City" ,mappedBy="country")
   * @ORM\JoinColumn(nullable=false)
   */
    private $cities;
  
    /**
   * @ORM\OneToMany(targetEntity="Admin\ManagementBundle\Entity\Address" ,mappedBy="country")
   * @ORM\JoinColumn(nullable=false)
   */
    private $adresses;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="CountryName", type="string", length=255)
       */
      private $countryName;
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set countryName
       *
       * @param string $countryName
       * @return Country
       */
      public function setCountryName($countryName)
      {
          $this-&gt;countryName = $countryName;
  
          return $this;
      }
  
      /**
       * Get countryName
       *
       * @return string 
       */
      public function getCountryName()
      {
          return $this-&gt;countryName;
      }
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;cities = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Add cities
       *
       * @param \Admin\ManagementBundle\Entity\City $cities
       * @return Country
       */
      public function addCity(\Admin\ManagementBundle\Entity\City $cities)
      {
          $this-&gt;cities[] = $cities;
  
          return $this;
      }
  
      /**
       * Remove cities
       *
       * @param \Admin\ManagementBundle\Entity\City $cities
       */
      public function removeCity(\Admin\ManagementBundle\Entity\City $cities)
      {
          $this-&gt;cities-&gt;removeElement($cities);
      }
  
      /**
       * Get cities
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getCities()
      {
          return $this-&gt;cities;
      }
  
         /**
       * @return String()
       */
      public function __toString()
      {
         return $this-&gt;getCountryName();
      }
  
      /**
       * Add adresses
       *
       * @param \Admin\ManagementBundle\Entity\Address $adresses
       * @return Country
       */
      public function addAdress(\Admin\ManagementBundle\Entity\Address $adresses)
      {
          $this-&gt;adresses[] = $adresses;
  
          return $this;
      }
  
      /**
       * Remove adresses
       *
       * @param \Admin\ManagementBundle\Entity\Address $adresses
       */
      public function removeAdress(\Admin\ManagementBundle\Entity\Address $adresses)
      {
          $this-&gt;adresses-&gt;removeElement($adresses);
      }
  
      /**
       * Get adresses
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getAdresses()
      {
          return $this-&gt;adresses;
      }
  }
  </code></pre>
  
  <p>this is my city entity</p>
  
  <pre><code>    &lt;?php
  
  namespace Admin\ManagementBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * City
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Admin\ManagementBundle\Entity\CityRepository")
   */
  class City
  {
      /**
       * @ORM\ManyToOne(targetEntity="Admin\ManagementBundle\Entity\Country", inversedBy="cities")
       * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
       **/
      private $country;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="CityName", type="string", length=255)
       */
      private $cityName;
  
  
  
  
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set cityName
       *
       * @param string $cityName
       * @return City
       */
      public function setCityName($cityName)
      {
          $this-&gt;cityName = $cityName;
  
          return $this;
      }
  
      /**
       * Get cityName
       *
       * @return string 
       */
      public function getCityName()
      {
          return $this-&gt;cityName;
      }
  
      /**
       * Set PostalCode
       *
       * @param string $postalCode
       * @return City
       */
      public function setPostalCode($postalCode)
      {
          $this-&gt;PostalCode = $postalCode;
  
          return $this;
      }
  
      /**
       * Get PostalCode
       *
       * @return string 
       */
      public function getPostalCode()
      {
          return $this-&gt;PostalCode;
      }
  
      /**
       * Set country
       *
       * @param \Admin\ManagementBundle\Entity\Country $country
       * @return City
       */
      public function setCountry(\Admin\ManagementBundle\Entity\Country $country = null)
      {
          $this-&gt;country = $country;
  
          return $this;
      }
  
      /**
       * Get country
       *
       * @return \Admin\ManagementBundle\Entity\Country 
       */
      public function getCountry()
      {
          return $this-&gt;country;
      }
  }
  </code></pre>
  
  <p>my controller </p>
  
  <pre><code>public function getallCountryAction(Request $request) 
     {
  
      $countryall = $this-&gt;getDoctrine()-&gt;getRepository('AdminManagementBundle:Country')-&gt;findAll();
  
      //ar_dump($user);die;
        header("Access-Control-Allow-Origin: *");
        $countrytab = array();
  
              foreach ($countryall as $Country) 
              {
                $countrytab[$Country-&gt;getCountryName()] = array(
                    "CountryName" =&gt;  $Country-&gt;getCountryName(),
                    "CountryId" =&gt; $Country-&gt;getId(),
                     "cities"  =&gt; $Country-&gt;getCities());
  
              }
  
      return new JsonResponse($countrytab);
  
    }
  </code></pre>
  
  <p>if I run, I have this </p>
  
  <pre><code>{
  Tunisia: {
  CountryName: "Tunisia",
  CountryId: 1,
  cities: { }
  },
  France: {
  CountryName: "France",
  CountryId: 2,
  cities: { }
  }
  }
  </code></pre>
  
  <p>cities are always empty :( </p>