Doctrine 2 - 多对一,单向 - 未配置为级联持久化操作

时间:2016-04-13 14:13:29

标签: orm doctrine-orm nette

我对教义2(多对一,单向关联)有点问题。

如果我只保存没有文件的日志,那么日志将保存,但是如果我将文件添加到日志,我会收到此错误消息(问题底部的图片)。

我对OneToOne BiDirection assoc有同样的问题。使用日志文件(一个日志有一个文件和文件,如果存在,只有一个日志)

  

$ this-> em - 实体经理

MeetingFile实体

    /* ------------------------- 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实体

/* ------------------------- 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
    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();
}

db scheme withou cascade @ORM\ManyToOne(targetEntity="User") nocascade 如果我设置cascade={"persist"} persist

在实体用户中,我没有与MeetingLog& amp; MeetingFile

我使用nette 2.3

Doctrine用户实体

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,只合并,merge并不返回lastInsertID(插入的实体ID)

THX很多

0 个答案:

没有答案