public function persist(IEntity $entity, $recursive = TRUE)
{
$this->identityMap->check($entity);
if (isset($this->isPersisting[spl_object_hash($entity)])) {
return $entity;
}
$this->isPersisting[spl_object_hash($entity)] = TRUE;
$this->attach($entity);
$relationships = [];
if ($recursive) {
foreach ($entity->toArray(IEntity::TO_ARRAY_LOADED_RELATIONSHIP_AS_IS) as $k => $v) {
if ($v instanceof IEntity) {
$this->model->getRepositoryForEntity($v)->persist($v);
} elseif ($v instanceof IRelationshipCollection) {
$relationships[] = $v;
}
}
}
if ($entity->isModified()) {
$id = $this->mapper->persist($entity);
$entity->fireEvent('onPersist', [$id]);
}
foreach ($relationships as $relationship) {
$relationship->persist($recursive);
}
unset($this->isPersisting[spl_object_hash($entity)]);
return $entity;
}