Create the added models.

@param  bool $persistLastModel Whether to persist the last added model in the database.
@return array The inserted models or collections indexed by class name.
559696
public function execute($persistLastModel = true)
    {
        $this->addLastModel();

        $insertedPKs = [];
        $createdModels = [];

        foreach ($this->quantities as $modelClass => $quantity) {
            $this->modelPopulators[$modelClass]->setGuessedColumnFormatters(false);

            $persist = $modelClass === $this->lastModelClass ? $persistLastModel : true;

            if ($quantity > 1) {
                $createdModels[$modelClass] = (new $modelClass)->newCollection();
            }

            for ($i = 0; $i < $quantity; $i++) {
                $createdModel = $this->modelPopulators[$modelClass]->run($insertedPKs, $persist);

                $insertedPKs[$modelClass][] = $createdModel->getKey();

                if ($quantity === 1) {
                    $createdModels[$modelClass] = $createdModel;
                } else {
                    $createdModels[$modelClass]->add($createdModel);
                }
            }
        }

        // We'll unset the previously added models so that if, for example, the developer calls
        // $populator->create(App\Foo::class), and then $populator->create(App\Bar::class),
        // Foo won't be created twice.
        $this->forgetAddedModels();

        $this->closeDoctrineConnections();

        return $createdModels;
    }
86137
public function create(array $data = null, $persist=false)
    {
        $entity = parent::create($data);
        
        $eventArgs = new LifecycleEventArgs($entity, $this->dm);
        $this->dm->getEventManager()->dispatchEvent(
            Events::postLoad,
            $eventArgs
        );
        return $entity;
    }
315191
public function generateAlteredMeta()
    {
        //get unmanaged converted to managed
        $managed = array_intersect(
            $this->oldUnmanagedKeys,
            $this->newModelKeys
        );

        //get managed converted to unmanaged
        $unmanaged = array_intersect(
            $this->oldModelKeys,
            $this->newUnmanagedKeys
        );

        $modelsToCheck = array_merge(
            $this->keptProxyKeys,
            $this->keptUnmanagedKeys,
            $managed,
            $unmanaged
        );

        $modelsToCheck = array_unique($modelsToCheck);

        /* @var $oldState ModelState */
        /* @var $newState ModelState */
        foreach ($modelsToCheck as $modelName) {
            $oldModelName = $this->getOldModelName($modelName);
            $oldState = $this->fromState->getModelState($oldModelName);
            $newState = $this->toState->getModelState($modelName);

            $oldMeta = [];

            if ($oldState->getMeta()) {
                foreach ($oldState->getMeta() as $name => $opt) {
                    if (AlterModelMeta::isAlterableOption($name)) {
                        $oldMeta[$name] = $opt;
                    }
                }
            }

            $newMeta = [];
            if ($newState->getMeta()) {
                foreach ($newState->getMeta() as $name => $opt) {
                    if (AlterModelMeta::isAlterableOption($name)) {
                        $newMeta[$name] = $opt;
                    }
                }
            }

            if ($oldMeta !== $newMeta) {
                $this->addOperation(
                    $newMeta->getAppName(),
                    AlterModelMeta::createObject(
                        [
                            'name' => $modelName,
                            'meta' => $newMeta,
                        ]
                    )
                );
            }
        }
    }
1219601
public function create()
    {
        $sm = $this->em->getConnection()->getSchemaManager();
        $table = $this->em->getClassMetadata(self::$entityName)->getTableName();

        return $sm->tablesExist(array($table));
    }
447608
public function persistAllObjects(PersistenceContext $context, array $objects) : array
    {
        $rows = [];

        foreach ($objects as $key => $object) {
            $rows[$key] = new Row($this->table);
        }

        $this->persistAllBeforeParent($context, $objects, $rows);
        $this->persistAll($context, $objects, $rows);
        $this->persistAllAfterParent($context, $objects, $rows);

        return $rows;
    }
93077
function getModels(done) {
  if (models) {
    return done(models);
  }

  return orm.initialize(ormConfig, function(err, m) {
    if (err) {
      throw err;
    }

    // make the models available as soon as possible for other functions
    models = m;

    // ignore error if groups already created
    let catcher = (createErr) => { if (createErr && createErr.code !== "E_VALIDATION") { throw createErr; } };

    // create the administrators group
    createGroup("admin", catcher);

    // create the public group
    createGroup("public", catcher);

    return done(models);
  });
}
1095360
function setModelPersist(MongoDB\BSON\Persistable $persistable)
    {
        // reset _query collection
        $this->_q = null;
        
        $this->typeMap['root'] = get_class($persistable);
        $this->persist = $this->typeMap['root'];
        return $this;
    }
1147741
public function generateInsertForModel(AbstractModel $model)
    {
        $cacheData = $this->fetchCacheData($model);

        $strategy = new MySQLInsertStrategy(
            $cacheData[self::TABLE_DATA_INDEX],
            $this->prepareModelData(
                $model,
                $cacheData[self::TABLE_DATA_INDEX]
            )
        );

        $strategy->setTransformer($this->paramTransformer);


        return [
            $strategy->generateSQLStatement(),
            $this->prepareModelDataForStatement($strategy->getModelData()),
            self::INSERT_QUERY
        ];
    }
713444
public function persistAllObjects(PersistenceContext $context, array $objects) : array
    {
        $rows = [];

        foreach ($objects as $key => $object) {
            $rows[$key] = new Row($this->table);
        }

        $this->persistAll($context, $objects, $rows);

        return $rows;
    }
417855
public function lastInsertID()
    {
        if (isset($this->persistence) && $this->persistence instanceof Persistence\SQL) {
            return $this->persistence->connection->lastInsertId($this);
        }

        throw new Exception('Model is not associated with SQL-enabled persistence');
    }
1050242
public function persist(AbstractModel $model)
    {
        $collection = $this->getCollectionBy(
            $this->getModelConfiguration(get_class($model))
        );

        $this->setLastResult(
            $collection->insert($this->getModelDataAsArray($model))
        );

        return (bool) ($this->lastResult) ? $this->lastResult[self::RESULT_OK_INDEX] : false;
    }
1102893
public static function newFromDatamapperObject(EntityContract $entity, $lastObjectId = null, $lastEloquentModel = null)
    {
        // directly get private properties if entity extends the datamapper entity class (fast!)
        if ($entity instanceof \ProAI\Datamapper\Support\Entity) {
            return $entity->toEloquentObject($lastObjectId, $lastEloquentModel);
        }

        // get private properties via reflection (slow!)
        $class = get_mapped_model(get_class($entity));

        $eloquentModel = new $class;

        $reflectionObject = new ReflectionObject($entity);

        $mapping = $eloquentModel->getMapping();

        // attributes
        foreach ($mapping['attributes'] as $attribute => $column) {
            if (! $eloquentModel->isAutomaticallyUpdatedDate($column)) {
                // get property
                $property = $eloquentModel->getProperty(
                    $reflectionObject,
                    $entity,
                    $attribute
                );

                // set attribute
                $eloquentModel->setAttribute($column, $property);
            }
        }

        // embeddeds
        foreach ($mapping['embeddeds'] as $name => $embedded) {
            $embeddedObject = $eloquentModel->getProperty($reflectionObject, $entity, $name);


            if (! empty($embeddedObject)) {
                $embeddedReflectionObject = new ReflectionObject($embeddedObject);

                foreach ($embedded['attributes'] as $attribute => $column) {
                    // get property
                    $property = $eloquentModel->getProperty(
                        $embeddedReflectionObject,
                        $embeddedObject,
                        $attribute
                    );

                    // set attribute
                    $eloquentModel->setAttribute($column, $property);
                }
            }
        }

        // relations
        foreach ($mapping['relations'] as $name => $relation) {
            $relationObject = $eloquentModel->getProperty(
                $reflectionObject,
                $entity,
                $name
            );

            if (! empty($relationObject) && ! $relationObject instanceof \ProAI\Datamapper\Contracts\Proxy) {
                // set relation
                if ($relationObject instanceof \ProAI\Datamapper\Support\Collection) {
                    $value = EloquentCollection::newFromDatamapperObject($relationObject, $this, $eloquentModel);
                } elseif (spl_object_hash($relationObject) == $lastObjectId) {
                    $value = $lastEloquentModel;
                } else {
                    $value = EloquentModel::newFromDatamapperObject($relationObject, spl_object_hash($this), $eloquentModel);
                }
                
                $eloquentModel->setRelation($name, $value);
            }
        }

        return $eloquentModel;
    }
505915
public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false)
    {
        $mapping  = $coll->getMapping();
        $pb       = $this;
        $callback = isset($mapping['embedded'])
            ? static function ($v) use ($pb, $mapping, $includeNestedCollections) {
                return $pb->prepareEmbeddedDocumentValue($mapping, $v, $includeNestedCollections);
            }
            : static function ($v) use ($pb, $mapping) {
                return $pb->prepareReferencedDocumentValue($mapping, $v);
            };

        $setData = $coll->map($callback)->toArray();
        if (CollectionHelper::isList($mapping['strategy'])) {
            $setData = array_values($setData);
        }

        $this->uow->unscheduleCollectionDeletion($coll);
        $this->uow->unscheduleCollectionUpdate($coll);

        return $setData;
    }
809371
public function save(...$args) : Collection
    {
        $collection = new Collection();

        foreach ($this->added as $current) {
            $return = $current->insert(...$args);
            $collection->add(new ModelSaved($current, $current->getChangedProperties(), $return));
        }

        foreach ($this->removed as $current) {
            $return = $current->delete(...$args);
            $collection->add(new ModelSaved($current, $current->getChangedProperties(), $return));
        }

        foreach ($this->elements as $current) {
            if (in_array($current, $this->added, true)) {
                continue;
            }

            $return = null;
            if (sizeof($current->getChangedProperties()) > 0) {
                $return = $current->update(...$args);
            }
            $collection->add(new ModelSaved($current, $current->getChangedProperties(), $return));
        }

        $this->clearHistory();

        return $collection;
    }
1186872
public function save()
    {
        $array = $this->toArrayWithoutEmptyValues();

        $query = $this->createQuery();

        $now = (new \DateTime())->format('Y-m-d H:i:s');

        if ($this->{$this->getTableKey()}) {
            $query->setAction('update');
            $query->where($query->buildAndSet([
                [$this->getTableKey(), "=", $this->id],
            ]));

            foreach ($array as $key => $value) {
                $query->addValue($key, $value);
            }

            // Since it's an update, add updated_at if the model implements it
            if (property_exists($this, 'updated_at')) {
                $query->addValue('updated_at', $now);
                $this->updated_at = $now;
            }
        } else {
            $query->setAction('insert');

            foreach ($array as $key => $value) {
                if ($key !== $this->tableKey) {
                    $query->addValue($key, $value);
                }
            }

            // Since it's an insert, add created_at if the model implements it
            if (property_exists($this, 'created_at')) {
                $query->addValue('created_at', $now);
                $this->created_at = $now;
            }
        }
        $result = $this->database->query($query);
        if ($result && $query->getAction() === 'insert') {
            $this->id = $this->database->getInstance()->lastInsertId();
        }
        return $result ? true : false;
    }
147916
public static function getAllPersistedAttributes() {
    $calledClass = get_called_class();

    if (array_key_exists($calledClass, self::$allPersisted)) {
      return self::$allPersisted[$calledClass];
    } else {
      $persisted = [];
      if(property_exists($calledClass, 'persisted')) {
        $persisted  = $calledClass::$persisted;
      }
      $parent = get_parent_class($calledClass);
      if (method_exists($parent, 'getAllPersistedAttributes')) {
        $persisted = array_merge($persisted, $parent::getAllPersistedAttributes());
      }
    }
    self::$allPersisted[$calledClass] = $persisted;
    return self::$allPersisted[$calledClass];
  }
460107
public function create(): array
    {
        $attributes = collect($this->getDataKeys())
            ->flip()
            ->map(function() {
                return null;
            })->all();

        // Build a fake Model class based on attributes
        return $this->transform(new class($attributes) extends \stdClass
        {
            public function __construct($attributes)
            {
                $this->attributes = $attributes;

                foreach($attributes as $name => $value) {
                    $this->$name = $value;
                }
            }
            public function toArray()
            {
                return $this->attributes;
            }
        });
    }