本文整理汇总了PHP中yii\db\Connection类的典型用法代码示例。如果您正苦于以下问题:PHP Connection类的具体用法?PHP Connection怎么用?PHP Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: init
public function init()
{
$this->db = Instance::ensure($this->db, Connection::className());
parent::init();
// Note the default configuration data value will not store to database.
$this->data = array_merge($this->loadData(), $this->data);
}
示例2: getConnection
/**
* @param boolean $reset whether to clean up the test database
* @param boolean $open whether to open and populate test database
* @return \yii\db\Connection
*/
public function getConnection($reset = true, $open = true)
{
if (!$reset && $this->db) {
return $this->db;
}
$db = new \yii\db\Connection();
$db->dsn = $this->database['dsn'];
if (isset($this->database['username'])) {
$db->username = $this->database['username'];
$db->password = $this->database['password'];
}
if (isset($this->database['attributes'])) {
$db->attributes = $this->database['attributes'];
}
if ($open) {
$db->open();
$lines = explode(';', file_get_contents($this->database['fixture']));
foreach ($lines as $line) {
if (trim($line) !== '') {
$db->pdo->exec($line);
}
}
}
$this->db = $db;
return $db;
}
示例3: getConnection
/**
* @param boolean $reset whether to clean up the test database
* @param boolean $open whether to open and populate test database
* @throws \yii\base\InvalidParamException
* @throws \yii\db\Exception
* @throws \yii\base\InvalidConfigException
* @return \yii\db\Connection
*/
public function getConnection($reset = true, $open = true)
{
if (!$reset && $this->db) {
return $this->db;
}
$db = new Connection();
$db->dsn = $this->database['dsn'];
if (isset($this->database['username'])) {
$db->username = $this->database['username'];
$db->password = $this->database['password'];
}
if (isset($this->database['attributes'])) {
$db->attributes = $this->database['attributes'];
}
if ($open) {
$db->open();
$lines = explode(';', file_get_contents(\Yii::getAlias('@yii/rbac/schema-' . $this->driverName . '.sql')));
foreach ($lines as $line) {
if (trim($line) !== '') {
$db->pdo->exec($line);
}
}
}
$this->db = $db;
return $db;
}
示例4: addUser
public function addUser($ldap)
{
$profile = Yii::$app->userHelpers->getUserProfileFromHr($ldap->citizen);
$profile['moo'] = empty($profile['moo']) ? '' : ' หมู่ ' . $profile['moo'];
$profile['soi'] = empty($profile['soi']) ? '' : ' ซอย ' . $profile['soi'];
$profile['street'] = empty($profile['street']) ? '' : ' ถ.' . $profile['street'];
$profile['district'] = empty($profile['district']) ? '' : ' ต.' . $profile['district'];
$profile['amphur'] = empty($profile['amphur']) ? '' : ' อ.' . $profile['amphur'];
$profile['province'] = empty($profile['province']) ? '' : ' จ.' . $profile['province'];
$profile['zipcode'] = empty($profile['zipcode']) ? '' : ' ' . $profile['zipcode'];
$user = new User();
$user->id = $profile['id'];
$user->username = $this->username;
$user->email = "{$this->username}@kku.ac.th";
$user->password_hash = Password::hash($this->password);
$user->save(false);
$connection = new Connection(Yii::$app->db);
$connection->createCommand('INSERT INTO auth_assignment VALUES(:item_name, :user_id, :created_at)', [':item_name' => 'User', ':user_id' => $profile['id'], ':created_at' => time()])->execute();
$admins = Yii::$app->getModule('user')->admins;
if (in_array($ldap->username, $admins)) {
$connection->createCommand('INSERT INTO auth_assignment VALUES(:item_name, :user_id, :created_at)', [':item_name' => 'Administrator', ':user_id' => $profile['id'], ':created_at' => time()])->execute();
}
$connection->createCommand('UPDATE profile SET
name = :name,
address = :address,
phone = :phone,
faculty_id = :faculty_id,
position_id = :position_id,
position_type_id = :position_type_id,
level_id = :level_id,
division_id = :division_id
WHERE user_id = :user_id
', [':name' => "{$profile['title']}{$profile['firstname']} {$profile['lastname']}", ':address' => "{$profile['homeadd']}{$profile['moo']}{$profile['soi']}{$profile['street']}{$profile['district']}{$profile['amphur']}{$profile['province']}{$profile['zipcode']}", ':phone' => isset($profile['telephone']) ? $profile['telephone'] : null, ':faculty_id' => isset($profile['faculty_id']) ? $profile['faculty_id'] : Yii::$app->mappingHelpers->mapFaculty($profile['faculty'])['id'], ':position_id' => isset($profile['position_id']) ? $profile['position_id'] : Yii::$app->mappingHelpers->mapPosition($profile['posi'])['id'], ':position_type_id' => isset($profile['position_type_id']) ? $profile['position_type_id'] : Yii::$app->mappingHelpers->mapPositionType($profile['positype'])['id'], ':level_id' => isset($profile['level_id']) ? $profile['level_id'] : Yii::$app->mappingHelpers->mapLevel($profile['level'])['id'], ':division_id' => isset($profile['division_id']) ? $profile['division_id'] : Yii::$app->mappingHelpers->mapDivision($profile['division'])['id'], ':user_id' => $profile['id']])->execute();
}
示例5: createAdminUser
public static function createAdminUser(AdminUser $model, \yii\db\Connection $db)
{
$db->createCommand()->insert('{{%user}}', ['username' => $model->username, 'password_hash' => Yii::$app->security->generatePasswordHash($model->password), 'email' => $model->email, 'auth_key' => '', 'create_time' => time(), 'update_time' => time()])->execute();
$userId = intval($db->lastInsertID);
$assignmentResult = $db->createCommand()->insert('{{%auth_assignment}}', ['item_name' => 'admin', 'user_id' => $userId])->execute() === 1;
return $assignmentResult && $userId > 0;
}
示例6: search
public static function search(array $input)
{
$connection = new Connection(['dsn' => 'mysql:host=localhost;dbname=work', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8']);
$connection->open();
/**запрос выбирает имя сети, агенства и сумму
* с группировкой ао агенству и "ИТОГО"
* из подзапроса суммы с границами по датам.
* COALESCE() меняет сумму NULL на 0
*/
$sql = 'SELECT agency_network.agency_network_name, agency.agency_name, COALESCE(sum(t.amount),0) AS sum
FROM agency_network
RIGHT JOIN agency
ON agency_network.agency_network_id = agency.agency_network_id
LEFT JOIN
(SELECT amount, date, agency_id
FROM billing WHERE date BETWEEN :startdate AND :enddate) t
ON t.agency_id=agency.agency_id
GROUP BY agency_name WITH ROLLUP;';
/*Привязываем параметры с датами, для исключения иньекций*/
$command = $connection->createCommand($sql)->bindParam(':startdate', $startdate)->bindParam(':enddate', $enddate);
$startdate = $input[date1];
$enddate = $input[date2];
$result = $command->queryAll();
$connection->close();
return $result;
}
示例7: getSchemaNames
/**
* @param Connection $db
* @param bool $refresh
* @return string[]
*/
public static function getSchemaNames(Connection $db, $refresh = false)
{
try {
$schemaNames = array_diff($db->getSchema()->getSchemaNames($refresh), ['public']);
} catch (NotSupportedException $e) {
$schemaNames = [];
}
return $schemaNames;
}
示例8: hasConnect
/**
* @return bool
*/
public function hasConnect()
{
$connection = new Connection(['dsn' => "mysql:host={$this->host};dbname={$this->dbname}", 'username' => $this->username, 'password' => $this->password, 'charset' => $this->charset]);
try {
$connection->open();
return true;
} catch (\Exception $e) {
return false;
}
}
示例9: getConfigFromDb
/**
* Конфиг полученный из БД
* */
private function getConfigFromDb()
{
$dbConfig = $this->textConfig['components']['db'];
$connection = new Connection(['dsn' => $dbConfig['dsn'], 'username' => $dbConfig['username'], 'password' => $dbConfig['password']]);
$connection->open();
$config = $connection->createCommand('SELECT * FROM settings')->queryAll();
return ArrayHelper::map($config, 'name', 'value', function ($array) {
return ArrayHelper::getValue($array, 'category');
});
}
示例10: init
public function init()
{
parent::init();
$host = '127.0.0.1';
$dbname = 'tmc_test';
$connection = new Connection(['dsn' => 'mysql:host=' . $host . ';dbname=' . $dbname, 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'tablePrefix' => 't_']);
$connection->open();
$this->db = $connection;
if ($this->cache !== null) {
$this->cache = Instance::ensure($this->cache, Cache::className());
}
}
示例11: setUp
protected function setUp()
{
parent::setUp();
$this->mockApplication();
Yii::$app->set('db', ['class' => Connection::className(), 'dsn' => 'sqlite::memory:']);
Yii::$app->db->createCommand()->createTable('session', ['id' => 'string', 'expire' => 'integer', 'data' => 'text', 'user_id' => 'integer'])->execute();
}
示例12: init
public function init()
{
parent::init();
$db = Instance::ensure($this->db, Connection::className());
$query = new Query();
$this->ticket = $query->select(['*'])->from($this->table)->createCommand($db)->queryAll();
}
示例13: testOpenClose
public function testOpenClose()
{
$connection = $this->getConnection(false, false);
$this->assertFalse($connection->isActive);
$this->assertEquals(null, $connection->pdo);
$connection->open();
$this->assertTrue($connection->isActive);
$this->assertTrue($connection->pdo instanceof \PDO);
$connection->close();
$this->assertFalse($connection->isActive);
$this->assertEquals(null, $connection->pdo);
$connection = new Connection();
$connection->dsn = 'unknown::memory:';
$this->setExpectedException('yii\\db\\Exception');
$connection->open();
}
示例14: __construct
public function __construct($db = 'db')
{
$this->db = Instance::ensure($db, Connection::className());
$this->generator = new Generator();
$this->dbHelper = new Migration(['db' => $this->db]);
$this->generatorConfigurator = new GeneratorConfigurator();
}
示例15: init
public function init()
{
parent::init();
$this->i2db = Instance::ensure($this->i2db, Connection::className());
$this->infodb = Instance::ensure($this->infodb, Connection::className());
$this->db46 = Instance::ensure($this->db46, Connection::className());
}