I have little bit problem with doctrine 2 (Many-To-One, Unidirectional assoc.).
If I save only log without file, then log will be save, but if I add file to log, i got this error messages (pictures at the bottom of question).
I have same problem with OneToOne BiDirection assoc. with log - file (one log have one file and file if exist, have only one log)
$this->em -- entity manager
MeetingFile Entity
/* ------------------------- Association Mapping ------------------------ */
/**
*
* @ORM\ManyToOne(targetEntity="Meeting")
* @ORM\JoinColumn(name="meeting_id", referencedColumnName="id")
*/
protected $meeting;
/**
*
* -- 5.1. Many-To-One, Unidirectional
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
protected $created_by;
/**
*
*
* @ORM\OneToOne(targetEntity="MeetingLog", inversedBy="file", cascade={"persist"})
* @ORM\JoinColumn(name="log_id", referencedColumnName="id")
*/
protected $log;
MeetingLog Entity
/* ------------------------- Association Mapping ------------------------ */
/**
*
* @ORM\ManyToOne(targetEntity="MeetingLogType", inversedBy="ac_meeting_log")
* @ORM\JoinColumn(name="log_type_id", referencedColumnName="id")
*/
protected $log_type;
/**
*
* -- 5.1. Many-To-One, Unidirectional
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
protected $created_by;
/**
*
* @ORM\ManyToOne(targetEntity="Meeting", inversedBy="ac_meeting_log")
* @ORM\JoinColumn(name="meeting_id", referencedColumnName="id")
*/
protected $meeting;
Facade to save log with file
public function addLog(NS_User $creator, $values) {
$created_by = $creator->identity->entity;
$this->em->clear();
$log = new MeetingLog();
$log->name = $values->name;
$log->description = $values->description;
$log->created = new DateTime();
$log->created_by = $created_by;
$log->meeting = $this->getMeeting($values->mid);
$log->log_type = $this->getMeetingLogType(1);
$this->em->merge($log);
//Add file
if ($values->file_1->name != NULL) {
$file = new MeetingFile();
$file->name = $values->file_1->getName();
$file->revision = 1.0;
$file->size = $values->file_1->getSize();
$file->content_type = $values->file_1->getContentType();
$file->sanitized_name = $values->file_1->getSanitizedName();
$file->created = new DateTime();
$file->meeting = $this->getMeeting($values->mid);
$file->created_by = $created_by;
$file->log = $log;
$this->em->merge($file);
$this->em->flush();
$this->file_facade->addFile('meetings', $values->mid, $values->file_1);
}
// Store data to Db tables
$this->em->flush();
}
withou cascade
@ORM\ManyToOne(targetEntity="User")
If i set
cascade={"persist"}
In entity User i dont have association to MeetingLog & MeetingFile
I use nette 2.3
Doctrine User Entity
namespace App\Model\Entities;
use Doctrine\ORM\Mapping as ORM;
use Kdyby\Doctrine\Entities\BaseEntity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Doctrine entity for table Users.
* @package App\Model\Entities
* @ORM\Entity
* @ORM\Table(name="user")
*
* @author
*/
class User extends BaseEntity {
/** admin have ID 1. */
const ROLE_ADMIN = 1;
/**
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
protected $id;
/**
*
* @ORM\Column(type="string")
*/
protected $password;
/**
*
* @ORM\Column(type="string")
*/
protected $email;
/**
*
* @ORM\Column(type="string")
*/
protected $first_name;
/**
*
* @ORM\Column(type="string")
*/
protected $last_name;
/**
*
* @ORM\Column(type="datetime")
*/
protected $created;
/**
*
* @ORM\Column(type="integer")
*/
protected $created_by;
/**
*
* @ORM\Column(type="datetime")
*/
protected $edited;
/**
*
* @ORM\Column(type="integer")
*/
protected $edited_by;
/**
*
* @ORM\Column(type="boolean")
*/
protected $active;
/* ------------------------- Association Mapping ------------------------ */
/**
*
* @ORM\ManyToOne(targetEntity="AclRole", inversedBy="ac_user")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
*
*/
protected $role;
/**
*
* @ORM\ManyToOne(targetEntity="Tm1Role", inversedBy="ac_user")
* @ORM\JoinColumn(name="tm1_role_id", referencedColumnName="id")
*
*/
protected $tm1_role;
/**
*
* @ORM\OneToMany(targetEntity="UserLoginInfo", mappedBy="user")
*/
protected $ac_login_info;
/**
*
* @ORM\OneToMany(targetEntity="UserAttribute", mappedBy="user")
*/
protected $ac_user_attribute;
/* --------------------------- Entity Methods --------------------------- */
public function __construct() {
parent::__construct();
$this->ac_login_info = new ArrayCollection();
$this->ac_user_attribute = new ArrayCollection();
}
/**
*
* @param \App\Model\Entities\UserLoginInfo $info
*/
public function addLoginInfo(UserLoginInfo $info) {
$this->ac_login_info[] = $info;
$info->user = $this;
}
/**
*
* @param \App\Model\Entities\UserAttribute $attribute
*/
public function addUserAttribute(UserAttribute $attribute) {
$this->ac_user_attribute[] = $attribute;
$attribute->user = $this;
}
/**
*
* @return bool - vrací true, pokud je uživatel administrátor; jinak vrací false
*/
public function isAdmin() {
return ($this->role->id === self::ROLE_ADMIN ? true : false);
}
/* ---------------------- Others Entity Methods ------------------------- */
/**
*
* @return bool -
*/
public function isEditable() {
return $this->role->editable;
}
private function getUserAttributeByName($name) {
foreach ($this->ac_user_attribute as $attribute) {
if ($attribute->user_attribute_type->name == $name) {
return $attribute->user_attribute_param->value;
}
}
}
public function getUserAttributeLangValue() {
return $this->getUserAttributeByName(UserAttributeType::ATTR_LANG);
}
}
If I dont use persist, only merge, and merge doesnt return lastInsertID (inserted entity ID)
THX a lot