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;
Related
I am developing in Slim 3. I decided for Eloquent as ORM and would like to use Illuminate validators in middleware to validate info that comes to my Controllers.
Validation rules that do not need to use database to validate work great but I am having trouble using database validators.
I do not get any errors it just does not pass when it should.
As far as I understand it I should create validator factory and then call make function. Example:
$v = $this->validatorFactory
->make($toCheck, $this->rules[$route], $this->messages);
if ($v->fails()) {....}
Since it works for all routes that do not need database I won't go here in any more detail. I think the problem is with how I add PressenceValidator to my factory.
I do it like this.
private function initiateValidatorFactory($dbConnectionSettings)
{
$validatorFactory = new ValidatorFactory(
new Translator(new FileLoader(new Filesystem(), "en"), "en"),
null);
$sqlConfig = $dbConnectionSettings['mysql'];
$dsn = 'mysql:host=' . $sqlConfig['host'] . ';port=' . $sqlConfig['port'] . ';dbname=' . $sqlConfig['database'];
$connection = new Connection(
new \PDO($dsn, $sqlConfig['username'], $sqlConfig['password']),
$sqlConfig['database'],
$sqlConfig['prefix']
);
// var_dump($connection->select('SELECT * FROM platform;'));
// die();
$resolver = new ConnectionResolver(['mysql' => $connection]);
// var_dump($resolver->hasConnection('mysql'));
// die();
// var_dump($resolver->connection('mysql'));
// die();
$verifier = new DatabasePresenceVerifier($resolver);
// $verifier->setConnection('mysql');
$validatorFactory->setPresenceVerifier($verifier);
$this->validatorFactory = $validatorFactory;
}
I would like to note that this raw SQL query which is commented out works at that point. That is why I don't think problem is with Connection class.
Notice how I created almost dummy FileLoader but I don't think I need it for my use case (or I might I am not sure).
All commented out checks look ok.
I tried googling and saw that people mention something about "booting eloquent". I tried to find where I could do that in my code but wasn't successful.
Last thing is I am missing is how I write my rules. Example rule is this:
'platformid' => 'exists:mysql.platform,platformid'
PS don't ask why we have column "platformid" in table "platform" =).
I am using illuminate/validation 5.5 and illuminate/database 5.5.
Thank you for your time and any help you can provide in advance.
I somehow figured it out. It is hacky but I think I can live with it now. I don't quite understand why this approach works but it does.
So I have database service provider like this:
$capsule = new Manager();
////////CONFIGURE MYSQL\\\\\\\
$sqlConfig = $pimple['settings']['db']['mysql'];
$capsule->addConnection([
'driver' => $sqlConfig['driver'],
'host' => $sqlConfig['host'],
'database' => $sqlConfig['database'],
'username' => $sqlConfig['username'],
'password' => $sqlConfig['password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], $this->connectionName);
....
....
$pimple['db'] = function ($c) use ($capsule) {
return $capsule;
};
I call that in my dependencies.php
Then when I come to routes I do this:
$container = $app->getContainer();
$validatorMiddleware = new \....\Middleware\BaseMiddleware($container->get('db'));
I changed my middleware init to this
private function initiateValidatorFactory($dbConnectionSettings)
{
$validatorFactory = new ValidatorFactory(
new Translator(new FileLoader(new Filesystem(), "en"), "en"),
null);
$resolver = new ConnectionResolver(['mysql' => $dbConnectionSettings->getConnection('mysql')]);
$verifier = new DatabasePresenceVerifier($resolver);
$validatorFactory->setPresenceVerifier($verifier);
$this->validatorFactory = $validatorFactory;
}
Notice how my variables don't make sense any more but you will figure it out how to make it nicer and stuff. (me included). Most important thing is this works somehow. I know it is hacky. If you wish to post an answer that does not include hacks please do =).
So, I'm trying to get around in Silex. Just learn the way it works and I'm trying to use Doctrine in it. I can use it on the index.php, but I'd also like to use it in my classes. These lines are used in the normal root file (index.php):
$images = $app['db']->prepare("SELECT * FROM images");
$images->execute();
$images = $images->fetchAll(\PDO::FETCH_CLASS, \AI\Models\Image::class);
So that would give me the ability to do something with the images. But I don't want to work this way. I'd like classes to do it all for me, so that I just script some methods which do all the hard work for me. That would let me just run one line for each Route in index.php
The problem is that I don't know how to connect with Doctrine from inside my classes. Because there is no '$app' in there. I think it would be weird to start the app inside of a class.
So let's say I wanted to create a user class. This SQL would give me all the users: "SELECT * FROM users". But how would I use Doctrine inside the User class?
<?php
namespace Models;
class User {
public function find($user){
if($user) {
$field = (is_numeric($user)) ? 'id' : 'username';
$sql = "SELECT * FROM users";
$data = // RUN QUERY $SQL
if($data->count()) {
$this->_data = $data->all();
return true;
}
}
return false;
}
}
In your index.php follow these steps.
Create instance of the silex application:
$app = new Silex\Application();
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
Set the database configuration:
$config = new \Doctrine\DBAL\Configuration();
$connParams = array(
'driver' => 'driver',
'dbname' => 'dbname',
'host' => 'host',
'user' => 'user',
'password' => 'pass',
'charset' => 'charset',
'port' => 'port'
);
Connect to database:
$conn = \Doctrine\DBAL\DriverManager::getConnection($connParams, $config);
Now you have different ways to make this db instance accessible throughout your app. You can whether make a global variable adding the instance in it:
global $dbcon;
$dbcon = $conn;
Or simply add it to the $app itself:
$app['dbcon'] = $conn;
Furthermore you might want to add a constructor to your models like this:
public function __construct($db)
{
$this->db = $db;
}
You would need to inject those instances in your controller.
I looked through the docs real quick and think I found what you need.
Here is a link to the documentation page : http://silex.sensiolabs.org/doc/2.0/providers/service_controller.html.
the documentation page explains everything you need to know to get what you want to achieve.
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);
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.
I am working on a project based on Zend 1.12. There is a new requirement in which we have to encode all of our files to prevent hacking. We used ioncube encoder which encode all of the .php file but not application.ini where it store db information username, password, secret key etc. There are two approach :
1) Have configuration reside in .php file such as config.php instead of application.ini
2) have ioncube encode application.ini
With the first approach I found Zend Config Introduction where you could have configuration store in array. I need a concrete example with setup in Bootstrap.php where it could utilize these configuration. Right now all of our model extends from Zend_Db_Table_Abstract. I really want when i migrate all the application.ini db configuration into config.php all the db operation works and there are several instance in front controller make use of $this->applicationOptions. I hope my putting configuration this will work as well. Please help
With second approach I did not found much resolution on ioncube be able to encode application.ini
This is my configuration file
return $configArray = array(
'db' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'test'
)
)
);
When i do this in Bootstrap.php and it work
$config = new Zend_Config(require APPLICATION_PATH .'/configs/config.php');
$db = Zend_Db::factory($config->db->adapter,
$config->dv->params->toArray());
$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
but when i do
protected function _initDatabase(){
$config = new Zend_Config(require APPLICATION_PATH .'/configs/config.php');
$resource = $this->getPluginResource('db');
var_dump($this->getPluginResource('db')); // this will be null.
My question is is should i do anything different in configuration so it will mimic resources.db.adapter, resources.db.params.host etc and it will be picking up by the pluginResources or getoptions
Normally it is expected that users would use one of the adapter classes such as Zend_Config_Ini or Zend_Config_Xml, but if configuration data are available in a PHP array, one may simply pass the data to the Zend_Config constructor in order to utilize a simple object-oriented interface.
Basically, you first create a PHP file that contains the configuration :
// config.php
return array(
...
...
);
And, then, from another file, use than configuration file :
$config = new Zend_Config(require 'config.php');
In your Bootstrap.php try getting the configuration like this
protected function _initDb()
{
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
Zend_Registry::set("db", $db);
}
After registering the db variable in your Bootstrap.php you can access it like this
$dbAdapter = Zend_Registry::get("db");
You use getPluginResource() hence you need to have your configuration keys in the following way:
resources.db.adapter = ...
resources.db.params.host = ...
...
or your config.php should look like this:
// config.php
return array(
'resources' => array(
'db' => array(
...
),
),
...
);
This page could be helpful.