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.
Related
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);
I want to use Illuminate database(https://github.com/illuminate/database). Not with Laravel, use only in my php file.
I do
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
But it seems not working, and don't show any error message. Do I need to require any file? The illuminate directory is in the same directory with my php file.
EDIT:
I can use query now. Like this
$users = Capsule::table('users')->where('votes', '>', 100)->get();
I don't know how to use model.
User.php
class User extends Illuminate\Database\Eloquent\Model {
}
My php file
require 'vendor/autoload.php';
require 'User.php';
$users = User::where('status', '=', 1)->get();
Got error
Fatal error: Call to a member function connection() on a non-object in /Users/someone/repos/test/vendor/illuminate/database/Illuminate/Database/Eloquent/Model.php on line 2472
SOLVED:
Everything works fine. Use #majid8911 example https://github.com/mattstauffer/IlluminateNonLaravel
Thank you everyone.
take a look at here I successfully did the same with this tutorial:
https://github.com/mattstauffer/IlluminateNonLaravel
I am new to Cakephp, I am trying to install it. I extract everything and create the needed files/folders, now there is the following error:
Warning (2): include_once(C:\xampp\htdocs\TravelBuddy\app\Config\database.php): failed to open stream: No such file or directory [CORE\Cake\Model\ConnectionManager.php, line 67]
Warning (2): include_once() [function.include]: Failed opening 'C:\xampp\htdocs\TravelBuddy\app\Config\database.php' for inclusion (include_path='C:\xampp\htdocs\TravelBuddy\lib;.;C:\xampp\php\PEAR') [CORE\Cake\Model\ConnectionManager.php, line 67]
The file database.php does not exists. Do I have to create it?
Here is the code of ConnectionManager where the problem is:
protected static function _init() {
include_once APP . 'Config' . DS . 'database.php';
if (class_exists('DATABASE_CONFIG')) {
self::$config = new DATABASE_CONFIG();
}
self::$_init = true;
}
create inside: app/Config a file called database.php
now you need to configure your database connection like:
<?php
class DATABASE_CONFIG {
public $default = array (
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'yourusermysql',
'password' => 'passwordmysql',
'database' => 'nameofdatabase',
'prefix' => ''
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
?>
To avoid the given error you should do
if (class_exists('DATABASE_CONFIG', FALSE)) {
self::$config = new DATABASE_CONFIG();
}
i was struggling with same problem, but if the class didn't exist it should perform the remaining operation, and not giving me a fatal error.
http://www.php.net/manual/en/function.class-exists.php
Check and make sure the database.php file is committed in your git repo. Checkout the .gitignore file.
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
I can call SQL server 2008 from PHP with Microsoft Drivers for PHP for SQL Server But as Sqlsvr driver class is needed to use CakePHP with SQL server 2008, I got the driver file from following repository.
However, when running my test cakephp with following database.php
<?php
class DATABASE_CONFIG {
var $default = array(
'driver' => 'sqlsvr',
'host' => 'localhost\EPHP',
'login' => 'sa',
'password' => 'xxxxxxx',
'database' => 'Blog',
);
}
?>
I got following error:
Fatal Error (256): ConnectionManager::loadDataSource - Unable to import DataSource class .DboSqlsvr [CORE\cake\libs\model\connection_manager.php, line 185]
Then I have read all the you-cannot-make-cakePHP-work-with-sql-2008 discussions. Is there any resolution by now?
UPDATE: Let me rephrase my question. I would appreciate if someone successfully made CakePHP work with SQL 2008 and tell me the procedure he followed to do that.
After some research, I found this article ( http://book.cakephp.org/view/1652/Plugin-DataSources-and-Datasource-Drivers). Basically, you can not place your driver file in the directory ( \cakephp\cake\libs\model\datasources\dbo) where the "factory" driver files are located. Instead you should place the driver file in following directory of your baked cake.
your-cake-application\plugins\your-plugin-name\models\datasources\dbo
And then you should change your database.php in config accordingly.
<?php
class DATABASE_CONFIG {
var $default = array(
'driver' => 'your-plugin-name.DboSqlsrv',
'host' => 'localhost\EPHP',
'login' => 'sa',
'password' => 'xxxxxxx',
'database' => 'Blog',
);
}
?>
After this, I could run my cakephp app.
Assuming you have the datasources in the folder app/model/datasource/dbo_sqlsrv.php load it like this:
<?php
class DATABASE_CONFIG {
var $default = array(
'datasource' => 'sqlsvr',
'host' => 'localhost\EPHP',
'login' => 'sa',
'password' => 'xxxxxxx',
'database' => 'Blog',
);
}
?>
The difference is in the 'driver' vs 'datasource' keyword