Php 如何通过实体正确初始化惰性集合?

Php 如何通过实体正确初始化惰性集合?,php,doctrine-orm,Php,Doctrine Orm,我需要通过自己使用实体来初始化集合。我的意思是,我可以用Java实现,如下所示: 我将从StatelesBean类调用该方法 那么,我如何用php的方式来做呢?如果有人能写示例代码,我将不胜感激 @Transient public void initialize(Collection collection, int levelCursor, int level) { if (collection instanceof PersistentBag) { if (Ob

我需要通过自己使用实体来初始化集合。我的意思是,我可以用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();
    }
}
@Transient
公共void初始化(集合集合、int-levelCursor、int-level)
{
if(PersistentBag的收集实例)
{
if(ObjectUtil.isNull(((PersistentBag)集合).getSession())
返回;
其他的
{
迭代器itr=((集合)集合).Iterator();
while(itr.hasNext())
{
if(levelCursor
那么,据我所知,您的任务是在检索实体集合的对象时初始化它们

当你需要时,条令会自动加载你的收藏

因此,您可以使用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;
        }
    }

你能再解释一下你想做什么吗?顺便说一句,我不确定Java示例在这里是否有用。一些PHP上下文可能更有用。假设其中有用户实体和ownedCars arrayCollection。而且fetchType是惰性的,所以当我获取用户实体记录时,我可能希望访问ownedCars集合。我想初始化它们,比如user.initialize(1){1是deepLevel}或user.initialize(user.getOwnedCars())谢谢,是的,这是一种解决延迟初始化的方法。但实际上,我想用反射的方式来解决这个问题,因为我不想写出所有具有arrayCollection的实体。我更喜欢我在问题中提到的通用解决方案,比如MappedSuperClass上的方法包含反射。它是在目标实体中实现magic方法的选项吗?将对象反序列化时将调用它。