Yii force lowercase column names in Oracle - php

I found it and write here if it will be useful for someone. By default Yii framework uses PDO and pdo serves oracle column names in uppercase. To force returning them in lowercase U have to create custom connection and set PDO attribute, like:
class COraConnection extends CDbConnection
{
protected function initConnection($pdo)
{
parent::initConnection($pdo);
$pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
...
}
}

I checked, that U can set it directly on your db, without creating custom CDbConnection.
Say your oracle connection name in main.php is
'dbora' => array(
'class' => 'CDbConnection',
'connectionString' => 'oci:dbname=192.168.0.1:1521/shop;charset=CL8MSWIN1251',
'username' => 'dbuser',
'password' => 'dbpwd',
),
Just write it where U need it:
$db = Yii::app()->dbora;
$db->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);

Related

Yii2 : How to change db connection string dynamically?

I want to change databases connection string and its credentials
on runtime.
I Have master database and many children databases.
And want to switch databses string and its credentials stored in master DB.
not like this Yii::$app->db, Yii::$app->db2, .. connections name
I found the solution to switch DB
Yii provides to set or override component by Yii::$app->set() function.
So we can override it any where within application.
In case of globally change the database, we can override it in on beforeRequest accrding to condition
In config/main.php
'components'=>[
...
],
'on beforeRequest' => function() {
$model = app\models\MainFY::findOne(Yii::$app->session->get('activeSession'));
Yii::$app->set('db', [
'class' => 'yii\db\Connection',
'dsn' => $model->db_string,
'username' => $model->db_username,
'password' => $model->db_password,
'charset' => 'utf8',
'enableSchemaCache' => !YII_DEBUG,
]);
},
in model app\models\MainFY , I am using main DB connection
public static function getDb() {
return Yii::$app->dbMain;
}

Symfony 5 Cannot autowire argument $Driver for a dynamic database connection

I am currently facing a problem, I explain myself. For my project I'm trying to make a dynamic connection to a database (I have 2 virtual machines with a different IP but with the same identifiers and tables with MSSQL database engine (SQLSRV)).
I try something like this ->
use Doctrine\DBAL\Driver\SQLSrv\Driver;
/**
* #Route("/testconnection", name="test_connect")
*/
public function testConnection(Driver $Driver){
$connectionParams = array(
'dbname' => 'job',
'user' => 'sa',
'password' => 'Lasernet#2020',
'host' => '192.168.1.34',
'driver' => 'pdo_sqlsrv',
);
$conn = $Driver->connect($connectionParams);
dd($conn);
}
Error message
Cannot autowire argument $Driver of "App\Controller\HomeController:testConnection()": it references class "Doctrine\DBAL\Driver\SQLSrv\Driver" but no such services exists.
Error message
But the problem is that Symfony sends me back an error that I find hard to solve/understand.
If someone has a solution to my problem/success to make a dynamic connection to databases.
If you need more information tell me.
The error message says it. No such service exists.
You must define the Doctrine\DBAL\Driver\SQLSrv\Driver class as a symfony service in your public/services.yaml to autowire it.
Update your public/services.yaml with:
services:
Doctrine\DBAL\Driver\SQLSrv\Driver:
autowire: true
But why you will autowire this class? The same behaviour you can get with
public function testConnection(){
$Driver = new Driver();
$connectionParams = array(
'dbname' => 'job',
'user' => 'sa',
'password' => 'Lasernet#2020',
'host' => '192.168.1.34',
'driver' => 'pdo_sqlsrv',
);
$conn = $Driver->connect($connectionParams);
dd($conn);
}
And if you have no really dynamic values in your connection params like $connectionParams['dbname'] = 'sqldb'.$i, better to use the doctrine dbal config and get the connection with $this->getDoctrine()->getConnection('name'); in your controller.
Symfony Docs

Laravel php script DB class not found

I'm trying to write a script for laravel that queries
my database and fetches tables, but I'm not sure what to include
in the file in order to get the DB class to work.
Thus far my code looks like:
$tables = DB::query("SHOW TABLES");
foreach ($tables as $table) {
print $table . "\n";
}
I've tried using require "../../vendor/autoload.php" but that doesn't help.
So for instance, the DB class works fine if I call it within a controller, but not
if I create a directory, say /application/scripts and create a file test.php .
If I try to call DB in that file, the DB class isn't defined (for good reason).
I'm wondering what I need to require in that file in order for that class to be defined.
I would suggest using the Illuminate/Database package (part of Laravel 4). You won't get the DB interface, because that's a special facade provided by the Laravel Framework, but all the query builder functionality is available through the capsule.
Install illuminate/database using composer and then follow the included readme notes (included below for completeness).
Illuminate Database
Usage Outside Of Laravel 4
$config = array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laravel',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
$capsule = new Illuminate\Database\Capsule($config);
// If you want to use the Eloquent ORM...
$capsule->bootEloquent();
// Making A Query Builder Call...
$capsule->connection()->table('users')->where('id', 1)->first();
// Making A Schema Builder Call...
$capsule->connection()->schema()->create('users', function($t)
{
$t->increments('id');
$t->string('email');
$t->timestamps();
});
You do not need to include anything. Laravel will include what it needs and setup the system itself. You are using a variable called $db but it is not defined and means/does nothing. What you are looking for is the DB class. Try this:
$tables = DB::query('SHOW TABLES');
foreach ($tables as $table)
{
echo $table.PHP_EOL;
}
Also, check out the Fluent DB class documentation:
http://laravel.com/docs/database/fluent
...Isn't it DB::query()? A call to the database engine is through a class, not a variable, for scoping issues.

Zend Framework Firebird Example

I am new to Zend. I'm using Zend 1.11 and trying to successfully connect to a Firebird database. As far as I can tell I have all the php_interbase stuff enabled. I see the ZendX firebird adapter, but I still get this message
Warning: include_once(Zend\Db\Adapter\Php\Firebird.php)
[function.include-once]: failed to open stream: No such file or
directory in C:\wamp\bin\php\Zend_Framework\library\Zend\Loader.php on
line 146
As if it has no idea what adapter I'm speaking of.
I'm using this in my boot strap
protected function _initDb()
{
$this->bootstrap('config');
$config = $this->getResource('config');
$db = Zend_Db::factory('Php_Firebird', array(
'host' => $config->Database->Server,
'username' => $config->Database->Username,
'password' => $config->Database->Password,
'dbname' => $config->Database->DBName
));
return $db;
}
I'm assuming this has something to do with the fact this is ZendX stuff not Zend\db stuff but I cannot find an example of it. Or from the factory function using a ZendX adapter. I tried to use 'Php_Interbase' but that was not found either (and I dont see it in the folders anyway). And I tried Pdo_Firebird as well which of course didnt work.
Has someone done this that can point me to what I'm doing wrong?
Thanks
You just need to add adapterNamespace to the configuration array you are passing to the factory. See the 3rd example here, also remove 'PHP_' from the adapter name, so your call to the factory should look like this:-
$db = Zend_Db::factory('Firebird', array(
'host' => $config->Database->Server,
'username' => $config->Database->Username,
'password' => $config->Database->Password,
'dbname' => $config->Database->DBName,
'adapterNamespace' => 'ZendX_Db_Adapter'
));
Try something like
$db = new ZendX_Db_Adapter_Firebird(array(
//config part
));

Yii: How is this fixture being referenced?

I'm following along in the Yii book and am on CH5 (page 101). In our unit tests, it has us define an array that specifies which fixture(s) to use:
class ProjectTest extends CDbTestCase {
}
public $fixtures=array (
'projects'=>'Project', );
But, the fixtures file is created in 'protected/tests/fixtures/tbl_project.php' and clearly isn't named 'projects'. How does Yii infer this? Note: my DB table is named tbl_project.
Thanks :)
you need to define table prefix for your db and then create the models again.
This is how your 'db' definition would look like if you are using mysql (in config > main.php under 'components')
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=test',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix'=>'tbl_',
),
Once you have the prefix in place, yii should be able to name your models excluding the prefix.

Categories