/** * 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; }
/** * 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.'); } }
/** * 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); }
public function SetPostShares($UserIds) { \Pvik\Database\SQL\Manager::GetInstance()->DeleteWithParameters('DELETE FROM PostShares WHERE PostShares.PostId = %s', array($this->PostId)); // CAUTION: PostShares in the cache maybe still have a reference to this object // manually delete cache posts $CachePostShares = \Pvik\Database\Generic\ModelTable::Get('PostShares')->GetCache()->GetAllCacheInstances(); foreach ($CachePostShares as $CachePostShare) { /* @var $CachePostShare PostShare */ if ($CachePostShare->PostId == $this->PostId) { \Pvik\Database\Generic\ModelTable::Get('PostShares')->GetCache()->Delete($CachePostShare); } } // clear all reference keys $this->SetFieldData('PostShares', null); // validate userIds $UserShares = \Dashbird\Library\Services\UserService::Instance()->GetUser()->UserShares; $FilteredUserIds = array(); foreach ($UserIds as $UserId) { if ($UserShares->HasValue('ConnectedUserId', $UserId) && !in_array($UserId, $FilteredUserIds)) { $FilteredUserIds[] = $UserId; } } $UserIds = $FilteredUserIds; $UserIds[] = $this->UserId; // add him self to shared foreach ($UserIds as $UserId) { $PostShare = new \Dashbird\Model\Entities\PostShare(); $PostShare->PostId = $this->PostId; $PostShare->UserId = $UserId; $PostShare->Insert(); } }