Nextras\Orm\Repository\PersistenceHelper::visitEntity PHP Method

visitEntity() protected static method

protected static visitEntity ( Nextras\Orm\Entity\IEntity $entity, Nextras\Orm\Model\IModel $model, $withCascade = true )
$entity Nextras\Orm\Entity\IEntity
$model Nextras\Orm\Model\IModel
    protected static function visitEntity(IEntity $entity, IModel $model, $withCascade = true)
    {
        $entityHash = spl_object_hash($entity);
        if (isset(self::$outputQueue[$entityHash])) {
            if (self::$outputQueue[$entityHash] === true) {
                $cycle = [];
                $bt = debug_backtrace();
                foreach ($bt as $item) {
                    if ($item['function'] === 'getCascadeQueue') {
                        break;
                    } elseif ($item['function'] === 'addRelationshipToQueue') {
                        $cycle[] = get_class($item['args'][0]) . '::$' . $item['args'][1]->name;
                    }
                }
                throw new InvalidStateException('Persist cycle detected in ' . implode(' - ', $cycle) . '. Use manual two phase persist.');
            }
            return;
        }
        $repository = $model->getRepositoryForEntity($entity);
        $repository->attach($entity);
        $repository->doFireEvent($entity, 'onBeforePersist');
        if ($withCascade) {
            self::$outputQueue[$entityHash] = true;
            foreach ($entity->getMetadata()->getProperties() as $propertyMeta) {
                if ($propertyMeta->relationship !== null && $propertyMeta->relationship->cascade['persist']) {
                    self::addRelationshipToQueue($entity, $propertyMeta, $model);
                }
            }
            unset(self::$outputQueue[$entityHash]);
            // reenqueue
        }
        self::$outputQueue[$entityHash] = $entity;
    }