我需要通過使用實體自行初始化集合。我的意思是,我可以在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();
}
}
能否請您解釋一下更多你想要做什麼?順便說一句,我不確定Java示例在這裏有幫助。一些PHP上下文會更有用。 – lxg 2014-09-03 21:47:09
假設我們有User實體和ownCars arrayCollection。而fetchType是懶惰,所以當我得到用戶實體記錄我可能想要達到ownedCars集合。我想初始化它們,如user.initialize(1){1是deepLevel}或user.initialize(user.getOwnedCars()) – webyildirim 2014-09-04 07:15:03