/** * * @param RecordWrapper $model */ public function __construct($model) { $this->table = $model->getTable(); $this->name = Nibii::getModelName((new \ReflectionClass($model))->getName()); $relationships = $model->getRelationships(); $adapter = DriverAdapter::getDefaultInstance(); $schema = Db::getDriver()->describeTable($this->table)[$this->table]; $this->autoPrimaryKey = $schema['auto_increment']; foreach ($schema['columns'] as $field => $details) { $this->fields[$field] = ['type' => $adapter->mapDataTypes($details['type']), 'required' => !$details['nulls'], 'default' => $details['default'], 'name' => $field]; if (isset($details['default'])) { $this->fields[$field]['default'] = $details['default']; } if (isset($details['length'])) { $this->fields[$field]['length'] = $details['length']; } } $this->appendConstraints($schema['primary_key'], $this->primaryKey, true); $this->appendConstraints($schema['unique_keys'], $this->uniqueKeys); foreach ($relationships as $type => $relations) { $this->createRelationships($type, $relations); } }
/** * * @return \ntentan\atiaa\Driver */ public function getDriver() { $driver = \ntentan\atiaa\Db::getConnection(array('driver' => getenv('ATIAA_DRIVER'), 'host' => getenv('ATIAA_HOST'), 'user' => getenv('ATIAA_USER'), 'password' => getenv('ATIAA_PASSWORD'), 'file' => getenv('ATIAA_FILE'), 'dbname' => getenv("ATIAA_DBNAME"))); return $driver; }
public static function init($namespace) { self::$namespace = $namespace; self::$prefix = Config::get('app.prefix'); self::$prefix = (self::$prefix == '' ? '' : '/') . self::$prefix; self::setupAutoloader(); logger\Logger::init('logs/app.log'); Config::readPath(self::$configPath, 'ntentan'); kaikai\Cache::init(); panie\InjectionContainer::bind(ModelClassResolverInterface::class)->to(ClassNameResolver::class); panie\InjectionContainer::bind(ModelJoinerInterface::class)->to(ClassNameResolver::class); panie\InjectionContainer::bind(TableNameResolverInterface::class)->to(nibii\Resolver::class); panie\InjectionContainer::bind(ComponentResolverInterface::class)->to(ClassNameResolver::class); panie\InjectionContainer::bind(ControllerClassResolverInterface::class)->to(ClassNameResolver::class); panie\InjectionContainer::bind(controllers\RouterInterface::class)->to(DefaultRouter::class); if (Config::get('ntentan:db.driver')) { panie\InjectionContainer::bind(DriverAdapter::class)->to(Resolver::getDriverAdapterClassName()); panie\InjectionContainer::bind(atiaa\Driver::class)->to(atiaa\Db::getDefaultDriverClassName()); } Controller::setComponentResolverParameters(['type' => 'component', 'namespaces' => [$namespace, 'controllers\\components']]); nibii\RecordWrapper::setComponentResolverParameters(['type' => 'behaviour', 'namespaces' => [$namespace, 'nibii\\behaviours']]); controllers\ModelBinderRegister::setDefaultBinderClass(controllers\model_binders\DefaultModelBinder::class); controllers\ModelBinderRegister::register(utils\filesystem\UploadedFile::class, controllers\model_binders\UploadedFileBinder::class); }
public function __construct($config) { $this->connection = \ntentan\atiaa\Db::getConnection($config); }
public function tearDown() { \ntentan\atiaa\Db::reset(); \ntentan\panie\InjectionContainer::resetSingletons(); }
/** * * @return \ntentan\nibii\QueryEngine */ private function getQueryEngine() { if ($this->queryEngine === null) { $this->queryEngine = new QueryEngine(); $this->queryEngine->setDriver(Db::getDriver()); } return $this->queryEngine; }
/** * Save an individual record. * * @param array $record The record to be saved * @param type $primaryKey The primary keys of the record * @return boolean */ private function saveRecord(&$record, $primaryKey) { $status = ['success' => true, 'pk_assigned' => null, 'invalid_fields' => []]; // Determine if the primary key of the record is set. $pkSet = $this->isPrimaryKeySet($primaryKey, $record); // Reset the data in the model to contain only the data to be saved $this->wrapper->setData($record); // Run preUpdate or preSave callbacks on models and behaviours if ($pkSet) { $this->wrapper->preUpdateCallback(); $record = $this->wrapper->getData(); $record = reset($record) === false ? [] : reset($record); $record = $this->runBehaviours('preUpdateCallback', [$record]); } else { $this->wrapper->preSaveCallback(); $record = $this->wrapper->getData(); $record = reset($record) === false ? [] : reset($record); $record = $this->runBehaviours('preSaveCallback', [$record]); } // Validate the data $validity = $this->validate($record, $pkSet ? DataOperations::MODE_UPDATE : DataOperations::MODE_SAVE); // Exit if data is invalid if ($validity !== true) { $status['invalid_fields'] = $validity; $status['success'] = false; return $status; } // Assign the data to the wrapper again $this->wrapper->setData($record); // Update or save the data and run post callbacks if ($pkSet) { $this->adapter->update($record); $this->wrapper->postUpdateCallback(); $this->runBehaviours('postUpdateCallback', [$record]); } else { $this->adapter->insert($record); $keyValue = Db::getDriver()->getLastInsertId(); $this->wrapper->{$primaryKey[0]} = $keyValue; $this->wrapper->postSaveCallback($keyValue); $this->runBehaviours('postSaveCallback', [$record, $keyValue]); } // Reset the data so it contains any modifications made by callbacks $record = $this->wrapper->getData()[0]; return $status; }
<?php /* * The MIT License * * Copyright 2016 ekow. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ ntentan\config\Config::set('ntentan:db', ['driver' => getenv('NIBII_DATASTORE'), 'host' => getenv('NIBII_HOST'), 'user' => getenv('NIBII_USER'), 'password' => getenv('NIBII_PASSWORD'), 'file' => getenv('NIBII_FILE'), 'dbname' => getenv("NIBII_DBNAME")]); \ntentan\kaikai\Cache::init(); ntentan\nibii\Nibii::setupDefaultBindings(); ntentan\panie\InjectionContainer::bind(ntentan\nibii\DriverAdapter::class)->to(\ntentan\nibii\Resolver::getDriverAdapterClassName()); ntentan\panie\InjectionContainer::bind(\ntentan\atiaa\Driver::class)->to(\ntentan\atiaa\Db::getDefaultDriverClassName());
public function doDelete() { Db::getDriver()->beginTransaction(); $parameters = $this->getQueryParameters(false); if ($parameters === null) { $primaryKey = $this->wrapper->getDescription()->getPrimaryKey(); $parameters = $this->getQueryParameters(); $data = $this->wrapper->getData(); $keys = []; foreach ($data as $datum) { if ($this->dataOperations->isItemDeletable($primaryKey, $datum)) { $keys[] = $datum[$primaryKey[0]]; } } $parameters->addFilter($primaryKey[0], $keys); $this->adapter->delete($parameters); } else { $this->adapter->delete($parameters); } Db::getDriver()->commit(); $this->resetQueryParameters(); }