yong/magento2_doctrine

Magento 2 Doctrine 扩展

dev-master 2018-01-11 05:26 UTC

This package is not auto-updated.

Last update: 2024-09-21 16:49:47 UTC


README

Doctrine ORM 对 Magneto2 的支持

Total Downloads Latest Stable Version Latest Unstable Version License Github: (https://packagist.org.cn/packages/yong/magento2_doctrine)

我们的系统有一些由 Laravel 开发的现有系统表

并且我们使用 Magento2 CE,它不支持多个数据库

另一方面,Magento 2 对数据库模型进行了复杂的封装

因此,我们需要一个快速解决方案来从第三方数据库检索这些数据模型,我们决定使用 Doctrine ORM

* 它被打包为 Magento 2 模块,但您不需要启用此模块即可使用它

安装

1. 通过 Composer

运行命令

composer require yong/magento2_doctrine dev

2. 通过 git 下载

将此存储库下载到路径 app/code/Yong/Doctrine

    cd root_path_of_magento2
    git clone https://github.com/yongchengchen/magento2_doctrine.git app/code/Yong/Doctrine

使用方法

1. 配置数据库连接

编辑 root_path_of_magento2/app/etc/env.php

将您的数据库连接配置添加到 'db.connection' 节点

      'my_connection' =>
        array (
            'host' => 'mysql',
            'dbname' => 'mydb',
            'username' => 'root',
            'password' => 'root',
            'active' => '1',
        ),

2. 定义模型

为您表定义一个模型。

<?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. 支持的模型功能

1. get/set 属性

一旦您已定义字段,getter 和 setter 就准备好了。

$test = new TestDoctrineModel();
$test->setcreated_at('2018-01-01 00:00:00')
echo $test->getcreated_at()
2. 支持关系

目前它支持 hasOne 和 hasMany,您可以使用 hasOne 和 HasMany 定义一个关系。

function hasOne($extra_classname, $extra_field, $self_field = null) 
function hasMany($extra_classname, $extra_field, $self_field = null)
2. 查询构建器组合

如果您想查询 Doctrine 模型,您可以直接使用 Doctrine。这里有一个例子。

$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. 更新/保存/填充数据
  1. 如果您启用了时间戳,它将自动更新时间戳。
    public $timestamps = true;              //define if your table has timestamps(created_at and updated_at)
  1. 您可以通过调用 'function fill' 来填充数据。
$test = new TestDoctrineModel();
$test->fill(['name'=>'test', 'foreign_key_id'=>2]);
  1. 使用 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();