/** * Creates a EntityArray from a select statemet result * @param array $parameters * @return \Pvik\Database\ORM\EntityArray */ public function fillEntityArray($result) { $list = new \Pvik\Database\ORM\EntityArray(); $list->setModelTable($this); while ($data = Manager::getInstance()->fetchAssoc($result)) { $classname = $this->getEntityClassName(); $model = new $classname(); $model->fill($data); $list->append($model); } return $list; }
/** * Executes the select query * @return \Pvik\Database\ORM\EntityArray */ public function select() { $statement = $this->getStatement(); $result = \Pvik\Database\SQL\Manager::getInstance()->executeStatement($statement); return $this->modelTable->fillEntityArray($result); }
/** * Execute the delete query * @return type */ public function execute() { $statement = $this->getStatement(); return \Pvik\Database\SQL\Manager::getInstance()->executeStatement($statement); }
/** * Inserts an entity to the database. * @return string primary key */ public function insert() { $primaryKey = $this->getPrimaryKey(); if (empty($primaryKey)) { $insertBuilder = Query\Builder\Insert::getEmptyInstance($this->getModelTableName()); $helper = $this->getModelTable()->getFieldDefinitionHelper(); foreach ($helper->getFieldList() as $fieldName) { switch ($helper->getFieldType($fieldName)) { case FieldDefinition\Type::NORMAL: case FieldDefinition\Type::FOREIGN_KEY: $insertBuilder->set($fieldName, $this->getFieldData($fieldName)); break; case FieldDefinition\Type::PRIMARY_KEY: // only insert a value if it is a guid otherwise ignore // the primarykey will be set on the database if ($helper->isGuid($fieldName)) { $insertBuilder->set($fieldName, Core::createGuid()); } break; } } $insertBuilder->execute(); $primaryKey = \Pvik\Database\SQL\Manager::getInstance()->getLastInsertedId(); // update primarykey in object $this->setFieldData($this->getPrimaryKeyName(), $primaryKey); $this->getModelTable()->getCache()->insert($this); return $primaryKey; } else { throw new \Exception('The primarykey of this object is already set and the object can\'t be inserted.'); } }