nitronet/ezormbundle

eZ Platform 的类似 Doctrine ORM

dev-master 2017-10-03 15:36 UTC

This package is not auto-updated.

Last update: 2024-09-23 07:46:00 UTC


README

此包旨在提供类似 ORM 的 PHP API 来查询和管理存储在 eZ Platform 中的内容。主要目的是通过提供经典的 Symfony+Doctrine 特性(如实体、表单、验证等)来简化 Symfony 开发者的工作。以下是主要功能的列表

  • 流畅查询 API
  • 实体映射到 eZ 内容
  • 表单构建器
  • 内容类型迁移(类似于 doctrine:schema:update)

显然,这个增加的抽象层并不是为了性能而设计的。eZ,就像任何其他 Symfony 应用程序一样,需要适当的 HTTP 缓存和合理的环境配置才能发挥最佳性能。我们仍然认为性能是一个关键特性,但我们知道这个 ORM 事物不会像直接使用 eZ API 那样快,您也应该意识到这一点。

此包只是构建表单、查询 API 和管理内容类型时的便捷工具包。API 是以 SQL 为导向的,便于使用和学习,但 eZ Platform 的内容模型并不是 SQL,这导致了一些限制或差异

  • 内容(sql: 行)可以具有相同的 ID 但不同的值(版本、翻译)
  • 在执行 SELECT 操作时,内容类型可以被视为 SQL 表,但在写入时有时需要位置(INSERT)和/或特殊状态(UPDATE)
  • “持久性”是个谎言(但没有人真的在乎)

查询示例

经典 eZ 查询

use eZ\Publish\API\Repository\Values\Content\Query as eZQ;

$query = new \eZ\Publish\API\Repository\Values\Content\Query();
$query->limit = 5;
$query->offset = 10;
$query->filter = new eZQ\Criterion\LogicalAnd(array(
    new eZQ\Criterion\ContentTypeIdentifier('article'),
    new eZQ\Criterion\Visibility(eZQ\Criterion\Visibility::VISIBLE)
));
$query->sortClauses = array(new eZQ\SortClause\DateModified());

$results = $this->get('ezpublish.api.service.search')->findContent($query);

$articles = array();
foreach ($results->searchHits as $searchHit) {
    $articles[] = $searchHit->valueObject;
}

eZORM 查询

use eZ\Publish\API\Repository\Values\Content\Query as eZQ;
use Nitronet\eZORMBundle\ORM\Query;

$query = new Query();
$query->select()
    ->where(new eZQ\Criterion\ContentTypeIdentifier('article'))
    ->andWhere(new eZQ\Criterion\Visibility(eZQ\Criterion\Visibility::VISIBLE))
    ->limit("10,5")
    ->orderBy(new eZQ\SortClause\DateModified())
;

$articles = $this->get('ezorm.connection')->execute($query);

ORM 魔法

上面的示例将返回 stdClass 实例。要返回自定义实体(“内容类型”),我们只需像通常使用 Doctrine 一样映射它们即可。

<?php
namespace Acme\ExampleBundle\Entity;

use Nitronet\eZORMBundle\ORM\Mapping as eZORM;


/**
 * Article
 * eZ's "article" content-type
 *
 * @eZORM\Entity()
 * @eZORM\ContentType(
 *     identifier="article",
 *     mainLanguageCode="eng-GB",
 *     urlAlias="<short_title|title>",
 *     container=true,
 *     description="Blog Article"
 * )
 */
class Article
{
    /**
     * @eZORM\Field(
     *     name="Title",
     *     identifier="title",
     *     type="ezstring",
     *     description="Title of article",
     *     container=false,
     *     translatable=true,
     *     searchable=true,
     *     position=1,
     *     settings={"maxLength": 0}
     * )
     * @var string
     */
    public $title;

    /**
     * @eZORM\Field(
     *     name="short_title",
     *     identifier="short_title",
     *     type="ezstring",
     *     description="Short title of article",
     *     container=false,
     *     translatable=true,
     *     searchable=true,
     *     position=2,
     *     settings={"maxLength": 255}
     * )
     * @var string
     */
    public $shortTitle;

    /**
     * @eZORM\Field(
     *     name="author",
     *     identifier="author",
     *     type="ezauthor",
     *     description="Title of article",
     *     container=false,
     *     translatable=true,
     *     searchable=true,
     *     position=3,
     *     settings={"maxLength": 0}
     * )
     * @var string
     */
    public $author;

    /**
     * @eZORM\Field(
     *     name="intro",
     *     identifier="intro",
     *     type="ezrichtext",
     *     description="Intro of article",
     *     container=false,
     *     translatable=true,
     *     searchable=true,
     *     position=4
     * )
     * @var string
     */
    public $intro;

    /**
     * @eZORM\Field(
     *     name="body",
     *     identifier="body",
     *     type="ezrichtext",
     *     description="Body of article",
     *     container=false,
     *     translatable=true,
     *     searchable=true,
     *     position=5
     * )
     * @var string
     */
    public $body;

    /**
     * @eZORM\Field(
     *     name="image",
     *     identifier="image",
     *     type="ezobjectrelation",
     *     description="Image of article",
     *     container=false,
     *     translatable=true,
     *     searchable=true,
     *     position=6
     * )
     * @var string
     */
    public $image = null;

    /**
     * @eZORM\Field(
     *     name="enable comments",
     *     identifier="enable_comments",
     *     type="ezboolean",
     *     description="Enable commentse",
     *     container=false,
     *     translatable=false,
     *     searchable=true,
     *     position=7
     * )
     * @var string
     */
    public $enableComments = false;

    /**
     * @eZORM\MetaField(service="ezorm.metafield.content_id")
     * @var int
     */
    public $_contentId;
}

有了这个,我们之前的示例现在将返回 Article 实例。