使用Symfony2按国家/地区列出城市

时间:2015-04-03 07:30:22

标签: symfony

我使用symfony2,我有两个表Country和City。 我想按国家列出所有城市 但你总是有这个显示。请帮帮我。

这是我的国家/地区实体

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

    /**
     * Set countryName
     *
     * @param string $countryName
     * @return Country
     */
    public function setCountryName($countryName)
    {
        $this->countryName = $countryName;

        return $this;
    }

    /**
     * Get countryName
     *
     * @return string 
     */
    public function getCountryName()
    {
        return $this->countryName;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->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->cities[] = $cities;

        return $this;
    }

    /**
     * Remove cities
     *
     * @param \Admin\ManagementBundle\Entity\City $cities
     */
    public function removeCity(\Admin\ManagementBundle\Entity\City $cities)
    {
        $this->cities->removeElement($cities);
    }

    /**
     * Get cities
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCities()
    {
        return $this->cities;
    }

       /**
     * @return String()
     */
    public function __toString()
    {
       return $this->getCountryName();
    }

    /**
     * Add adresses
     *
     * @param \Admin\ManagementBundle\Entity\Address $adresses
     * @return Country
     */
    public function addAdress(\Admin\ManagementBundle\Entity\Address $adresses)
    {
        $this->adresses[] = $adresses;

        return $this;
    }

    /**
     * Remove adresses
     *
     * @param \Admin\ManagementBundle\Entity\Address $adresses
     */
    public function removeAdress(\Admin\ManagementBundle\Entity\Address $adresses)
    {
        $this->adresses->removeElement($adresses);
    }

    /**
     * Get adresses
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getAdresses()
    {
        return $this->adresses;
    }
}

这是我的城市实体

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

    /**
     * Set cityName
     *
     * @param string $cityName
     * @return City
     */
    public function setCityName($cityName)
    {
        $this->cityName = $cityName;

        return $this;
    }

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

    /**
     * Set PostalCode
     *
     * @param string $postalCode
     * @return City
     */
    public function setPostalCode($postalCode)
    {
        $this->PostalCode = $postalCode;

        return $this;
    }

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

    /**
     * Set country
     *
     * @param \Admin\ManagementBundle\Entity\Country $country
     * @return City
     */
    public function setCountry(\Admin\ManagementBundle\Entity\Country $country = null)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return \Admin\ManagementBundle\Entity\Country 
     */
    public function getCountry()
    {
        return $this->country;
    }
}

我的控制器

public function getallCountryAction(Request $request) 
   {

    $countryall = $this->getDoctrine()->getRepository('AdminManagementBundle:Country')->findAll();

    //ar_dump($user);die;
      header("Access-Control-Allow-Origin: *");
      $countrytab = array();

            foreach ($countryall as $Country) 
            {
              $countrytab[$Country->getCountryName()] = array(
                  "CountryName" =>  $Country->getCountryName(),
                  "CountryId" => $Country->getId(),
                   "cities"  => $Country->getCities());

            }

    return new JsonResponse($countrytab);

  }

如果我跑,我有这个

{
Tunisia: {
CountryName: "Tunisia",
CountryId: 1,
cities: { }
},
France: {
CountryName: "France",
CountryId: 2,
cities: { }
}
}

城市总是空的:(

1 个答案:

答案 0 :(得分:1)

您可以在城市存储库中使用函数,并在构建数组时调用它:

 foreach ($countryall as $Country) 
        {
          $countrytab[$Country->getCountryName()] = array(
              "CountryName" =>  $Country->getCountryName(),
              "CountryId" => $Country->getId(),
               "cities"  =>  $this->getDoctrine()->getRepository('AdminManagementBundle:City')->findCitiesByCountryId($Country->getId()););

        }

然后在findCitiesByCountryId函数中:

public function findCitiesByCountryId($country_id) {
    $citytab = array();
    $cities_list = $this->findBy('country' => $country_id);
    foreach ($cities_list as $city) {
       $citytab[][$city->getCityName()] = array(
              "CityName" =>  $city->getCityName(),
              "CityId" => $city->getId()); 
    }
return $citytab;
}