本文整理汇总了PHP中CActiveRecord::db方法的典型用法代码示例。如果您正苦于以下问题:PHP CActiveRecord::db方法的具体用法?PHP CActiveRecord::db怎么用?PHP CActiveRecord::db使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CActiveRecord
的用法示例。
在下文中一共展示了CActiveRecord::db方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
public function setUp()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) {
$this->markTestSkipped('PDO and PostgreSQL extensions are required.');
}
$this->db = new CDbConnection('pgsql:host=127.0.0.1;dbname=yii', 'test', 'test');
try {
$this->db->active = true;
} catch (Exception $e) {
$schemaFile = realpath(dirname(__FILE__) . '/../data/postgres.sql');
$this->markTestSkipped("Please read {$schemaFile} for details on setting up the test environment for PostgreSQL test case.");
}
try {
$this->db->createCommand('DROP SCHEMA test CASCADE')->execute();
} catch (Exception $e) {
}
try {
$this->db->createCommand('DROP TABLE yii_types CASCADE')->execute();
} catch (Exception $e) {
}
$sqls = file_get_contents(dirname(__FILE__) . '/../data/postgres.sql');
foreach (explode(';', $sqls) as $sql) {
if (trim($sql) !== '') {
$this->db->createCommand($sql)->execute();
}
}
$this->db->active = false;
$config = array('basePath' => dirname(__FILE__), 'components' => array('db' => array('class' => 'system.db.CDbConnection', 'connectionString' => 'pgsql:host=127.0.0.1;dbname=yii', 'username' => 'test', 'password' => 'test')));
$app = new TestApplication($config);
$app->db->active = true;
CActiveRecord::$db = $this->db = $app->db;
}
示例2: setUp
public function setUp()
{
$this->getFixtureManager()->resetTable('order');
$this->getFixtureManager()->loadFixture('order');
CActiveRecord::$db = $this->db = Yii::app()->db;
parent::setUp();
}
示例3: setUp
/**
* set up environment with yii application and migrate up db
*/
public function setUp()
{
$basePath = dirname(__FILE__) . '/tmp';
if (!file_exists($basePath)) {
mkdir($basePath, 0777, true);
}
if (!file_exists($basePath . '/runtime')) {
mkdir($basePath . '/runtime', 0777, true);
}
// create webapp
if (\Yii::app() === null) {
\Yii::createWebApplication(array('basePath' => $basePath));
}
\CActiveRecord::$db = null;
if (!isset($_ENV['DB']) || $_ENV['DB'] == 'sqlite') {
if (!$this->dbFile) {
$this->dbFile = $basePath . '/test.' . uniqid(time()) . '.db';
}
\Yii::app()->setComponent('db', new \CDbConnection('sqlite:' . $this->dbFile));
} elseif ($_ENV['DB'] == 'mysql') {
\Yii::app()->setComponent('db', new \CDbConnection('mysql:dbname=test;host=localhost', 'root'));
} elseif ($_ENV['DB'] == 'pgsql') {
\Yii::app()->setComponent('db', new \CDbConnection('pqsql:dbname=test;host=localhost', 'postgres'));
} else {
throw new \Exception('Unknown db. Only sqlite, mysql and pgsql are valid.');
}
// create db
$this->migration = new EActiveRecordRelationBehaviorTestMigration();
$this->migration->dbConnection = \Yii::app()->db;
$this->migration->up();
}
示例4: setUp
protected function setUp()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('PDO and SQLite extensions are required.');
}
$this->_connection = new CDbConnection('sqlite::memory:');
$this->_connection->active = true;
$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/../fixtures/data/sqlite.sql'));
CActiveRecord::$db = $this->_connection;
}
示例5: setUp
public function setUp()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('PDO and SQLite extensions are required.');
}
$this->db = new CDbConnection('sqlite::memory:');
$this->db->active = true;
$this->db->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/CSortTest.sql'));
CActiveRecord::$db = $this->db;
}
示例6: setUp
public function setUp()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('PDO and SQLite extensions are required.');
}
copy(SRC_DB_FILE, TEST_DB_FILE);
$config = array('basePath' => dirname(__FILE__), 'components' => array('db' => array('class' => 'system.db.CDbConnection', 'connectionString' => 'sqlite:' . TEST_DB_FILE)));
$app = new TestApplication($config);
$app->db->active = true;
CActiveRecord::$db = $this->db = $app->db;
}
示例7: setUp
protected function setUp()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('PDO and SQLite extensions are required.');
}
$this->_connection = new CDbConnection('sqlite::memory:');
$this->_connection->active = true;
$columns = array('id' => 'pk', 'name' => 'string');
$this->_connection->createCommand()->createTable($this->_tableName, $columns);
CActiveRecord::$db = $this->_connection;
$this->declareArModelClass();
}
示例8: setup
public function setup()
{
define('YII_DEBUG', false);
$basePath = dirname(dirname(dirname(dirname(__DIR__))));
// go 4 up
require $basePath . '/vendor/yiisoft/yii/framework/yii.php';
require_once "{$basePath}/app/classes/model/yii1/Comment.php";
require_once "{$basePath}/app/classes/model/yii1/Post.php";
$dbConfig = DbConfig::get_params();
$db = new CDbConnection("mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};charset=utf8", $dbConfig['username'], $dbConfig['password']);
CActiveRecord::$db = $db;
}
示例9: setUp
public function setUp()
{
if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('PDO and SQLite extensions are required.');
}
$this->_connection = new CDbConnection('sqlite::memory:');
$this->_connection->active = true;
$this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__) . '/../data/sqlite.sql'));
CActiveRecord::$db = $this->_connection;
UserWithWrappers::clearCounters();
PostWithWrappers::clearCounters();
CommentWithWrappers::clearCounters();
}
示例10: setUp
public function setUp()
{
if (self::DB_DSN_PREFIX == 'sqlsrv' && (!extension_loaded('pdo') || !extension_loaded('sqlsrv') || !extension_loaded('pdo_sqlsrv'))) {
$this->markTestSkipped('PDO and SQLSRV extensions are required.');
} else {
if (self::DB_DSN_PREFIX != 'sqlsrv' && (!extension_loaded('pdo') || !extension_loaded('pdo_dblib'))) {
$this->markTestSkipped('PDO and MSSQL extensions are required.');
}
}
if (self::DB_DSN_PREFIX == 'sqlsrv') {
$dsn = self::DB_DSN_PREFIX . ':Server=' . self::DB_HOST . ';Database=' . self::DB_NAME;
} else {
$dsn = self::DB_DSN_PREFIX . ':host=' . self::DB_HOST . ';dbname=' . self::DB_NAME;
}
$this->db = new CDbConnection($dsn, self::DB_USER, self::DB_PASS);
if (self::DB_DSN_PREFIX == 'sqlsrv') {
$this->db->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM);
}
try {
$this->db->active = true;
} catch (Exception $e) {
$schemaFile = realpath(dirname(__FILE__) . '/../data/mssql.sql');
$this->markTestSkipped("Please read {$schemaFile} for details on setting up the test environment for MSSQL test case.");
}
$tables = array('comments', 'post_category', 'posts', 'categories', 'profiles', 'users', 'items', 'orders', 'types');
foreach ($tables as $table) {
$sql = <<<EOD
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[{$table}]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROP TABLE [dbo].[{$table}]
EOD;
$this->db->createCommand($sql)->execute();
}
$rawSqls = file_get_contents(dirname(__FILE__) . '/../data/mssql.sql');
// remove comments from SQL
$sqls = '';
foreach (array_filter(explode("\n", $rawSqls)) as $line) {
if (substr($line, 0, 2) == '--') {
continue;
}
$sqls .= $line . "\n";
}
// run SQL
foreach (explode('GO', $sqls) as $sql) {
if (trim($sql) !== '') {
$this->db->createCommand($sql)->execute();
}
}
CActiveRecord::$db = $this->db;
}
示例11: setUp
/**
* set up environment with yii application and migrate up db
*/
public function setUp()
{
$basePath = dirname(__FILE__) . '/tmp';
if (!file_exists($basePath)) {
mkdir($basePath, 0777, true);
}
if (!file_exists($basePath . '/runtime')) {
mkdir($basePath . '/runtime', 0777, true);
}
if (!$this->db) {
$this->db = $basePath . '/test.' . uniqid(time()) . '.db';
}
// create webapp
if (\Yii::app() === null) {
\Yii::createWebApplication(array('basePath' => $basePath));
}
\CActiveRecord::$db = null;
\Yii::app()->setComponent('db', new \CDbConnection('sqlite:' . $this->db));
// create db
$this->migration = new EActiveRecordRelationBehaviorTestMigration();
$this->migration->dbConnection = \Yii::app()->db;
$this->migration->up();
}
示例12: getDbConnection
public function getDbConnection()
{
if (self::$db !== null) {
return self::$db;
} else {
self::$db = Yii::app()->getDb();
if (self::$db instanceof CDbConnection) {
return self::$db;
} else {
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
}
}