I am trying to connect the doctrine, but I do not quite understand how I can create entities from the console, so for example, to make it easier to create Many to Many
I have already connected the Doctrine itself:
//public/bootstrap/bootstrap.php
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(// Подробнее настройка DBAL тут: http://silex.sensiolabs.org/doc/providers/doctrine.html
'driver' => 'pdo_mysql',
'dbname' => 'test',
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'password',
'charset' => 'utf8'
)
));
there is nothing in the official documentation on how to create entities - https://silex.symfony.com/doc/2.0/providers/doctrine.html
I could not find it on the Internet either, maybe someone knows how to cope with this?
Related
I'm not asking you to help me with a code purpose, but for an advice of how to do what I've got to do.
Since a few weeks, i'm developing a web application based on the symfony3 framework, and now, I have to modify what I did because my boss wants me to open the solution on multiple "groups". In fact, the group will correspond with a database which will be the group's content for the application.
I explain with a schema:
Login page -> login / password / group
If GROUP = A, database = db_group_a (for exemple)
If GROUP = B, database = db_group_b (..)
I dont know if it's clear, but here's what I have to do, and I really dont know how to do it with Symfony (In simple PHP, I would do it quite simply, but Symfony makes me ask you). If you could help me, it would be awesome.
Thanks!
In symfony you can define multiple databases like this:
$container->loadFromExtension('doctrine', array(
'dbal' => array(
'default_connection' => 'default',
'connections' => array(
'default' => array(
'driver' => '%database_driver%',
'host' => '%database_host%',
'port' => '%database_port%',
'dbname' => '%database_name%',
'user' => '%database_user%',
'password' => '%database_password%',
'charset' => 'UTF8',
),
'group_a' => array(
'driver' => '%database_driver2%',
'host' => '%database_host2%',
'port' => '%database_port2%',
'dbname' => '%database_name2%',
'user' => '%database_user2%',
'password' => '%database_password2%',
'charset' => 'UTF8',
),
'group_b' => array(
'driver' => '%database_driver2%',
'host' => '%database_host2%',
'port' => '%database_port2%',
'dbname' => '%database_name2%',
'user' => '%database_user2%',
'password' => '%database_password2%',
'charset' => 'UTF8',
),
),
),
Now, once you logged in and found out, what group is the user privileged to, you can set that in your session and add as a parameter to queries similar to this:
$allowed_db = 'group_a';
$customers = $this->get('doctrine')
->getRepository('AcmeCustomerBundle:Customer', $allowed_db)
->findAll();
I'm digging into ZF2, and I've run into some confusion on how to use Zend\Config with Zend\Db to manually set up a connection.
In different places in the manual, there are db configs in different formats.
This one shows a flat array as the config format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'zend_db_example',
'username' => 'developer',
'password' => 'developer-password'
));
While this one shows a nested format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html
$configArray = array(
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
What I expect to happen is that I can call for a new db adapter like so, but this throws exceptions:
$config = new Zend\Config\Config(
array(
'db' => array(
'adapter' => 'Mysqli',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db);
What I end up having to do is:
$config = new Zend\Config\Config(
array(
'db' => array(
'driver' => 'Mysqli',
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'database' => 'mydatabase'
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db->toArray());
Is there a better way of achieving what I'm trying to achieve without having to resort to the service manager?
Ignore the example from the Zend Config introduction page, that's just showing how to make a config object from a PHP array, the structure of the array isn't meant to show anything in particular.
Since you don't want to use the service manager, you need to pass the parameters to the adapter class in the structure it expects. It expects an array, a config object won't work. You've worked out what the structure of the array is, so that's what you need to use.
I think this page in the docs: http://framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (the "Basic setup" section) gives a better explanation of the service manager approach, which is how I'd do it in an MVC app at least.
I have a Zend Framework 1.12 application that uses the Zend DB Adapter class to connect with MySql. I need to deploy it soon and am investigating Google App Engine.
My current problem is that I can't connect the application to Google Cloud SQL. This Google page, https://developers.google.com/appengine/docs/php/cloud-sql/, gives an example using PHP and PDO. It uses a DSN as a parameter.
The Zend DB Adapter class does not take a DSN as a parameter, but instead constructs it from the other parameters it receives. I have tried to engineer the Zend DB Adapter params to give the correct DSN, but still not luck. Here's my attempt:
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => 'test-instance',
'dbname' => 'my_db_name',
'username' => 'root',
'password' => 'rootpassword',
'charset' => 'utf8',
'driver_options' => [
'unix_socket' => '/cloudsql/test-project'
]
)
)
)
);
try {
$this->_db = Zend_Db::factory($config->database);
$this->_db->getConnection();
} catch (Exception $e){
Zend_Registry::get('logger')->debug('Cannot create the connection...');
Zend_Registry::get('logger')->debug(print_r($e, true));
}
The exception in the message is
The mysql driver is not currently installed
Any ideas?
At this stage I am just trying it from the GAE SDK on my desktop. I have not uploaded the application to the cloud. I doubt this is the issue though. (My workstation IP is authorised to use the Cloud Sql instance).
I have not used the Zend Framework with AppEngine myself, but in my experience (not that I have found it documented anywhere), if you are connecting to Cloud SQL from an AppEngine app using the "root" user, you should not provide the password, or it fails to connect.
Also as shown in the documentation (here: https://developers.google.com/appengine/docs/php/cloud-sql/#PHP_Connecting_to_your_Cloud_SQL_instance), the unix_socket option should be in the format /cloudsql/project-name:instance-name
Therefore I would try rewriting your $config as follows:
$config = new Zend_Config(
array(
'database' => array(
'adapter' => 'Pdo_Mysql',
'params' => array(
'host' => '',
'dbname' => 'my_db_name',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'driver_options' => [
'unix_socket' => '/cloudsql/test-project:test-instance'
]
)
)
)
);
I can't find where to connect to the database in ZF2, where to create the application.ini to setup required data or in the module->bootstrap method, please help
As others have pointed out, you can add something like the following to your config/autoload/global.php or config/autoload/local.php.
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'root',
'password' => '',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
Note that it is best practice to add sensitive information (e.g. the password) to the local.php file. The reason is that this file should be ignored by version control software and is ignored by Git by default if you are using the Zend Skeleton Application due to a .gitignore file.
To use the connection, you can fetch the database adapter from the service manager. For instance, you can do the following in a controller action:
public function someAction() {
$dbAdapter = $this->serviceLocator->get('Zend\Db\Adapter\Adapter');
// Do something with the database adapter, e.g.:
$dbAdapter->query('SELECT username FROM user WHERE id = ?', array(37));
}
The manual should get you started on how you can use the database adapter to execute queries; you will probably want to make use of Zend\Db\Sql, so the above is just an example. Note that you would ideally not access the database (i.e. execute database queries) directly within your controllers, but the above is just an example; it is quite easy to inject the database adapter into a Data Access Layer (DAL) or service layer if you are using any of those design patterns.
What happens above is that the Zend\Db\Adapter\AdapterServiceFactory is invoked. This happens because the class implements the Zend\ServiceManager\FactoryInterface, which the service manager checks against (see the service manager's source code for more information).
The Zend\Db\Adapter\AdapterServiceFactory factory fetches the configuration from the service manager and reads the 'db' key. This configuration (as shown in the first code example) is then passed to the Zend\Db\Adapter\Adapter's constructor which configures the instantiated database adapter.
So, all you have to do is to configure your database connection under the db key in a configuration file and fetch it with the Zend\Db\Adapter\Adapter key from the service manager - then the rest is done for you automatically.
EDIT:
Make sure that you have enabled the AdapterServiceFactory in your configuration for the above to work. Provided that you are using the ZendSkeletonApplication, add the below within config/autoload/global.php.
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
to config ( global.php etc ):
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'mydb',
'password' => '',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
Go to diectory : you_application_folder/config/autoload/
in global.php
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
);
?>
in local.php // save important data here
<?php
return array(
'db' => array(
'username' => '*****',
'password' => '*****',
);
?>
I need to add sql logging to work in native WebProfileBundle.
When I do one default connection in application config, I see the sql
queries in my log. But my application uses many connections to many db
servers, so I can't add all the possible connections to config file.
I create runtime connections, i.e.:
$config = array(
'user' => 'user1',
'password' => 'pass1',
'driver' => 'pdo_mysql',
'port' => 3306,
);
$conn = DriverManager::getConnection($config);
then I think, should be command something like this
$conn->getConfiguration()->getSQLLogger($someLoggerObject);
I've tried to solve this problem with DependencyInjection, took
DoctrineBundle as example. But have no luck.
Any help with live code or link to proper documentation would be great
I have this in my conf:
$config = new Configuration;
/** configuration stuff */
$config->setSQLLogger(MyLogger::getInstance());
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => DB_USER,
'password' => DB_PASS,
'host' => DB_HOST,
'dbname' => DB_NAME,
'charset' => 'utf8',
'driverOptions' => array(
'charset' => 'utf8'
)
);
$em = EntityManager::create($connectionOptions, $config);
I use Zend + Doctrine2 and that is in my bootstrap, my logger is a singleton (thats why getInstance). I've never user DriverManager, but i hope this helps.