2017 © Pedro Peláez
 

magento2-module magento2_doctrine

Magento 2 Doctrine extension

image

yong/magento2_doctrine

Magento 2 Doctrine extension

  • Thursday, January 11, 2018
  • by Yongcheng
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Doctrine ORM support for Magneto2

Total Downloads Latest Stable Version Latest Unstable Version License 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)

* It's packaged as Magento 2 Module, but you don't have to enable this module to use it.

Installation

1.Via Composer

run command:, (*6)

composer require yong/magento2_doctrine dev

2. Download by git

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

Usage

1. Config Database Connection

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

2. Define Model

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');
    }
}

3. Supported Model Features

1. get/set property

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()
2. Relation ship support

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)
2. Query builder combination

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'
3. Update/Save/Fill data

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();

The Versions

11/01 2018

dev-master

9999999-dev

Magento 2 Doctrine extension

  Sources   Download

OSL-3.0 AFL-3.0

The Requires