How do we use transactions in Zend 2? I didn't find anything in the API, and a couple questions for Zend 1 refered to the regular PDO functions, but I don't see anything like that in Zend 2.
The documentation is lacking a bit in this department for ZF2:
Start Transaction:
$this->adapter->getDriver()->getConnection()->beginTransaction();
Commit Transaction:
$this->adapter->getDriver()->getConnection()->commit();
Rollback Transaction:
$this->adapter->getDriver()->getConnection()->rollback();
Try this:
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=db;hostname=localhost',
'username' => 'root',
'password' => 'password',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
));
$adapter->getDriver()->getConnection()->beginTransaction();
DB will run command:
START TRANSACTION
Related
I have two database connections. One for my application and another for testing.
In my ..\config\database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'testing' => [
'driver' => 'mysql',
'host' => env('DB_TEST_HOST', 'localhost'),
'database' => env('DB_TEST_DATABASE', 'forge'),
'username' => env('DB_TEST_USERNAME', 'forge'),
'password' => env('DB_TEST_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am able to change the database connection in seeding using
php artisan db:seed --database=testing
I wanted to use tinker for the connection 'testing' but unable to change it. Is there any way to change the database connection for tinker similar with database seeding?
As your question starts with using one database for testing/development and one for production, you should look into using different environments, this will allow you to have no change in your code between deployment & local testing.
This task can easily be achieved by specifying your environment:
php artisan tinker --env=local
By default, if you specify no --env, you will be using /your-app/.env
When using local you read variables from /your-app/.env.local
For your specific use case:
php artisan db:seed --env=local
Further reading for Laravel 5.1: https://laravel.com/docs/5.1/configuration
Latest version: https://laravel.com/docs/configuration
NB: You should avoid checking in the ".env" file to VCS, the .env.local should be OK to share, but it is best practice to not bundle production credentials with your VCS.
To set the default database connection to 'mysql_test' from within tinker I use this command:
>>> use DB
>>> DB::setDefaultConnection('mysql_test');
It is especially useful when you want to test your migrations and seeders without messing up your existing (working) local database.
Change default connection
$model_instance = new App\YourModel();
$model_instance->setConnection('new_connection');
$data = $model_instance->find(1);
I am new to laravel. I've got the whole database designed in phpmyadmin. Now I want to integrate the existing database in Laravel. Is there any way to do that? If so then do I have to create models?
Yes, you can use your database in laravel but at first you have to provide your database credentials to allow the framework/Laravel to access your database. So, you can use a .env file or simply you can use the config/database.php to provide credentials, for example, to use your mySql database all you need to setup the database configuration as follows:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your_database_name'),
'username' => env('DB_USERNAME', 'your_database_username'),
'password' => env('DB_PASSWORD', 'your_database_password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]
Then regarding your second question, yes, you have to create models for each table or you can use DB facade to run queries directly from your controllers (not recommended). For example, to access your posts table you can create a Post model in app folder which may look something like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
// If your table name is other than posts
// then use this property to tell Laravel
// the table name.
protected $table = 'posts';
}
Then you can use something like this:
$posts = \App\Post::all(); // Get all posts
$post = \App\Post::find(1); // Get the post with id 1
If you don't want to use Eloquent Model like one I've created above then you may use something like this:
$posts = \DB::table('posts')->get(); // Get allp posts
These are some basic examples to get you start. To learn more about the use cases of models/database you should visit Laravel website and read the manual (find Database section and check Query Builder and Eloquent).
I've been using Laravel 4 for a while with success until I found a recent problem. I'm using an alternate DB connection to retrieve a list of products. My problem is that I don't find a way to create a connection like DB::connection('foo') and implement my query in a query builder style. I assume its some IoC behavior but my lack of understanding of the inner framework's code keeps me away from the answer
Thank you all
Add a second connection in app/config/database.php
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
Now use that second connection:
DB::connection('mysql2')->select('where...');
The proper way to accomplish this is DB::connection('mysql2')->table('foo')->join(...)->where(array(...))
My problem was outside the scope of this question.
Thank you all.
Currently I have to share my database with many CakePHP applications and the tables are prefixed to identify each application respectively.
So, can many applications share the same ACL tables?
Or, I could change the default names of the tables ACL and add the prefix of each application, eg. app_aros, app_acos, app_aros_acos?
/* beforeFilter() # AppController */
$this->Acl->Aro->useTable = 'app_aros';
$this->Acl->Aco->useTable = 'app_acos';
This code worked but I haven't found a way to change the tablename of the model Permission...
Suggestions? What could I do?
You have change this line in app/Config/core.php in all of your apps
Configure::write('Acl.database', 'default');
to:
Configure::write('Acl.database', 'your_acl_connection');
And also add connection in app/Config/database.php
example:
public $your_acl_connection = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
Simply you will have one DB for managing all yours app ACLs.
In this DB you create all ACL tables.
I was wondering if is possible to have CodeIgniter 2 with Doctrine 2 with multiple database connections.
I currently got CodeIgniter 2 and Doctrine 2 to work with 1 database, but is it possible to do multiple database?
If so, how can this be accomplished?
I am unsure if you can do this where the two databases interact directly. (Cross-Database Joins/etc..)
However in your load-up for doctrine you probably have something along the lines of this:
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $doctrine_config);
When You would just configure a second entity manager with the other db, perhaps with a seperate doctrine-config:
// Database connection information
$connectionOptions2 = array(
'driver' => 'pdo_mysql',
'user' => $db['other']['username'],
'password' => $db['other']['password'],
'host' => $db['other']['hostname'],
'dbname' => $db['other']['database']
);
// Create EntityManager
$this->emOther = EntityManager::create($connectionOptions2, $doctrine_config2);