Cannot create database from Doctrine command "orm:schema-tool:create" - php

I am following this tutorial.
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html
I am using mysql instead of sqlite.
Following command should create the database.
php vendor/bin/doctrine orm:schema-tool:create
But it is not creating any. If i manually create the database, following command works fine
php vendor/bin/doctrine orm:schema-tool:update --force
Any idea why that command is not working?
Doctrine version 2.4.1

Try this link:
vendor/bin/doctrine-module orm:schema-tool:create
User doctrine-module instead of doctrine.

I was faced with a similar issue and found that the command below
php vendor/bin/doctrine orm:schema-tool:create
Doesn't create the DB itself, it only creates the schema/tables.
I solved this by doing the following:
<?php
include 'bootstrap.php';
use Doctrine\ORM\Tools\Console\ConsoleRunner,
Doctrine\ORM\EntityManager;
try {
$entityManager->getConnection()->connect();
}
catch(Exception $ex) {
$connection = EntityManager::create([
'driver' => 'pdo_mysql',
'host' => MYSQL_HOST,
'user' => MYSQL_USERNAME,
'password' => MYSQL_PASSWORD,
'charset' => 'UTF8'
], $config)->getConnection();
$connection->executeUpdate('CREATE DATABASE '.MYSQL_DATABASE.' CHARACTER SET utf8 COLLATE utf8_general_ci');
}
return ConsoleRunner::createHelperSet($entityManager);
?>
Basically the script attempts to connect to the specified database, if that fails it attempts to create the database (assuming that the user is allowed to perform database creation that is - might be prudent to delete this file
after deployment).
Source: http://www.cstruter.com/blog/395

Related

Laravel 9 (sqlsrv) could not find driver

I have this issue with trying to connect MS SQL server from laravel project to get some data from table MECommssion I have created the command name test.php in the handle function of the below code
* Execute the console command.
*
* #return int
*/
public function handle()
{
$crmData = DB::connection('core')->table('dbo.MECommssion')->get();
dd($crmData);
}
In config/database.php
'core' => [
'driver' => env('DB_CONNECTION_SYNTELLICORE'),
'host' => env('DB_HOST_SYNTELLICORE'),
'port' => env('DB_PORT_SYNTELLICORE'),
'database' => env('DB_DATABASE_SYNTELLICORE'),
'username' => env('DB_USERNAME_SYNTELLICORE'),
'password' => env('DB_PASSWORD_SYNTELLICORE'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
In .env I have add the login
DB_CONNECTION_SYNTELLICORE=sqlsrv
DB_HOST_SYNTELLICORE=192.168.4.59
DB_PORT_SYNTELLICORE=1433
DB_DATABASE_SYNTELLICORE=test
DB_USERNAME_SYNTELLICORE=MSSB
DB_PASSWORD_SYNTELLICORE=mypassword
Every time I call the command I got this error in terminal
Illuminate\Database\QueryException
could not find driver (SQL: select * from [dbo].[MECommssion])
at E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
756▕ // If an exception occurs when attempting to run a query, we'll format the error
757▕ // message to include the bindings with SQL, which will make this exception a
758▕ // lot more helpful to the developer instead of just the database's errors.
759▕ catch (Exception $e) {
➜ 760▕ throw new QueryException(
761▕ $query, $this->prepareBindings($bindings), $e
762▕ );
763▕ }
764▕ }
1 E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("could not find driver")
2 E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct("dblib:host=192.168.4.59:1433;dbname=test;charset=utf8", "MSSB", "mypassword", [])
What I trying to do is
1- install ODBC drivers 11, 17 and 18.
2- adding DLL extension for PHP 8.1.10php_pdo_sqlsrv_81_ts_x64 and php_sqlsrv_81_ts_x64.dll and enable the extension from php.ini.
3- restart the server.
4- restart the machine.
5- For every change in .env I re-clear the catch.
6- trying the connection in a separate PHP file use PDO out of laravel project below code it is working with no problem and it shows data.
<?php
$servername = "192.168.4.59,1433";
$username = "MSSB";
$password = "mypassword";
try {
$conn = new PDO("sqlsrv:Server={$servername};Database=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT * FROM dbo.MECommssion");
$user = $stmt->fetch();
print_r($user);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: ". $e->getMessage();
}
7- install laragon server on another machine and install the new laravel project same version 9 and create the same command and try the same thing in the new project it is working normally with no problem.
8- composer dump autoload
This is all that I tried, I have no idea why the project did not read the drive sqlsrv while the separate PHP file can connect and get the data.
I would appreciate any help you can provide.
Thanks for help.
I have found the problem it was in the composer
PHP version was running PHP in the WAMP server on the same machine, after removing the WAMP server and re-install the composer in laragon server in PHP 8.1.10 folder, Now all working fine

Laravel Passport migrations not using default connection

I am updating an application for my organization from Laravel 5.4 to 8.8. One of the packages we're using is an OAuth2 implementation that is replaced with laravel/passport - so I'm trying to get that package working.
Following the directions here: https://laravel.com/docs/8.x/passport#installation, I've installed laravel/passport, and want to apply the migrations. Running php artisan migrate gives me an error saying "Database connection [mysql] not configured".
That's odd, because the default configured database connection is "uoda".
I have tried:
In config/database.php, duplicating the connection details for uoda to mysql.
Renaming uoda to mysql and changing the default connection to "mysql"
Creating a new config/passport.php file that looks like:
return [
'storage' => [
'database' => [
'connection' => 'uoda'
]
]
];
but this file isn't even being called.
Going into vendor/laravel/passport/config/passport.php and changing "storage.database.connection" to env('DB_CONNECTION', 'uoda'). I changed this back to mysql after renaming the 'uoda' connection as mentioned above.
After each of these I call php artisan cache:clear.
Nothing I do seems to cause the migration to use the actually configured connection properties. What am I missing?
Please ELI5 your responses - this is my first foray into Laravel so I know next to nothing about its intricacies.
Thanks.
I was missing an evidently important command:
php artisan config:clear

Yii2 exception 'yii\db\Exception' with message 'could not find driver'

I have an advanced yii2 template. I'm trying to create a console command.
I've created a controller class and action incide console/controllers folder:
namespace console\controllers;
use yii\console\Controller;
class WorkModelController extends Controller
{
public function actionValidate(){}
}
My action should connect with mysql database, select some data and do something with it. When I running the command: yii work-model/validate I get this error:
C:\OSPanel\domains\localhost>yii work-model/validate Exception
'yii\db\Exception' with message 'could not find driver'
in
C:\OSPanel\domains\localhost\vendor\yiisoft\yii2\db\Connection.php:56
My console/config/main.php and main-local.php files contain next db-config:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
];
When I trying connect to the database from another part of app all works fine, but when I trying connect from console I get this error.
Please, help me slove this error.
try to run php -me from the cli and make sure pdo_mysql is there..
if not, then u need to enable it from your php.ini config.
Sometime, when u use server stack such as lamp/xampp, u probably missed out from resync your environment path to use the same version of php.ini of your server stack. to do this, u may simply check/compare php.ini path from both phpinfo() from the browser and php -i from the cli

Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'

I have this error when i use php artisan migrate in my Laravel project.
[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'
this is my database.php file :
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'current_db_name'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
I saw this error in this question and this question but none of them was not helpful.
It clearly mentions that there's no such database named previous_db_name.
From what it seems the database.php file is not where the variable is from.
Check the .env file in your Laravel installation folder to see if that's the database name that you have wrongly specified.
Please run this command:
php artisan config:cache
php artisan cache:clear
AND
restart your server
I had to kill my server and re-run php artisan serve for my project to register the change of my DB_DATABASE name. I kept getting the error even after clearing the cache because I didn't realize this.
In Laravel 5.x You should define the DB Details in two files
.env file in the project folder
.database.php file inside config folder
If you are using PHP artisan serve just restart it. (just in case someone comes searching as I did).
set your database name in .env file too.
I know, it is super late but for people like me, new in laravel and following tutorial from artisansweb. Note that, migrate will not create your database. Rather, it will just create all the tables. Assuming, you have set up your .env file. Now the important part is create the database and users (if you decided to go with custom users) manually. Then, do the php artisan migrate command
hope that helps.
Try this :
<?php
class Config {
private $host = "localhost";
private $db_name = "db_ahp";
private $username = "root";
private $password = "";
public $conn;
public function getConnection() {
$this->conn = null;
try {
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
This might be late but I am just updating in case someone else faces a similar issue. I came across the same error, in my case it happened to be due to a missing database in xampp. i.e I was literally running a migration on an inexisting database in xampp.
Kill your server in case it's running. (Use Ctrl+C on windows.)
Make sure that you created the actual DB in your phpMyAdmin.
Run the following command to clear the cache: php artisan cache:clear
Run php artisan migrate once again.
Everything should be good after this.
Happy coding.

Laravel migration and PostgreSQL schemas

I'm using PostgreSQL and want to try Laravel.
First - my up() function:
public function up()
{
Schema::create('entries.entries', function($t) {
$t->increments('id');
$t->string('username', 50);
$t->string('email', 100);
$t->text('comment');
$t->timestamps();
});
}
And i have two questions:
1) I haven't shema entries in my database, so, how can i change my up function to create it too? I dont want to do it manually.
2) I got an error when executed migration:
sudo php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected
to create in (SQL: create table "emigrations" ("migration" varchar(255) not null,
"batch" integer not null))
How can i fix it?
Well i know that this question has more than 2 year's... but for the future people that reaches this. Laravel does not recognize the default 'public' schema of postgres by default and that's why you get the:
[Illuminate\Database\QueryException]
SQLSTATE[3F000]: Invalid schema name: 7 ERROR: no schema has been selected
to create in (SQL: create table "emigrations" ("migration" varchar(255) not null,
"batch" integer not null))
Thus to solve this error you just rename your Schema in postgres to anything besides public and, in your laravel project in the file config/database.php change the name of your pgsql conection schema to the one you want to use in your DB.
This should solve it.
It did for me anyways...
1) I don't think there is such functionality in Laravel but you can use an external package like https://github.com/pacuna/Laravel-PGSchema
2) Did you remove the default schema in your app/config/database.php file?
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
Laravel and PostgreSQL DB Connection, Authentication, Migration:
Prerequisites*
PHP 7.2+
Composer (a dependency manager for PHP)
PostgreSQL 9.5+
Installing Laravel
Create a new project with auth scaffolding.
laravel new blog --auth
Now Change default db connection in blog\config\database.php
'default' => env('DB_CONNECTION', 'mysql'),
change to
'default' => env('DB_CONNECTION', 'pgsql'),
Next, we’ll need to configure our database credentials in Laravel by updating the .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=projectmanager
DB_USERNAME=postgres
DB_PASSWORD=1234
Finally, we have to enable the PostgreSQL driver in our php.ini file. Let’s go into our XAMPP/WAMP directory and modify this file by commenting out these two drivers:
- extension=pdo_pgsql
- extension=pgsql
Note: check php.ini file location is bin\php\php7.3.5. Also, check the php.ini file is PHP or apache file.
Then Run migration for creating the table.
php artisan migrate
Now you get your final output.
Thanks and best of luck.

Categories