将 eloquent 模型包装到另一个 class 导致超出嵌套

Wrapping eloquent model to another class causes exceeded nesting

我正在使用 Laravel 5.5。我写了一个包装器,它采用 Eloquent 模型并将其包装到 Entity class 并且每个模型都有自己的包装器。假设,用户有很多产品,一个产品属于一个用户。包装时,我需要获取用户的产品并将它们传递给产品包装器以将它们包装到产品实体中。在产品包装器中,我需要让该产品的用户所有者将其包装到用户实体。再次强调,在用户包装器中,我需要用户产品!,这会造成无限循环。

实体包装器:

abstract class EntityWrapper
{
    protected $collection;
    protected $entityClass;
    public $entity;

    public function __construct($collection)
    {
        $this->collection = $collection;
        $this->entity = $this->buildEntity();
    }

    protected function buildEntity()
    {
        $tempEntity = new $this->entityClass;

        $Entities = collect([]);

        foreach ($this->collection as $model) {
            $Entities->push($this->makeEntity($tempEntity, $model));
        }

        return $Entities;
    }

    abstract protected function makeEntity($entity, $model);
}

UserEntityWrapper:

class UserEntityWrapper extends EntityWrapper
{
    protected $entityClass = UserEntity::class;

    protected function makeEntity($userEntity, $model)
    {
        $userEntity->setId($model->user_id);
        $userEntity->setName($model->name);

        // set other properties of user entity...

        //--------------- relations -----------------
        $userEntity->setProducts((new ProductEntityWrapper($model->products))->entity);

        return $userEntity;
    }
}

ProductEntityWrapper:

class ProductEntityWrapper extends EntityWrapper
{
    protected $entityClass = ProductEntity::class;

    protected function makeEntity($productEntity, $model)
    {
        $productEntity->setId($model->product_id);
        $productEntity->setName($model->name);

        // set other properties of product entity...

        //--------------- relations -----------------
        $productEntity->setUser((new UserEntityWrapper($model->user))->entity);

        return $productEntity;
    }
}

用户实体:

class UserEntity
{
    private $id;
    private $name;
    private $products;
    //... other properties

    public function setProducts($products)
    {
         $this->products = $products;
    }

    // other getters and setters...
}

当我想通过调用 (new UserEntityWrapper(User::all()))->entity 来获取用户实体时,它会导致无限循环。那么,如何防止对模型之间关系的嵌套调用呢?感谢任何建议。

我终于找到了解决办法。在每个包装器 class 中,我使用动态 属性 来获取关系集合,除了强加额外的查询外,这会导致延迟加载。因此,在将模型集合传递给每个包装器之前,检索必要的关系模型,每个包装器首先使用方法 getRelations()(即 returns 可用关系数组)检查关系是否存在。如果预期的关系可用,关系模型的集合将传递到适当的包装器 class.

UserEntityWrapper:

class UserEntityWrapper extends EntityWrapper
{
    protected $entityClass = UserEntity::class;

    protected function makeEntity($userEntity, $model)
    {
        $userEntity->setId($model->user_id);
        $userEntity->setName($model->name);

        // set other properties of user entity...

        //--------------- relations -----------------
        $relations = $model->getRelations();

        $products = $relations['products'] ?? null;
        if ($products) {
            $userEntity->setProducts((new ProductEntityWrapper($products))->entity);
        }

        return $userEntity;
    }
}

并且,类似的功能也用于其他包装器。