我对学说 2(多对一,单向关联)有一点问题。
如果我只保存没有文件的日志,那么日志将被保存,但是如果我将文件添加到日志中,我会收到此错误消息(问题底部的图片)。
我对 OneToOne BiDirection assoc 有同样的问题。with log - file(一个日志有一个文件,如果存在文件,只有一个日志)
$this->em -- 实体管理器
会议文件实体
/* ------------------------- 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;
会议日志实体
/* ------------------------- 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;
用文件保存日志的外观
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();
}
没有级联@ORM\ManyToOne(targetEntity="User")
如果我设置cascade={"persist"}
在实体用户中,我没有与 MeetingLog 和 MeetingFile 的关联
我使用网络 2.3
教义用户实体
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);
}
}
如果我不使用persist,则仅合并,并且合并不返回lastInsertID(插入的实体ID)
多谢