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

Orm 条令2-多对一,单向-未配置为级联持久化操作,orm,doctrine-orm,nette,Orm,Doctrine Orm,Nette,我对条令2(多对一,单向关联)有点问题 如果我只保存日志而不保存文件,则日志将被保存,但如果我将文件添加到日志,则会收到此错误消息(问题底部的图片) 我对OneTONE双向关联的日志文件也有同样的问题(一个日志有一个文件,如果存在,则只有一个日志) $this->em--实体管理器 会议文件实体 /* ------------------------- Association Mapping ------------------------ */ /** * * @ORM\Man

我对条令2(多对一,单向关联)有点问题

如果我只保存日志而不保存文件,则日志将被保存,但如果我将文件添加到日志,则会收到此错误消息(问题底部的图片)

我对OneTONE双向关联的日志文件也有同样的问题(一个日志有一个文件,如果存在,则只有一个日志)

$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;
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();
}
无需级联
@ORM\manytone(targetEntity=“User”)
如果我设置了
cascade={“persist”}

在实体用户中,我与会议日志和会议文件没有关联

我使用Nette2.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,则只使用merge,merge不会返回lastInsertID(插入的实体ID)


THX很多

您收到的错误消息中有一行重要信息是“未配置为级联持久化操作”。你能用
App\Model\Entities\User
条令注释更新你的帖子吗?好的,解决了-问题是因为我在一个有序列化/非序列化的会话中保存了用户实体,我现在只有会话中的用户IDAh。。。这就解释了一些问题。所以这些关联没有转换回对象?@CharlieVieillard-可能没有,但这足以改变一些方法,并将其加入会话,而不仅仅是用户的id实体-nette论坛建议我使用一个补充,感谢链接!在过去,我也会将ID存储在会话存储中,因为我不喜欢在不同的位置存储相同的信息。这个想法变为在会话中存储实体,非常小心地使用此信息,因为它不可靠,只能在少数情况下使用。您收到的错误消息中的一行重要信息是“未配置为级联持久化操作”。你能用
App\Model\Entities\User
条令注释更新你的帖子吗?好的,解决了-问题是因为我在一个有序列化/非序列化的会话中保存了用户实体,我现在只有会话中的用户IDAh。。。这就解释了一些问题。所以这些关联没有转换回对象?@CharlieVieillard-可能没有,但这足以改变一些方法,并将其加入会话,而不仅仅是用户的id实体-nette论坛建议我使用一个补充,感谢链接!在过去,我也会将ID存储在会话存储中,因为我不喜欢在不同的位置存储相同的信息。这种想法转变为在会话中存储实体,非常小心地使用此信息,因为它不可靠,只能在少数情况下使用。