doctrine 2.0 cascase error

2019-08-03 05:20发布

问题:

I am getting following error in doctrine...

A new entity was found through a relationship that was not configured to cascade persist operations: Default_Model_EventImages@000000004b59e58d000000003ff05ded. Explicitly persist the new entity or configure cascading persist operations on the relationship.

Event model

<?php
/**
 * @Entity
 * @Table(name="events")
 */
class Default_Model_Event
{
     /**
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;


    /** 
    * @Column(name="title", type="string") 
    */
    private $title;

    /** 
    * @Column(name="date_from", type="string") 
    */
    private $date_from;

    /** 
    * @Column(name="date_to", type="string") 
    */
    private $date_to;

    /** 
    * @Column(name="venue", type="string") 
    */
    private $venue;

    /** 
    * @Column(name="address", type="string") 
    */
    private $address;

    /** 
    * @Column(name="city", type="string") 
    */
    private $city;

    /** 
    * @Column(name="state", type="string") 
    */
    private $state;

    /** 
    * @Column(name="zip", type="string") 
    */
    private $zip;

    /** 
    * @Column(name="country", type="string") 
    */
    private $country;

    /** 
    * @Column(name="company_logo", type="string") 
    */
    private $company_logo;

    /** 
    * @Column(name="website", type="string") 
    */
    private $website;

    /** 
    * @Column(name="email", type="string") 
    */
    private $email;

    /** 
    * @Column(name="phone_number", type="string") 
    */
    private $phone_number;  

    /**
    * @Column(name="short_description", type="text") 
    */
    private $short_description;

     /**
    * @Column(name="long_description", type="text") 
    */
    private $long_description;

    /** 
    * @Column(name="status", type="string") 
    */
    private $status;

    /** 
    * @Column(name="date_created", type="string") 
    */
    private $date_created;

    /** 
    * @Column(name="date_modified", type="string") 
    */
    private $date_modified;

    /**
     * @ManyToOne(targetEntity="Default_Model_User", inversedBy="user_mapper")
     * @JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $event_mapper;

    /**
     * @OneToMany(targetEntity="Default_Model_EventImages", mappedBy="eventimage_mapper",cascade={"All"}) 
     */
    private $images_mapper;


    public function setUser($event_mapper)
    {
      if (!is_null($event_mapper )) {
         $event_mapper->assignedToEvent($this);
         $this->event_mapper = $event_mapper;
      }
    }

    public function setEventimage_mapper($name, $order) {
        $this->images_mapper[$name] = new Default_Model_EventImages($name, $order, $this);
    }

    /**
     * @return the $id
     */
    public function getId() {
        return $this->id;
    }


    /**
     * @return the $title
     */
    public function getTitle() {
        return $this->title;
    }

    /**
     * @return the $date_from
     */
    public function getDate_from() {
        return $this->date_from;
    }

    /**
     * @return the $date_to
     */
    public function getDate_to() {
        return $this->date_to;
    }

    /**
     * @return the $venue
     */
    public function getVenue() {
        return $this->venue;
    }

    /**
     * @return the $address
     */
    public function getAddress() {
        return $this->address;
    }

    /**
     * @return the $city
     */
    public function getCity() {
        return $this->city;
    }

    /**
     * @return the $state
     */
    public function getState() {
        return $this->state;
    }

    /**
     * @return the $zip
     */
    public function getZip() {
        return $this->zip;
    }

    /**
     * @return the $country
     */
    public function getCountry() {
        return $this->country;
    }

    /**
     * @return the $company_logo
     */
    public function getCompany_logo() {
        return $this->company_logo;
    }

    /**
     * @return the $website
     */
    public function getWebsite() {
        return $this->website;
    }

    /**
     * @return the $email
     */
    public function getEmail() {
        return $this->email;
    }

    /**
     * @return the $phone_number
     */
    public function getPhone_number() {
        return $this->phone_number;
    }

    /**
     * @return the $short_description
     */
    public function getShort_description() {
        return $this->short_description;
    }

    /**
     * @return the $long_description
     */
    public function getLong_description() {
        return $this->long_description;
    }

    /**
     * @return the $status
     */
    public function getStatus() {
        return $this->status;
    }

    /**
     * @return the $date_created
     */
    public function getDate_created() {
        return $this->date_created;
    }

    /**
     * @return the $date_modified
     */
    public function getDate_modified() {
        return $this->date_modified;
    }

    /**
     * @param field_type $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @param field_type $title
     */
    public function setTitle($title) {
        $this->title = $title;
    }


    /**
     * @param field_type $date_from
     */
    public function setDate_from($date_from) {
        $this->date_from = $date_from;
    }

    /**
     * @param field_type $date_to
     */
    public function setDate_to($date_to) {
        $this->date_to = $date_to;
    }

    /**
     * @param field_type $venue
     */
    public function setVenue($venue) {
        $this->venue = $venue;
    }

    /**
     * @param field_type $address
     */
    public function setAddress($address) {
        $this->address = $address;
    }

    /**
     * @param field_type $city
     */
    public function setCity($city) {
        $this->city = $city;
    }

    /**
     * @param field_type $state
     */
    public function setState($state) {
        $this->state = $state;
    }

    /**
     * @param field_type $zip
     */
    public function setZip($zip) {
        $this->zip = $zip;
    }

    /**
     * @param field_type $country
     */
    public function setCountry($country) {
        $this->country = $country;
    }

    /**
     * @param field_type $company_logo
     */
    public function setCompany_logo($company_logo) {
        $this->company_logo = $company_logo;
    }

    /**
     * @param field_type $website
     */
    public function setWebsite($website) {
        $this->website = $website;
    }

    /**
     * @param field_type $email
     */
    public function setEmail($email) {
        $this->email = $email;
    }

    /**
     * @param field_type $phone_number
     */
    public function setPhone_number($phone_number) {
        $this->phone_number = $phone_number;
    }

    /**
     * @param field_type $short_description
     */
    public function setShort_description($short_description) {
        $this->short_description = $short_description;
    }

    /**
     * @param field_type $long_description
     */
    public function setLong_description($long_description) {
        $this->long_description = $long_description;
    }

    /**
     * @param field_type $status
     */
    public function setStatus($status) {
        $this->status = $status;
    }

    /**
     * @param field_type $date_created
     */
    public function setDate_created($date_created) {
        $this->date_created = $date_created;
    }

    /**
     * @param field_type $date_modified
     */
    public function setDate_modified($date_modified) {
        $this->date_modified = $date_modified;
    }


}//end class

Event images model

<?php
/**
 * @Entity
 * @Table(name="event_images")
 */
class Default_Model_EventImages
{
     /**
     * @Id @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
    * @Column(name="name", type="string") 
    */
    private $name;

    /** 
    * @Column(name="order", type="integer") 
    */
    private $order;

    /** 
    * @Column(name="status", type="string") 
    */
    private $status;

    /** 
    * @Column(name="date_created", type="string") 
    */
    private $date_created;

    /** 
    * @Column(name="date_modified", type="string") 
    */
    private $date_modified;

    /**
     * @ManyToOne(targetEntity="Default_Model_Event", inversedBy="images_mapper", cascade={"All"})
     * @JoinColumn(name="event_id", referencedColumnName="id")
     */
    private $eventimage_mapper;


    public function __construct($name, $order, $event) {
        $this->name = $name;
        $this->order = $order;
        $this->eventimage_mapper = $event;
        $this->date_created = new DateTime();
        $this->status = 'active';
    }

    /**
     * @return the $id
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @return the $status
     */
    public function getStatus() {
        return $this->status;
    }

    /**
     * @return the $date_created
     */
    public function getDate_created() {
        return $this->date_created;
    }

    /**
     * @return the $date_modified
     */
    public function getDate_modified() {
        return $this->date_modified;
    }

    /**
     * @param field_type $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @param field_type $status
     */
    public function setStatus($status) {
        $this->status = $status;
    }

    /**
     * @param field_type $date_created
     */
    public function setDate_created($date_created) {
        $this->date_created = $date_created;
    }

    /**
     * @param field_type $date_modified
     */
    public function setDate_modified($date_modified) {
        $this->date_modified = $date_modified;
    }


}//end class

controller

$user_id = '2'; //(int)$formData['category_id'];
                $user_mapper = $this->_em->find("Default_Model_User", (int)$user_id);
                $event = new Default_Model_Event(); 
                $event->setUser($user_mapper);

                $event->setTitle($title);
                $event->setDate_from($date_from);
                $event->setDate_to($date_to);
                $event->setVenue($venue);
                $event->setAddress($address);
                $event->setCity($city);
                $event->setState($state);
                $event->setZip($zip);
                $event->setCountry($country);
                $event->setCompany_logo($company_logo);
                $event->setWebsite($website);
                $event->setEmail($email);
                $event->setPhone_number($phone);
                $event->setStatus('pending');
                $event->setShort_description($short_description);
                $event->setLong_description($long_description);
                $date = new Zend_Date;
                $event->setDate_created($date->toString('YYYY-MM-dd HH:mm:ss'));

                foreach($images as $key => $value) {
                    $event->setEventimage_mapper($value, $key);
                    //$this->_em->persist($eventImages);
                }

                $this->_em->persist($event);
                $this->_em->flush();

回答1:

The error says that you have to persist Default_Model_EventImages first. So instead of just pushing the Default_Model_EventImages to the images stack - you will also have to explicitly persists these entities.



回答2:

Actually the problem was i had to persist all the event images object individually..

foreach($images as $key => $value) {
    $eventImages = $event->setEventimage_mapper($value, $key);
    $this->_em->persist($eventImages);

}

and in event model

public function setEventimage_mapper($name, $order) {
        return $this->images_mapper[$name] = new Default_Model_EventImages($name, $order, $this);
    }


标签: php doctrine