Github: (https://packagist.org/packages/yong/magento2_doctrine)
Our system has some tables for exist system which developed by Laravel,
And we using Magento2 CE which not support multiple databases.
On the other side, Magento 2 encapsulates database model a little bit too complicated.
So we need a rapid solution to retrieve these data model from 3rd database, and we decided to use Doctrine ORM.
run command:
composer require yong/magento2_doctrine dev
Download this repo to the path app/code/Yong/Doctrine
cd root_path_of_magento2
git clone https://github.com/yongchengchen/magento2_doctrine.git app/code/Yong/Doctrine
Edit root_path_of_magento2/app/etc/env.php
Add your database connection configuration to 'db.connection' node
'my_connection' =>
array (
'host' => 'mysql',
'dbname' => 'mydb',
'username' => 'root',
'password' => 'root',
'active' => '1',
),
Define a model for your table.
<?php
namespace TestNameSpace;
class TestDoctrineModel extends \Yong\Doctrine\Model\Doctrine\Model
{
public $connection = 'my_connection'; //define your connection
public $primaryKey = 'id'; //define your primary Key
public $timestamps = true; //define if your table has timestamps(created_at and updated_at)
/** @Id @Column(type="integer") **/
protected $id;
/** @Column(type="string") **/
protected $name;
/** @Column(type="integer") **/
protected $foreign_key_id; //For has many or has one
/** @Column(type="string") **/
protected $created_at;
/** @Column(type="string") **/
protected $updated_at;
/**
* relationship support
*/
public function foreignitem() {
// return $this->hasMany(OtherTestDoctrineModel::class, 'id', 'foreign_key_id');
return $this->hasOne(OtherTestDoctrineModel::class, 'id', 'foreign_key_id');
}
}
Once you've defined fields, getter and setter is ready.
$test = new TestDoctrineModel();
$test->setcreated_at('2018-01-01 00:00:00')
echo $test->getcreated_at()
So far it support hasOne and hasMany, you can define a relationship with hasOne and HasMany.
function hasOne($extra_classname, $extra_field, $self_field = null)
function hasMany($extra_classname, $extra_field, $self_field = null)
If you want to query your Doctrine Model, you can use your Doctrine directly. Here's an example.
$collection = TestDoctrineModel::select(['id', 'name', 'foreign_key_id', 'created_at', 'updated_at'])
->where('foreign_key_id', 'in', [1])
->whereLike('name', '%test%')
->where('id', 'notIn', [1])
->orWhere('id', 'in', [2])
->get();
print_r($collection); //collection is an array or row array
$item = TestDoctrineModel::find(1); //if found, it will return TestDoctrineModel instance which id =1
$collection = TestDoctrineModel::findAll('created_at', '2018-01-01 00:00:00');
//if found, it will return an anrray of TestDoctrineModel instances which is created at '2018-01-01 00:00:00'
- If you enable timestamps, it will auto update timestamps.
public $timestamps = true; //define if your table has timestamps(created_at and updated_at)
- You can call 'function fill' to fill data.
$test = new TestDoctrineModel();
$test->fill(['name'=>'test', 'foreign_key_id'=>2]);
- Use update/delete/save
$test = new TestDoctrineModel();
$test->fill(['name'=>'test', 'foreign_key_id'=>2]);
$test->save();
$test = TestDoctrineModel::find(1);
$test->fill(['name'=>'test', 'foreign_key_id'=>2]);
$test->update();
$test->delete();