I read from here:
http://www.yiiframework.com/doc-2.0/yii-db-mysql-schema.html#findUniqueIndexes()-detail
findUniqueIndexes() returns all unique indexes for the given table.
Here what I did so far:
$table = \Yii::$app->db->schema->getTableSchema('my_tbl');
$dbSchema = new \yii\db\mysql\Schema();
$uniqueKeys = $dbSchema->findUniqueIndexes($table);
But I got this error:
Exception 'Error' with message 'Call to a member function createCommand() on null'
in /media/yahya/Data/my-projects/yii2-api-template-dev/vendor/yiisoft/yii2/db/mysql/Schema.php:232
Stack trace:
#0 /media/yahya/Data/my-projects/yii2-api-template-dev/vendor/yiisoft/yii2/db/mysql/Schema.php(322): yii\db\mysql\Schema->getCreateTableSql(Object(yii\db\TableSchema))
#1 /media/yahya/Data/my-projects/yii2-api-template-dev/console/controllers/RevmigrateController.php(103): yii\db\mysql\Schema->findUniqueIndexes(Object(yii\db\TableSchema))
And here is my DB connection config:
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=api_template',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]
If it is not a bug in the framework, then what is the right way to use this method?
you are creating a new instance of Schema without any config options.
looks like it's missing its db property.
i recommend you use instance provided trough Yii::$app
$dbSchema = Yii::$app->db->schema;
$myTableSchema = $dbSchema->getTableSchema('my_tbl');
$uniqueKeys = $dbSchema->findUniqueIndexes($myTableSchema);
Related
So I am making a web application which should be able to upload any SQL statement to any server listed in the database on a schedule. It should connect to the database, execute the statement and make a report based on the information received. Then it can disconnect.
Currently, I have a job in place and it grabs all the information required from the database eg. host, port, DBName etc. I just need to form a connection and then execute the statement.
I am using the Laravel framework.
Edit: I cant modify any database configs as the database information is stored in a table, not a config file.
Thanks
You can set it at runtime this way:
$connKey = 'CustomConnection';
$dbInfo = DatabaseInfo::find($databaseId);
Config::set('database.connections.' . $connKey, array(
'driver' => 'mysql',
'host' => $dbInfo->hostName,
'database' => $dbInfo->database,
'username' => $dbInfo->username,
'password' => $dbInfo->password,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
));
// Now you can set the new connection to the models
$myModel = new MyModel;
$myModel->setConnection($connKey);
// Or you can use it this way too:
$users = DB::connection($connKey)->select(...);
// Or, also:
$pdo = DB::connection($connKey)->getPdo();
I solved this in a very similar way to one posted :
$connectionName = Crypt::encryptString(str_random(80));
DB::purge("$connectionName");
Config::set("database.connections.$connectionName", [
"driver" => "$Driver",
"port" => "$Port",
"host" => "$Host",
"database" => "$DBName",
"username" => "$UserName",
"password" => "$Password"
]);
$query = DB::connection("$connectionName")->select($statement);
$query= json_decode( json_encode($query), true);
Excel::create("$filename-$mytime", function($excel) use ($query, $filename) {
$excel->sheet("$filename", function($sheet) use ($query){
$sheet->fromArray($query);
});
})->store('xls', storage_path('excel/exports'));
This creates a new connection with a random name (so when in a queue if multiple are executed they wont purge the same database), assigns the details to it, executes the select statement which is defined in the variable $statement. Then creates an excel spreadsheet from that.
I have PHP-Solr extension version 1.0.3-alpha installed on my server. I'm initializing the connection using:
$options = array(
'hostname' => 'hostname',
'login' => '',
'password' => '',
'port' => '',
'path' => 'solr/core1');
$client = new SolrClient($options);
It returns the following object:
class SolrClient#17 (1) { private $_hashtable_index => int(13444) }
But whenever I try to run a query like:
$qryArray = 'key_id:123456';
$client->deleteByQuery($qryArray);
It throws an exception as follows:
Solr HTTP Error 7: 'Couldn't connect to server
Can anyone help me to find what would causing this issue?
I have tried var_dump($client->getDebug());, but it returns NULL.
I'm sure this is more simple than I'm making it.
The error that I get...
Notice (8): Undefined property: Cake\Database\Connection::$config [... line 73]
The code on line 73...
Line 72: debug($conn);
Line 73: debug($conn->config);
Here's what $conn equals
$conn = object(Cake\Database\Connection) {
'config' => [
'password' => '*****',
'username' => '*****',
'host' => '*****',
'database' => '*****',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'encoding' => 'utf8',
],
}
Anyone know why $conn->config isn't the right syntax?
There is no public property named $config only a protected $_config in the database connection class in Cake (http://api.cakephp.org/3.2/source-class-Cake.Database.Connection.html).
With the the database connection in Cake you must use the public function config() to get the configuration array.
$config = $conn->config();
I don't know what debug() does, but it's not returning a detailed representation of the properties of the object as would var_dump or as you could get from reflection. Don't rely on it to learn the interface of an object/class.
Im creating a Zend APIgility application REST service and having a problem with my fetchAll Mapper function.
I'm connecting to an IBM DB2 database running on an i Series server (AS/400) via DB2 Connect on a Windows Application Server.
My Connection is is made in my local.php as such:
return array(
'db' => array(
'driver' => 'IbmDb2',
'database' => $database,
'username' => $user,
'password' => $password,
'hostname' => $host,
'port' => $port,
'driver_options' => array(
'i5_naming' => DB2_I5_NAMING_ON,
'i5_lib' => 'LIBWEB',
),
),
);
The fetchAll() function in my Mapper class is:
public function fetchAll()
{
$select = new Select('WBRESOURCE');
$paginatorAdapter = new DbSelect($select, $this->adapter);
$collection = new ResourcesCollection($paginatorAdapter);
return $collection;
}
When I hit the DbSelect, ZF2 throws the following DB2 Connect error:
"[IBM][CLI Driver][AS] SQL0204N \"*LIBL.WBRESOURCE\" is an undefined name. SQLSTATE=42704"
Im not sure why its using *LIBL (user defined library list), since I defined the library (SCHEMA) to use as LIBWEB in my connection option.
Thanks in advance!
Rob
Try changing this section:
'driver_options' => array(
'i5_naming' => DB2_I5_NAMING_ON,
'i5_lib' => 'LIBWEB',
Change to":
'driver_options' => array(
'i5_naming' => DB2_I5_NAMING_OFF, <=== change
'i5_lib' => 'LIBWEB',
By using DB2_I5_NAMING_OFF, you should get SQL naming mode. The use of DB2 i5 naming mode will result in things like reliance on a job's library list.
See PHP: db2-connect for some info on the parameter.
I've installed Laravel-4 and jenssegers / Laravel-MongoDB which has the same interface to Eloquent model as Laravel, so everything is pretty transparent and 1 database connection works OK.
what I'm trying to do, is switch to another database based on user request (Consider it as API that decided where to go and grab data).
This is what I did:
App::before(function($request)
{
$dbPrefix = $request->segment(1);
if (!is_null($dbPrefix)) {
$dbName = strtolower($dbPrefix);
$newDb = DB::connection('mongodb_'.$dbName);
}
});
From here.. I don't know what to do.. Is it connected to new database that way? how do I tell my Laravel to use $newDb when I refer to DB constant in Models?
But I want it to happen before application starts, so specifying "$connection" variable in model or using explicit call to other database like DB::connection('mongodb2')->query(...) is no good for me.
Thanks
The solution to this would be:
/app/filters.php:
App::before(function($request)
{
$dbPrefix = $request->segment(1);
if (!is_null($dbPrefix)) {
$connectionName = 'mongodb_'.strtolower($dbPrefix);
Config::set('database.default', $connectionName);
}
});
/config/database.php:
'mongodb_soccer' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'username' => 'user',
'password' => 'password',
'database' => 'SoccerData'
),
'mongodb_tennis' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'username' => 'user',
'password' => 'password',
'database' => 'TennisData'
)
Requests:
site.com/soccer
will get connection mongodb_soccer
site.com/tennis
will get connection mongodb_tennis
You can pre-authorize it in default "admin" database where your users are stored, and then switch to any database connection per user request to get the actual data.
I needed it this way for API development.
Good luck