我有
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;
}
}
}