1

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

db scheme withou cascade @ORM\ManyToOne(targetEntity="User") nocascade If i set cascade={"persist"} 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

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
  • One important line of information from the error message you are receiving is _"not configured to cascade persist operations"_. Can you update your post with the `App\Model\Entities\User` Doctrine annotations? – Charlie Vieillard Apr 14 '16 at 08:11
  • Ok, SOLVED - the problem was caused by the fact that I saved user entity in a session where there was serialize/unserialize, i have now in session only user ID – Jan Sršeň Apr 15 '16 at 11:14
  • Ah ... that explains some. So the associations did not convert back to objects? – Charlie Vieillard Apr 15 '16 at 19:01
  • @CharlieVieillard - Probably not, but it was enough to change a few methods and put in a session instead of just the user's id entity - nette forum to advise me and suggested to use a supplement that allows it [Nette 2 & Doctrine 2 entities](https://github.com/Majkl578/nette-identity-doctrine) – Jan Sršeň Apr 16 '16 at 08:23
  • 1
    Thanks for the link! In the past I would also have stored the ID in the session storage since I'm not fond of storing the same information on different places. That idea changed to storing the entity in the session, using this information with great care because it is unreliable and should only be used in a few number of cases. – Charlie Vieillard Apr 16 '16 at 20:49

0 Answers0