/** * @param string $username * @param string $email * @param string $fullname * @param string $password clear text * @param int $hashCost * @return User */ public static function create($username, $email, $fullname, $password, $hashCost = USecurity::HASHING_COST) { $passwordHash = USecurity::encryptPassword($password, $hashCost); Aelix::db()->prepare('INSERT INTO `user` SET `username` = :username, `email` = :email, `passwordHash` = :passwordHash, `fullname` = :fullname')->execute([':username' => $username, ':email' => $email, ':passwordHash' => $passwordHash, ':fullname' => $fullname]); $userID = Aelix::db()->getPDO()->lastInsertId('user'); $user = new User(['id' => $userID, 'username' => $username, 'email' => $email, 'passwordHash' => $passwordHash, 'fullname' => $fullname]); self::$userByID[$userID] = $user; return $user; }
/** * @param $name * @param $value * @return ConfigNode * @throws ConfigNodeAlreadyExistsException */ public function add($name, $value) { if (isset($this->nodes[$name])) { throw new ConfigNodeAlreadyExistsException('Config node ' . $name . ' already exists'); } $stmt = Aelix::db()->prepare('INSERT INTO `' . $this->tableName . '` SET `name` = :name, `value` = :value'); $stmt->execute([':name' => $name, ':value' => serialize($value)]); $node = new ConfigNode($this, Aelix::db()->getPDO()->lastInsertId($this->tableName), $name, $value); $this->nodes[$name] = $node; return $node; }
<?php /** * @author aelix framework <info@aelix framework.org> * @copyright Copyright (c) 2015 aelix framework * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 */ /* * init aelix */ define('DS', DIRECTORY_SEPARATOR); define('DIR_START', dirname(__FILE__) . DS); // from where we started the execution // only init aelix (until database) // we need the DB for phinx define('AELIX_ONLY_INIT', true); require 'src' . DS . 'bootstrap.php'; /* * aelix done, generate phinx config */ use aelix\framework\Aelix; // return phinx config array return ['paths' => ['migrations' => DIR_ROOT . 'migrations'], 'environments' => ['default_migration_table' => 'phinxlog', 'default_database' => 'aelix', 'aelix' => ['name' => Aelix::db()->getDatabaseName(), 'connection' => Aelix::db()->getPDO()]]];
/** * creates a new field. if already exists, returns the existing * @param string $fieldName * @return UserDataField */ public static function createField($fieldName) { // is already cached? if (isset(self::$fields[$fieldName]) && self::$fields[$fieldName] instanceof UserDataField) { return self::$fields[$fieldName]; } // find in DB $rowCount = Aelix::db()->prepare('SELECT * FROM `user_data_field` WHERE `fieldName` = :fieldName')->execute([':fieldName' => $fieldName])->rowCount(); // field exists, return it if ($rowCount > 0) { return self::getField($fieldName); } // create a new one Aelix::db()->prepare('INSERT INTO `user_data_field` SET `fieldName` = :fieldName')->execute([':fieldName' => $fieldName]); $fieldID = Aelix::db()->getPDO()->lastInsertId('user_data_field'); $obj = new UserDataField($fieldID, $fieldName); self::$fields[$fieldName] = $obj; return $obj; }
/** * @param mixed $value */ public function setValue($value) { $this->value = $value; $stmt = Aelix::db()->prepare('UPDATE `' . $this->config->getTableName() . '` SET `value` = :value WHERE `id` = :id'); $stmt->execute([':id' => $this->id, ':value' => serialize($this->value)]); }
/** * @param UserDataField $field * @param User $user * @param mixed $value * @return UserData * @throws \InvalidArgumentException */ public static function create(UserDataField $field, User $user, $value) { // check if field is already set for user if ($user->getData($field->getName()) !== null) { throw new \InvalidArgumentException('User\'s data field ' . $field->getName() . ' already set.'); } $value = serialize($value); Aelix::db()->prepare('INSERT INTO `user_data` SET `userID` = :userID, `fieldID` = :fieldID, `value` = :value')->execute([':userID' => $user->getID(), ':fieldID' => $field->getID(), ':value' => $value]); $dataID = Aelix::db()->getPDO()->lastInsertId('user_data'); return new UserData($user, $field, $dataID, $value); }