2012-03-13 10 views
6

दो संस्थाओं GalleryAlbum और Galleryimage निकाल नहीं है OneToMany/ManyToOne संबंध:Symfony2: PrePersist/PreUpdate जीवन चक्र घटना

One GalleryAlbum ==== can have ====> Many GalleryImage

Many GalleryImage === can be in ===> One GalleryAlbum

(नीचे स्रोतों)

समस्या क्या है?

  1. जोड़ा जा रहा है (अपलोड) GalleryAlbum

    करने के लिए फ़ाइलों
  2. $ em-> जारी रहती है ($ एलबम)

  3. $ em-> फ्लश()

  4. प्रत्येक अपलोड की गई फ़ाइल के लिए GalleryAlbum क्लास $ छवियों को एक नई गैलरी इमेज इकाई बनाता है और जोड़ता है

  5. मेरा ईसीएचओ/एक्स्टिट परीक्षण नहीं है टी दिखाया गया है (गैलरी इमेज का प्रीपेर्सिस्ट/प्रीअपडेट इवेंट कॉलबैक फ़ंक्शन प्रीप्लोड लोड नहीं किया गया है!)

  6. मेरी नई छवियां डेटाबेस में सहेजी नहीं गई हैं? क्यूं कर?

अजीब क्या है!अगर मैं कार्य करें:

  1. (अपलोडिंग) फ़ाइलों को जोड़ने

  2. $ em-> जारी रहती है ($ एलबम)

  3. $ em-> फ्लश()

  4. फिर से $ em-> फ्लश()

  5. मेरा ईसीएचओ/एक्सआईटीटी परीक्षण दिखाया गया है (गैलरी इमेज का प्रीपेर्सिस्ट/प्रीउप्लोड इवेंट कॉलबैक फ़ंक्शन प्रीप्लोड लोड किया गया है!)

  6. (यदि मैं गूंज/बाहर निकलता हूं) मेरी नई गैलरी छवियां अब सहेजी गई हैं !!!

क्यों?

जब मैं फ्लश() एक बार फ्लश करता हूं तो प्रीप्लोड कभी ट्रिगर नहीं होता है, और जब मैं दो बार फ्लश करता हूं तो ट्रिगर होता है?

# src GalleryAlbum.php

 
    /** 
    * @ORM\Entity 
    * @ORM\HasLifecycleCallbacks 
    * @ORM\Table(name="gallery_album") 
    */ 
    class GalleryAlbum 
    { 
     // some properties like id, name, description, etc 

     /** 
     * @ORM\OneToMany(targetEntity="GalleryImage", mappedBy="parent") 
     */ 
     protected $images; 

     /* Files container. Used for upload service. Must not be persisted. */ 

     protected $files;  

     /* @ORM\Column(type="boolean", nullable=TRUE) 
     * 
     * if set to true will updateing object and calling preUpdate event callback 
     * becouse it's always set to null in database by prePersist event callback */ 

     protected $files_added; 

     /** 
     * Set container files 
     * 
     * @return GalleryAlbum 
     */ 
     public function setFiles($files) 
     { 
      $this->files = $files; 
      $this->files_added = true; 
      /* setting files_added to true forces EntityManager to update 
       * this GalleryAlbum even if no other properties have changed */ 

      return $this; 
     } 

     /** 
     * @ORM\PrePersist() 
     * @ORM\PreUpdate() 
     */ 
     public function preUpload() 
     { 
      if(null !== $this->files) { 
       foreach($this->files as $key =>$file) { 
        $this->addGalleryElement($file); 
        unset($this->files[$key]); 
       } 
      } 
      /* Resetting property files_added to NULL 
      * so it always stays null in database */ 
      $this->files_added = null; 
     } 


     /** 
     * Constructing new GalleryImage and setting it's file and parent 
     */ 
     public function addGalleryElement($file) 
     {  
      $element = new GalleryImage($this, $file); 
      $this->addGalleryImage($element); 
     } 
    } 

# src Galleryimage।php

 
    /** 
    * @ORM\Entity 
    * @ORM\HasLifecycleCallbacks 
    * @ORM\Table(name="gallery_image") 
    */ 
    class GalleryImage 
    { 
     // some properties like id, name, description, etc 

     /** 
     * @ORM\ManyToOne(targetEntity="GalleryAlbum", inversedBy="images") 
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id") 
     */ 
     protected $parent; 

     /* Constructing new GalleryImage */ 

     public function __construct($parent = null, $file = null) 
     { 
      if($parent) $this->setParent($parent); 
      if($file) $this->setFile($file);  
     } 

     /** 
     * @ORM\PrePersist() 
     * @ORM\PreUpdate() 
     */ 
     public function preUpload() 
     { 
      echo 'TEST: is this event callback function fired?'; exit; 

      if(null !== $this->file) { 
       $this->path = $this->file->guessExtension(); 
      } 

      $this->file_added = null; 
     } 
    } 

उत्तर

6

पहली बार जब आप सिद्धांत जारी रहती फोन केवल $ एलबम और नहीं अपनी छवियों बचत होगी।

/** 
    * @ORM\OneToMany(targetEntity="GalleryImage", mappedBy="parent", cascade={"persist", "remove"}) 
    */ 
    protected $images; 

इस तरह से जब आप जारी रहती है कॉल ($ एल्बम) यह भी आपकी छवियों बने रहेंगे और अपने Galleryimage में preUpload उत्प्रेरित करने चाहिए: आप है कि आप सिद्धांत झरना $ छवियों घोषणा में यह निर्दिष्ट करने के द्वारा जारी रहती चाहते निर्दिष्ट करना होगा । वहाँ कुछ विभिन्न विकल्प व्यापक के लिए उपलब्ध हैं और वे अच्छी तरह से यहां समझाए गए हैं:

Doctrine transitive persistence cascade operations

+0

दुर्भाग्य से जोड़ने झरना = { "जारी रहती है", "हटाने"} या यहाँ तक कि झरना = { "सभी"} कुछ भी नहीं बदलता है :( – loostro

+0

यह अजीब बात है। मुझे कुछ दिन पहले एक ही समस्या थी और यह मेरे लिए तय हो गया। अगर मैं कुछ और सोचता हूं तो मैं वापस आऊंगा – user1207727

+0

ठीक है मुझे पता है कि डबल फ्लश क्यों काम करता है। मैं "प्रीपेर्सिस्ट" में नई संस्थाएं बना रहा हूं "जिसका अर्थ यह है कि सिद्धांत ओआरएम पहले ही गणना कर रहा है कि कौन सी संस्थाओं को बनाए रखा जाना चाहिए। इस प्रकार, पहली बार फ्लश करने पर कॉल करना - केवल ALBUM ही जारी रहता है। इसे कॉल करते समय दूसरी बार सिद्धांत नई संस्थाओं को कायम रखने के लिए देखता है और यही कारण है कि यह उन्हें बचाता है 2 फ्लश के बाद डेटाबेस। – loostro

संबंधित मुद्दे