как сохранить запись (из службы), у которой одно из свойств установлено как отношение OneToMany с другим объектом?
У меня есть следующий класс обслуживания для моих пользователей:
namespace Federico\Entity;
use Federico\Entity\User;
/**
* Description of UserService
*
* @author fiodorovich
*/
class UserService {
protected $em;
public function __construct ($em) {
$this->em = $em;
}
public function saveUser($user) {
if ($user['id'] != null) { //update
$entity = $this->getUser($user['id']);
//do something
if (!$entity)
throw new Exception('Error saving user!');
} else { //insert
$entity = new User();
foreach ($user as $k => $v) {
if ($k !== 'submit') {
if ($k == 'password') {
//echo $this->_salt;die;
$entity->$k = $this->createPass($v);
} else {
$entity->$k = $v;
}
}
}
}
$this->em->persist($entity);
$this->em->flush(); //save the user
}
//do something else...
}
Но когда я пытаюсь сохранить свойство пользователя и страны (коллекция, которая ссылается на отношение OneToMany oneUser-manyCountries), я получаю исключение "Класс не существует".
РЕДАКТИРОВАТЬ: Пользователь и страны субъектов
namespace Federico\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Description of User
* @Table(name="users")
* @Entity
* @author fiodorovich
*/
class User
{
/**
* @var integer
* @Id @Column (name="id", type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* @Column(type="string",length=60,nullable=true, unique=true)
* @var string
*/
private $email;
/**
* @Column(type="string",length=60,nullable=true)
* @var string
*/
private $password;
/**
* @Column(type="string",length=60,nullable=true,unique=true)
* @var string
*/
private $url;
/**
* @Column(type="string",length=60,nullable=true,unique=true)
* @var string
*/
private $responsable;
/**
* @Column(type="string",length=20,nullable=true)
* @var string
*/
private $role;
/**
*
* @var datetime
* @Column(type="datetime", nullable=false)
*/
private $created;
/**
*
* @param \Doctring\Common\Collections\Collection $property
* @OneToMany(targetEntity="Countries",mappedBy="user", cascade={"persist", "remove"})
*/
private $countries;
public function __construct()
{
$this->created = new \DateTime(date('Y-m-d H:i:s'));
$this->countries = new ArrayCollection();
}
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
public function getCountries()
{
return $this->countries;
}
}
И Страна субъекта:
namespace Federico\Entity;
/**
* @Table(name="countries")
* @Entity
* @author fiodorovich
*
*/
class Countries {
/**
* @var integer
* @Id @Column (name="id", type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
*
* @var string
* @Column(type="string")
*/
private $countryName;
/**
*
* @var User
* @ManyToOne(targetEntity="User", inversedBy="id")
* @JoinColumns({
* @JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}