Symfony 2 Doctrine 2 EntityManager config - php

i'm trying to get my Doctrine CommandLine Tool working in Symfony 2 project on Windows 7 and I keep getting the same error message in console:
Fatal error: Call to protected Doctrine\ORM\EntityManager::__construct()
from invalid context in C:\wamp\www\firstSymfonyApp\cli-config.php on line 9
Call Stack:
0.0010 239440 1. {main}() C:\wamp\www\firstSymfonyApp\vendor\doctrine\orm\bin\doctrine.php:0
0.0090 621376 2. require('C:\wamp\www\firstSymfonyApp\cli-config.php') C:\wamp\www\firstSymfonyApp\vendor\doctrine\orm\bin\doctrine.php:48
Code of my cli-config.php file:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require_once 'app/bootstrap.php.cache';
$em = new \Doctrine\ORM\EntityManager();
return ConsoleRunner::createHelperSet($em);
Until today, I was only using doctrine on Linux where the installation was much more simple, please help me work this out.

Error message is very clear. EntityManager::__construct is protected method therefore you can't use it outside of the class.
Check out EntityManager::create.
Check this link for more information about how to start with Doctrine 2.
This is probably the snippet which should be important to you right now:
<?php
// bootstrap.php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/path/to/entity-files");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'foo',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

Related

Using ENUM in the cli-config.php

I am new to doctrine so please be patient.
When I am trying to run vendor/bin/doctrine orm:schema-tool:drop --force I am getting the following error message:
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it.
Yeah, yeah, yeah, I know, I've read tons of topics about this, like this one.
My problem is, I want it to do in my bootstrap.php for cli-config.php as in the documentation.
Here is my code:
$isDevMode = true;
$paths = [APP_DIR . 'classes/Entities'];
$dbParams = [
'driver' => 'pdo_mysql',
'user' => Config::DB_USER,
'password' => Config::DB_PASSWORD,
'dbname' => Config::DB_DATABASE
];
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
return \Doctrine\ORM\EntityManager::create($dbParams, $config);
I've tried to add this line to the params:
'mapping_types' => ['enum' => 'string']
but the documentation clearly says what options can be in the params.
I can not change the database. This is not a symfony project.
Does anybody has an idea?
Ok, this could be a solution.
Based on this post I've altered my code like this:
...
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = \Doctrine\ORM\EntityManager::create($dbParams, $config);
$conn = $entityManager->getConnection();
$sqlSchemaManager = new SQLServerSchemaManager($conn);
$sqlSchemaManager->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
return $entityManager;

Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable

I'm using Eloquent in combination with Slim Framework to build a small API. For some reason, I am getting this error, and I can't locate the problem:
Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.
Database settings are correct. This error is thrown in my staging environment on the server. Locally, with MySQL 5.6.29, it works perfectly. Although I don't think it's related to MySQL 5.7 because I have another app running on the same server with the same version of Eloquent.
Can anyone point me in the right direction?
I'm using:
Eloquent Version: 5.3.23
PHP Version: 5.6.28
Database Driver & Version: MySQL 5.7.16
Backtrace
/path/to/app/shared/vendor/illuminate/container/Container.php:644
Illuminate\Container\Container -> make
/path/to/app/shared/vendor/illuminate/database/Connectors/ConnectionFactory.php:130
call_user_func
/path/to/app/shared/vendor/illuminate/database/Connection.php:964
Illuminate\Database\Connection -> getPdo
/path/to/app/shared/vendor/illuminate/database/Connection.php:832
Illuminate\Database\Connection -> reconnectIfMissingConnection
/path/to/app/shared/vendor/illuminate/database/Connection.php:717
Illuminate\Database\Connection -> run
/path/to/app/shared/vendor/illuminate/database/Connection.php:350
Illuminate\Database\Connection -> select
/path/to/app/shared/vendor/illuminate/database/Query/Builder.php:1648
Illuminate\Database\Query\Builder -> runSelect
/path/to/app/shared/vendor/illuminate/database/Query/Builder.php:1634
Illuminate\Database\Query\Builder -> get
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:632
Illuminate\Database\Eloquent\Builder -> getModels
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:327
Illuminate\Database\Eloquent\Builder -> get
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:297
Illuminate\Database\Eloquent\Builder -> first
...
I'm suspecting it has something to do with an unregistered error handler:
github link
But how can I register the custom error handler when not using Laravel?
The solution of #patricus can be implemented as follows:
// bootstrap Eloquent ORM
$container = new Container();
$container->singleton(
'Illuminate\Contracts\Debug\ExceptionHandler'
, 'My\Custom\DatabaseExceptionHandler'
);
$factory = new ConnectionFactory( $container );
$connection = $factory->make([
'driver' => 'mysql'
, 'host' => App::config( 'database_host' )
, 'database' => App::config( 'database_name' )
, 'username' => App::config( 'database_user' )
, 'password' => App::config( 'database_password' )
, 'charset' => App::config( 'database_charset' )
, 'collation' => App::config( 'database_collate' )
, 'prefix' => ''
]);
$resolver = new ConnectionResolver();
$resolver->addConnection( 'default', $connection );
$resolver->setDefaultConnection( 'default' );
// initialize connection
Model::setConnectionResolver( $resolver );
You should be able to access the container to register your own exception handler through the capsule.
$capsule = new \Illuminate\Database\Capsule\Manager();
$capsule->getContainer()->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\Your\ExceptionHandler\Implementation::class
);
If you want a real quick, very dumb, exception handler, you can use this:
class DumbExceptionHandler implements \Illuminate\Contracts\Debug\ExceptionHandler
{
public function report(Exception $e)
{
//
}
public function render($request, Exception $e)
{
throw $e;
}
public function renderForConsole($output, Exception $e)
{
throw $e;
}
}
NB: untested. I believe this should work looking at the code.
In my case it was permissions issue of system user who executes php command.
Please make sure that your xampp server and the database server is working and running.

Add Doctrine DBAL into own php project

Trying to add Doctrine DBAL into my own project to use it to access my db etc. I don't have composer and i never used it. This is what i am trying to do according to the docu:
use Doctrine\Common\ClassLoader;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
require_once "doctrine/Common/ClassLoader.php";
$classLoader = new ClassLoader('Doctrine', 'doctrine');
$classLoader->register();
$config = new Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'root',
'password' => "",
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
}
This is taken from here:
-http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
and:
- http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/introduction.html
I have the Common and DBAL folder added into my project
My folder structure looks like this:
root
doctrine
DBAL
Common
php stuff
index.php (where connection.php) is executed
So what happens is that i either get "Cannot find class XY" or something similar, based upon what i change on the code. I never am able to execute it as it should following the tutorial.
What am i doing wrong here?
I just want to have the connection object, where i can start doing my stuff like useing the query builder etc...
I am completely lost here...
UPDATE: Installed composer as requested and have this Code now:
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Which is the 2nd code example in my 1st link. Tells me " Class 'Doctrine\DBAL\Configuration' not found ". Funny thing is, that IntelliJ can perfectly autocomplete the path (suggests me Configuration when finishing DBAL in the path) but PHP doesn't find it. If i remove the new Configuration PHP just tells me, that it doesn't find the DriverManager...
I installed it correctly via composer though, at least composer tells me it is installed correctly now (Where does composer save the libs?)
You now need to require composers autoload file.
require __DIR__.'/vendor/autoload.php';
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Please note, depending on your directory structure, the autoload file might be somewhere else, but usually this should work.
Pay attention to the use of namespaces: if the Doctrine namespace for its loader is Doctrine\Common\ClassLoader, you have to put the files inside the Doctrine\Common folder ("Doctrine" with a capital "D").
See the code snippet shown inside the Introduction chapter of the Doctrine DBAL documentations.

Fatal error HelperSet not found when setting up Doctrine Console (with CodeIgniter)

I've got a problem setting up Doctrine with CodeIgniter. When running the following error is coming up:
Fatal error: Class 'Symfony\Component\Console\Helper\HelperSet' not found in /Applications/MAMP/htdocs/CodeIgniter-2.2.1/application/doctrine.php on line 21
The folder structure looks like this
/application/
/application/doctrine.php
/application/libraries/
/application/libraries/Doctrine/
/application/libraries/Doctrine/Common
/application/libraries/Doctrine/DBAL
/application/libraries/Doctrine/ORM
/application/libraries/Doctrine/Symfony
/application/libraries/Doctrine/Doctrine.php
/application/libraries/Doctrine/index.html
This is the line 21
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
Cannot find out what is the problem..
Followed this tutorial: http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2/
update This is my doctrine.php in the application folder
<?php
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
chdir(APPPATH);
require __DIR__ . '/libraries/Doctrine.php';
foreach ($GLOBALS as $helperSetCandidate) {
if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
$helperSet = $helperSetCandidate;
break;
}
}
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
Just downloaded github version and without any extra settings everything seems to be ok.
Check if the HelperSet.php file exists in Doctrine/Symfony/Component/Console/Helper directory, and add these lines to Doctrine.php in libraries directory (you have to register Symfony classes):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'your_path_to/Doctrine');
$symfonyClassLoader->register();
This GitHub repository is for CodeIgniter 2, don't use it.
Try this tutorial, it works fine for me, even for CI 3.

Phalcon's universal class loader can't find my class

As a total php and phalcon newbie, I'm trying to use the recommended universal class loader using this code:
$loader = new \Phalcon\Loader();
// Register some directories
$loader->registerDirs(
array(
"php/assistants/"
)
);
// register autoloader
$loader->register();
$test = new dbAssistant();
as I understand I have to reffer to the php file as a class what I have inside of php/assistants/dbAssistant.php is the following code, trying to connect to a database:
<?php
function connect() {
$connection = new Phalcon\Db\Adapter\Pdo\Mysql(array(
'host' => 'localhost',
'username' => 'root',
'password' => 'tt',
'dbname' => 'testdb',
'port' => '3306'
));
echo 'Connected!!!';
}
again what I understand is that I have to refer to dbAssistant.php as a class and that's why I'm using $test = new dbAssistant();, but it gives me the following error:
Fatal error: Class 'dbAssistant' not found in /var/www/html/test/test.php on line 18
I know that it seeme normal, but the strange thing is that if I remove the connect() function and place the code out of it, I can see the Connected!!! echo, but it is followed by the same(above) error. I know that I'm missing something really small here, but as a complete php newbie, I really can't spot the problem.
Can you give me a push?
php/assistants/dbAssistant.php is not a class but a plain Php file. There should be a class in there with the name dbAssistant.
class dbConnect {
public function connect() {
///Do your stuff
}
}

Categories