/** * @covers \Asymptix\core\DB\DBSelector::__call * @todo Implement test__call(). */ public function testMagic_AllDBObjects() { $selector = new DBSelector(new User()); $users = $selector->selectAllUsers(); $this->assertTrue(is_array($users)); }
<?php /** * Settings module. Gets system settings and configuration from the database. * * @category Asymptix PHP Framework * @author Dmytro Zarezenko <*****@*****.**> * @copyright (c) 2009 - 2015, Dmytro Zarezenko * * @git https://github.com/Asymptix/Framework * @license http://opensource.org/licenses/MIT */ use Asymptix\db\DBSelector; use db\Settings; $dbSelector = new DBSelector(new Settings()); $settings = $dbSelector->selectDBObjects(); $_SETTINGS = array(); foreach ($settings as $setting) { $_SETTINGS[$setting->id] = $setting->value; }
<?php /** * Session module and manipulations with User DBObject in session. * * @category Asymptix PHP Framework * @author Dmytro Zarezenko <*****@*****.**> * @copyright (c) 2009 - 2015, Dmytro Zarezenko * * @git https://github.com/Asymptix/Framework * @license http://opensource.org/licenses/MIT */ use Asymptix\db\DBSelector; use db\access\User; $_USER = null; if (isset($_SESSION['user'])) { $_USER = unserialize($_SESSION['user']); $userSelector = new DBSelector(new User()); $_USER = $userSelector->selectDBObjectById($_USER->id); $user = clone $_USER; $user->password = null; //TODO: clear other secured data from session $_SESSION['user'] = serialize($user); unset($user); }
/** * Calls DBCore magic methods like: * get[User]By[Id]($userId) * get[User]By[Email]($email) * get[Users]() * delete[Users]($ids) * delete[User]($userId) * set[User]Activation($activationFieldName, $flagValue) * * @param string $methodName Name of the magic method. * @param array $methodParams List of dynamic parameters. * * @return mixed * @throws DBCoreException */ public function __call($methodName, $methodParams) { if (strrpos($methodName, "ies") == strlen($methodName) - 3) { $methodName = substr($methodName, 0, strlen($methodName) - 3) . "ys"; } /** * Get database record object by Id */ if (preg_match("#get([a-zA-Z]+)ById#", $methodName, $matches)) { $dbSelector = new DBSelector($matches[1]); return $dbSelector->selectDBObjectById($methodParams[0]); } /** * Get database record object by some field value */ if (preg_match("#get([a-zA-Z]+)By([a-zA-Z]+)#", $methodName, $matches)) { if (empty($methodParams[0])) { return null; } $dbSelector = new DBSelector($matches[1]); $fieldName = substr(strtolower(preg_replace("#([A-Z]{1})#", "_\$1", $matches[2])), 1); return $dbSelector->selectDBObjectByField($fieldName, $methodParams[0]); } /** * Get all database records */ if (preg_match("#get([a-zA-Z]+)s#", $methodName, $matches)) { return self::Selector()->selectDBObjects(); } /** * Delete selected records from the database */ if (preg_match("#delete([a-zA-Z]+)s#", $methodName, $matches)) { $className = $matches[1]; $idsList = $methodParams[0]; $idsList = array_filter($idsList, "isInteger"); if (!empty($idsList)) { $itemsNumber = count($idsList); $types = DBPreparedQuery::sqlSingleTypeString("i", $itemsNumber); $dbObject = new $className(); if (!isInstanceOf($dbObject, $className)) { throw new DBCoreException("Class with name '" . $className . "' is not exists"); } $query = "DELETE FROM " . $dbObject->getTableName() . "\n WHERE " . $dbObject->getIdFieldName() . "\n IN (" . DBPreparedQuery::sqlQMString($itemsNumber) . ")"; return self::doUpdateQuery($query, $types, $idsList); } return 0; } /** * Delete selected record from the database */ if (preg_match("#delete([a-zA-Z]+)#", $methodName, $matches)) { return call_user_func(array(self::Instance(), $methodName . "s"), array($methodParams[0])); } /** * Set activation value of selected records */ if (preg_match("#set([a-zA-Z]+)Activation#", $methodName, $matches)) { $className = $matches[1]; if (strrpos($className, "ies") == strlen($className) - 3) { $className = substr($className, 0, strlen($className) - 3) . "y"; } else { $className = substr($className, 0, strlen($className) - 1); } $idsList = $methodParams[0]; $activationFieldName = $methodParams[1]; $activationValue = $methodParams[2]; if (empty($activationFieldName)) { throw new DBCoreException("Invalid activation field name"); } $idsList = array_filter($idsList, "isInteger"); if (!empty($idsList)) { $itemsNumber = count($idsList); $types = DBPreparedQuery::sqlSingleTypeString("i", $itemsNumber); $dbObject = new $className(); if (!isInstanceOf($dbObject, $className)) { throw new DBCoreException("Class with name '" . $className . "' is not exists"); } $query = "UPDATE " . $dbObject->getTableName() . " SET `" . $activationFieldName . "` = '" . $activationValue . "'\n WHERE " . $dbObject->getIdFieldName() . " IN (" . DBPreparedQuery::sqlQMString($itemsNumber) . ")"; return self::doUpdateQuery($query, $types, $idsList); } } throw new DBCoreException('No such method "' . $methodName . '"'); }