mercredi 2 décembre 2015

Using 2 different Databases With Doctrine2 and Laravel 5.1

I am using Doctrine2 and Laravel 5.1.

In my database.php, i have set up a new connection that i named as 'adm'.

The 'mysql' connection is the default.

    '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,
    ],

    'adm' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => 'admin_condo',
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ]

in my model, i use the entity manager like this:

class TemplateDAO
{
    protected $em;

    const ENTITY = 'App\Model\Entities\Template';

    public function __construct (EntityManagerInterface $em, Validate $validate)
    {
        $this->em = $em;
        $this->validate = $validate;
    }
    public function create($data)
    {
        $obj = Factory::create($data);
        $this->em->persist($obj);
        $this->em->flush($obj);
    }
    public function retrieve($id)
    {
        $obj = $this->em->getRepository(self::ENTITY)->find(['id' => $id]);
        return Factory::retrieve($obj);
    }
    ...

And i want to know, what do i need to change in my DAO file, to persist in the 'adm' not in the default database?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire