如何将doctrine2中的实体集合添加到主实体中



我有

class user 
{
    @manytomany    
    private $countries
}

class country {
    @manytomany
    private $user
}

现在,当创建新用户时,我想将预定义的国家列表添加到用户

$countries = $em->getRepository(Country)->findBy(array('population'=>'2000'))

我想知道如何在用户实体中添加这些所有国家

有可能吗

$user->addCountry($countries)

它能工作吗?

class user
{
    private $countries;
    public function addCountries($countries) {
        foreach ($countries as $country) {
            $this->countries[] = $country;
        }
    }
}

最新更新