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();
Related
So. There is 2 different sites.
site_1 and site_2
Site_1:
PHP 7.3
Laravel 6.0
MYSQL
Contains corporate portal with helpdesk,news and so.
Site_2:
PHP 7.2
Laravel 5.6
MYSQL
Contains Videoportal(yeah like youtube:)) with users and webcams from construction sites.
On site_1 i have a class User with some properties (i.e. user id,department,and so) contains in db.
On site_2 i have different DB with user_id and cams .I want to select only webcams that belongs to user from site_1.
How can i pass value of User_id from site_1 to site_2 to select cams only for exact user?
In database.phpm define second mysql connection as
<?php
return array(
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Then use
$users = DB::connection('mysql2')->select('select * from users');
Hint
https://laravel.com/docs/5.7/database#using-multiple-database-connections
Are they using the same database or are the databases on those apps synced? If not, then this is a bad practice. Well as for passing values from one Laravel app to another, just create endpoints wherein it will send/accept values.
Read the docs especially on the Controllers and Routing and then study on how you will implement it using your own logic.
I'm a super rookie developer so be nice!
I have web hosting and database through GoDaddy. It's linked to MySQL Workbench but need it to be linked to my application, where I'm using CakePHP3.
Can someone please help me with how to enter the right host names, usernames etc. when there's an SSH key involved? See the image for the database credentials to help me fill in the blanks below:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '???',
'port' => '3306',
'username' => '???',
'password' => '???',
'database' => 'EquineEventFinder',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
DB details image - see for db details.
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 recently started using Laravel and absolutely love it, however I keep coming across the error:
SQLSTATE[42000] [1203] User 'root' already has more than 'max_user_connections' active connections
I know there is a way to change the MySQL variable to allow more connections, however this isn't an option with my host and along with this, there is no way I should of hit this limit.
Some example queries are:
return User::where('users.username', '=', Auth::user()->username)->join('settings', 'settings.username', '=', 'users.username')->first();
return Char::orderBy('calculate', 'ASC')->groupBy('charid')->get();
So my question is, what should I be looking for in order to combat the error? Should I be disconnecting myself from the database at the end of each function? Or something completely different?
Any help would be appreciated in this matter.
No, Once Change the username root to following username like *system a*nd try it
What version of Laravel do you have?
Go to "application/config.database.php" or "app/config/database.php". From here you can add custom options to your laravel database settings.
The "database.php" contains at the end something like this:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_name',
'username' => 'db_username',
'password' => 'username_password',
'charset' => 'utf8',
'prefix' => '',
),
),
You can add "options" to that array, just like this:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_name',
'username' => 'db_username',
'password' => 'username_password',
'charset' => 'utf8',
'prefix' => '',
'options' => array(
//here you will add your custom options
),
),
),
I would like to create a database with doctrine 2 and zend framework 2.
I tried to use the command line but it doesn't work because first of all I need to be connected to a database.
Here is the command line that I could use :
When I use the command "php doctrine dbal:run-sql CREATE DATABASE TOTO", I receive an error which tells me that I the database that I selected (but I don't want to select any database) is unknown.
Do you have any idea how I can figure out this problem ?
I really appreciate if I not obliged to use phpmyadmin and create it by my own. I'll prefer to use doctrine to make sure that my code is compatible with other kind of database (such as Mysql/Postegre)
Thank you =D
I found the solution.
You just have to specify in your configuration file that the dbname is equals to null.
<?php
return array (
'doctrine' => array (
'connection' =>
array (
'orm_default' =>
array (
'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
'params' =>
array (
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => null,
'charset' => 'UTF8',
),
),
'orm_poems' =>
array (
'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
'params' =>
array (
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'mot de passe',
'dbname' => 'poemsV3',
'charset' => 'UTF8',
),
),
),
),
);
Have a good day everybody =D