2014-09-03 62 views
0

我需要通過使用實體自行初始化集合。我的意思是,我可以在Java中做到這一點,例如: 我會從StatelesBean類中調用方法。如何正確初始化通過實體的懶惰收集?

那麼,我怎麼能在PHP的方式呢?如果有人可以寫樣本代碼,我將不勝感激。

@Transient 
public void initialize(Collection collection, int levelCursor, int level) 
{ 
    if (collection instanceof PersistentBag) 
    { 
     if (ObjectUtil.isNull(((PersistentBag)collection).getSession())) 
      return; 
     else 
     { 
      Iterator itr = ((Collection)collection).iterator(); 
      while (itr.hasNext()) 
      { 
       if (levelCursor < level) 
        ((SuperEntity)itr.next()).initialize(levelCursor, level); 
       else 
        itr.next(); 
      } 
     } 
    } else 
    { 
     Iterator itr = ((Collection)collection).iterator(); 
     while (itr.hasNext()) 
     { 
      if (levelCursor < level) 
       ((SuperEntity)itr.next()).initialize(levelCursor, level); 
      else 
       itr.next(); 
     } 
    } 
} 

/** 
* Searches for column and join column annotations for getter methods. 
* If found then tries to initialize childs 
* @param levelCursor 
* @param level 
*/ 
@Transient 
public void initialize(int levelCursor, int level) 
{ 
    levelCursor++; 

    Method[] methods = this.getClass().getMethods(); 

    Object obj = null; 

    try 
    { 
     for (Method method : methods) 
     { 
      if (method.getAnnotation(JoinColumn.class) != null || method.getAnnotation(JoinTable.class) != null || method.getAnnotation(OneToMany.class) != null) 
      { 
       Object result = method.invoke(this, new Object[0]); 
       if (result == null) 
        continue; 

       if (result instanceof SuperEntity) 
       { 
        if (levelCursor < level) 
         ((SuperEntity)result).initialize(levelCursor, level); 
       } else if (result instanceof Collection) 
        initialize((Collection)result, levelCursor, level); 
      } 
     } 
    } 
    catch (Exception exc) 
    { 
     exc.printStackTrace(); 
    } 
} 
+0

能否請您解釋一下更多你想要做什麼?順便說一句,我不確定Java示例在這裏有幫助。一些PHP上下文會更有用。 – lxg 2014-09-03 21:47:09

+0

假設我們有User實體和ownCars arrayCollection。而fetchType是懶惰,所以當我得到用戶實體記錄我可能想要達到ownedCars集合。我想初始化它們,如user.initialize(1){1是deepLevel}或user.initialize(user.getOwnedCars()) – webyildirim 2014-09-04 07:15:03

回答

1

所以,據我瞭解你的任務,你想初始化你的實體集合的對象,當你檢索它們?

當你要求它的時候,主義加載你的收藏automagicaly。

所以,你可以用一個getter做到這一點:

class User 
    { 

     /** 
     * OneToMany(targetEntity="Car", mappedBy="owner") 
     */ 
     private $ownedCars; 

     public function __construct() 
     { 
      $this->ownedCars = new ArrayCollection(); 
     } 

     public function getOwnedCars($level) 
     { 
      // autoload collection 
      foreach($this->ownedCars as $ownedCar) 
      { 
       $ownedCar->initialize($level); 
      } 

      return $this->ownedCars; 
     } 
    } 
+0

謝謝,是的,這是一種解決延遲初始化的方法。但實際上,我想用反射的方式來解決這個問題,因爲我不想寫所有具有arrayCollection的實體。我更喜歡通用解決方案,正如我在我的問題中提到的,例如MappedSuperClass上的方法包含反射。 – webyildirim 2014-09-04 12:39:21

+0

是否可以在目標實體中實現魔術[__wakeup()](http://php.net/manual/en/language.oop5.magic.php#object.wakeup)方法?當學說將對象反序列化時,它會被調用。 – Mischa 2014-09-04 15:03:33

相關問題