Mysql
 sql >> Base de données >  >> RDS >> Mysql

Symfony2 :Lister les villes par pays

Jetez un œil à la configuration bidirectionnelle OneToMany

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html#one-to-many-bidirectional

Voici un exemple utilisant des annotations :

/**
 * @Entity
 * @Table( name="country" )
 */

class Country
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    public $id;

    /**
     * @Column( type="string", length=30, name="name", nullable=false )
     */
    public $name;

    /**
     * @OneToMany( targetEntity="City", mappedBy="Country" )
     */
    private $cities;
}


/**
 * @Entity
 * @Table( name="city" )
 */
class City
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    public $id;

    /**
     * @ManyToOne( targetEntity="Country" )
     * @JoinColumn( name="country", referencedColumnName="id" )
     */
    public $country;

    /**
     * @Column(  type="string", length=30, name="name", nullable=false )
     */
    public $name;
}

Vous devez configurer ceci pour autoriser le $country->getCities() méthode de travail