sheerockoff / bitrix-entity-mapper
此包已 弃用 并不再维护。未建议替代包。
Bitrix 的替代 ORM
v1.1.2
2023-03-22 13:39 UTC
Requires
- php: >=7.3
- ext-mysqli: *
- doctrine/annotations: ^1.4
Requires (Dev)
- phpunit/phpunit: ^9.6
- sheerockoff/bitrix-ci: ^21.400
- symfony/var-dumper: ^5.4
This package is auto-updated.
Last update: 2024-06-28 11:47:09 UTC
README
Bitrix 实体映射器
Bitrix 的替代 ORM。
安装
composer require sheerockoff/bitrix-entity-mapper
快速开始
使用 PHPDoc 注释描述在 Bitrix 中存储对象的方式
<?php use Sheerockoff\BitrixEntityMapper\Annotation\Entity\InfoBlock; use Sheerockoff\BitrixEntityMapper\Annotation\Property\Field; use Sheerockoff\BitrixEntityMapper\Annotation\Property\Property; /** * @InfoBlock(type="library", code="books", name="Книги") */ class Book { /** * @Field(code="ID", primaryKey=true) */ public $id; /** * @Field(code="ACTIVE") */ public $active = true; /** * @Field(code="NAME") */ public $title; /** * @Property(code="author", type="string", name="Автор") */ public $author; /** * @var DateTime|null * @Property(code="published_at", type="datetime", name="Дата публикации") */ public $publishedAt; }
连接 Bitrix,模块 iblock
和 Composer 自动加载
require 'bitrix/modules/main/include/prolog_before.php'; require 'vendor/autoload.php'; CModule::IncludeModule('iblock');
需要手动创建信息块类型。其他迁移可以由 SchemaBuilder
执行。方法 SchemaBuilder::build($entityMap)
启动自动迁移,这些迁移将创建或更改所需的信息块及其属性以用于实体
use Sheerockoff\BitrixEntityMapper\SchemaBuilder; use Sheerockoff\BitrixEntityMapper\Map\EntityMap; use Entity\Book; $entityMap = EntityMap::fromClass(Book::class); SchemaBuilder::build($entityMap);
保存新对象
use Sheerockoff\BitrixEntityMapper\EntityMapper; use Entity\Book; $book = new Book(); $book->active = true; $book->title = 'Остров сокровищ'; $book->author = 'Р. Л. Стивенсон'; $book->publishedAt = new DateTime('1883-06-14 00:00:00'); $bitrixId = EntityMapper::save($book);
有几种方法可以遍历结果
use Sheerockoff\BitrixEntityMapper\EntityMapper; use Entity\Book; $query = EntityMapper::select(Book::class)->where('author', 'Р. Л. Стивенсон'); // Получить один результат. $query->fetch(); // Перебрать по одному результату. while ($book = $query->fetch()) { /* ... */ } // Использовать реализованную имплементацию интерфейса Iterator. foreach ($query as $book) { /* ... */ } // Использовать метод возвращающий генератор. foreach ($query->iterator() as $book) { /* ... */ } // Получить массив со всеми результатами. // Не рекомендуется! Небезопасное потребление памяти. $query->fetchAll();
根据实体筛选器获取结果
use Sheerockoff\BitrixEntityMapper\EntityMapper; use Entity\Book; /** @var Book|null $book */ $book = EntityMapper::select(Book::class)->where('title', 'Остров сокровищ')->fetch(); /** @var Book[] $books */ $books = EntityMapper::select(Book::class)->where('author', '%', 'Стивенсон')->fetchAll(); /** @var Book[] $books */ $books = EntityMapper::select(Book::class)->where('publishedAt', '<', '01.01.1900')->fetchAll();
根据 Bitrix 筛选器获取结果
use Sheerockoff\BitrixEntityMapper\EntityMapper; use Entity\Book; /** @var Book|null $book */ $book = EntityMapper::select(Book::class)->whereRaw('ID', 1)->fetch(); /** @var Book[] $books */ $books = EntityMapper::select(Book::class)->whereRaw('ACTIVE', 'Y')->fetchAll();
对选择进行排序
use Sheerockoff\BitrixEntityMapper\EntityMapper; use Entity\Book; /** @var Book|null $book */ $book = EntityMapper::select(Book::class)->orderBy('publishedAt', 'desc')->fetch();
更新现有对象
use Sheerockoff\BitrixEntityMapper\EntityMapper; use Entity\Book; /** @var Book|null $existBook */ $existBook = EntityMapper::select(Book::class)->fetch(); if ($existBook) { $existBook->title = 'Забытая книга'; $existBook->author = 'Неизвестный автор'; $existBook->publishedAt = null; $existBook->active = false; $updatedBitrixId = EntityMapper::save($existBook); }