dev-master
9999999-devMagento 2 Doctrine extension
OSL-3.0 AFL-3.0
The Requires
- php ~5.6.0|7.0.2|~7.0.6
- magento/framework 100.1.*
- doctrine/orm *
Magento 2 Doctrine extension
Github: (https://packagist.org/packages/yong/magento2_doctrine), (*1)
Our system has some tables for exist system which developed by Laravel,, (*2)
And we using Magento2 CE which not support multiple databases., (*3)
On the other side, Magento 2 encapsulates database model a little bit too complicated., (*4)
So we need a rapid solution to retrieve these data model from 3rd database, and we decided to use Doctrine ORM., (*5)
run command:, (*6)
composer require yong/magento2_doctrine dev
Download this repo to the path app/code/Yong/Doctrine, (*7)
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, (*8)
Add your database connection configuration to 'db.connection' node, (*9)
'my_connection' => array ( 'host' => 'mysql', 'dbname' => 'mydb', 'username' => 'root', 'password' => 'root', 'active' => '1', ),
Define a model for your table., (*10)
<?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., (*11)
$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., (*12)
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., (*13)
$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'
1) If you enable timestamps, it will auto update timestamps., (*14)
public $timestamps = true; //define if your table has timestamps(created_at and updated_at)
2) You can call 'function fill' to fill data., (*15)
$test = new TestDoctrineModel(); $test->fill(['name'=>'test', 'foreign_key_id'=>2]);
3) Use update/delete/save, (*16)
$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();
Magento 2 Doctrine extension
OSL-3.0 AFL-3.0