PHP code example of bitrix-toolkit / bitrix-entity-mapper
1. Go to this page and download the library: Download bitrix-toolkit/bitrix-entity-mapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bitrix-toolkit / bitrix-entity-mapper example snippets
use BitrixToolkit\BitrixEntityMapper\Annotation\Entity\InfoBlock;
use BitrixToolkit\BitrixEntityMapper\Annotation\Property\Field;
use BitrixToolkit\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;
}
CModule::IncludeModule('iblock');
use BitrixToolkit\BitrixEntityMapper\SchemaBuilder;
use BitrixToolkit\BitrixEntityMapper\Map\EntityMap;
use Entity\Book;
$entityMap = EntityMap::fromClass(Book::class);
SchemaBuilder::build($entityMap);
use BitrixToolkit\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 BitrixToolkit\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();