byng-systems/pimcore-doctrine-library

1.0.0 2015-12-24 23:34 UTC

This package is not auto-updated.

Last update: 2024-09-18 17:58:14 UTC


README

此插件允许开发人员使用 doctrine orm 来管理 pimcore 之外的实体。

用法

安装

在 composer.json 中添加插件

"require": {
    "byng-systems/pimcore-doctrine-library": "1.0.0"
}

您还需要添加一个后安装脚本来安装 doctrine cli 脚本。如果不添加以下行,则必须手动将 'cli-config.php' 从插件文件夹复制到您的文档根目录。

"scripts": {
    "post-install-cmd": "Byng\\Pimcore\\Doctrine\\Composer\\CliManager::postInstall",
    "post-update-cmd": "Byng\\Pimcore\\Doctrine\\Composer\\CliManager::postInstall"
}

设置

将以下内容添加到 'website/var/config/startup.php'。设置 $entityDir 为您希望创建实体的地方。

$entityDir = PIMCORE_DOCUMENT_ROOT . "/website/lib/Entity";
$setup = new \Byng\Pimcore\Doctrine\Setup([$entityDir]);
$em = $setup->init();

您可以将实体管理器引用 ($em) 存储在您的 DI 容器或 Zend_Registry 中。您也可以从代码库的任何地方检索设置类。

$em = \Byng\Pimcore\Doctrine\Setup::getEntityManager();

测试

打开终端并 'cd' 到您的文档根目录,然后运行以下命令

./vendor/bin/doctrine

您应该能看到所有可用的 doctrine 命令列表

示例

创建一个产品实体

注意:您可能需要将 'Entity' 命名空间添加到您的自动加载器中。

website/lib/Entity/Product.php

<?php
namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="products")
 **/
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue 
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }
    
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
}

使用 doctrine cli 创建产品表

./vendor/bin/doctrine orm:schema-tool:update --force

创建一个处理产品实体的存储库类

website/lib/Entity/Repository/ProductRepository.php

<?php
namespace Entity\Repository;

use Byng\Pimcore\Doctrine\AbstractRepository;

class ProductRepository extends AbstractRepository
{
    const ENTITY_CLASS = "Entity\\Product";
    
    /**
     *
     * @return string
     */
    protected function getEntityClass()
    {
        return static::ENTITY_CLASS;
    }
}

最后,我们可以编写代码来持久化我们的实体

<?php
use Entity\Repository\ProductRepository;
use Byng\Pimcore\Doctrine\Setup;
use Entity\Product;

$product = new Product();
$product->setName("Test");

$repository = new ProductRepository(Setup::getEntityManager());
$repository->save($product);