php - 可重用包的 Symfony2/Doctrine2 模型类映射

标签 php symfony orm doctrine-orm doctrine

我目前正在尝试使用模型类通过 Symfony2 创建一个可重用的包,但我无法注册它们的映射,因此 Doctrine 无法识别它们。

我读到使用编译器传递可能是解决方案,所以我遵循了 Symfony Cookbook (http://symfony.com/doc/current/cookbook/doctrine/mapping_model_classes.html) 中的指南,并且我还查看了 FOSUserBundle 中的源代码以获得一些灵感。

这是我到目前为止所做的:

class GPGoodsBundle extends Bundle{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $this->addRegisterMappingsPass($container);
    }

    /**
     * @param ContainerBuilder $container
     */
    private function addRegisterMappingsPass(ContainerBuilder $container)
    {
        $modelDir = realpath(__DIR__.'/Resources/config/doctrine/model');
        $mappings = array(
            $modelDir => 'GP\Bundle\GoodsBundle\Model',
        );
        $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
        if (class_exists($ormCompilerClass)) {
            $container->addCompilerPass(
                DoctrineOrmMappingsPass::createXmlMappingDriver(
                    $mappings,
                array('gp_goods.model_manager_name'),
                'gp_goods.backend_type_orm'
                )
            );
        }
    }
}

但是当尝试迁移我的实体时(只是为了查看它是否正常工作)结果如下:

$php app/console doctrine:migrations:diff
No mapping information to process.

我的实体存储在“GP\Bundle\GoodsBundle\Model”下,它们的映射存储在“GP\Bundle\GoodsBundle\Resources\config\doctrine\model”下

所以我的问题是:创建可重用包的好方法是什么以及如何注册模型类的映射?

如果您需要任何其他信息,请随时询问!

感谢您的帮助!

这是我的模型类之一:

class Good implements GoodInterface{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    protected $serial;

    /**
     * @var \DateTime
     */
    protected $manufacturedAt;

    /**
     * @var \DateTime
     */
    protected $deliveredAt;

    /**
     * @var \DateTime
     */
    protected $expiredAt;

    /**
     * @var string
     */
    protected $checkInterval;

    /**
     * @var string
     */
    protected $status;

    /**
     * @var string
     */
    protected $slug;

    /**
     * @var \DateTime
     */

    protected $createdAt;

    /**
     * @var \DateTime
     */

    protected $updatedAt;

    /**
     * @var \DateTime
     */

    protected $deletedAt;

    public function __construct(){
        $this->createdAt = new \DateTime("now");
        $this->status = 'good';
    }

    .... getters/setters .....
}

和映射:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping   xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="GP\Bundle\GoodsBundle\Model\Good" table="good">

        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>

        <field name="serial" type="string" column="serial" length="255"/>

        <field name="manufacturedAt" type="date" column="manufactured_at"/>

        <field name="deliveredAt" type="date" column="delivered_at"/>

        <field name="expiredAt" type="date" column="expired_at"/>

        <field name="checkInterval" type="string" column="check_interval" length="255" nullable="true"/>

        <field name="status" type="string" column="status" length="255"/>

        <field name="slug" type="string" column="slug" length="255">
            <gedmo:slug fields="serial" unique="true" />
        </field>

        <field name="createdAt" type="datetime" column="created_at">
            <gedmo:timestampable on="create"/>
        </field>

        <field name="updatedAt" type="datetime" column="updated_at" nullable="true">
            <gedmo:timestampable on="update"/>
        </field>

        <field name="deletedAt" type="datetime" column="removed_at" nullable="true">
            <gedmo:soft-deleteable field-name="deletedAt"/>
        </field>

    </entity>
</doctrine-mapping>

最佳答案

当你有任何包外的实体或位置不是通常的位置时,你必须更改 config.yml 中的 Doctrine 部分

doctrine:
    # ...
    orm:
        # ...
        auto_mapping: true

doctrine:
    # ...
    orm:
        # ...
        mappings:
            model: # replace `model` with whatever you want
                type: annotation # could be xml, yml, ...
                dir: %kernel.root_dir%/../path/to/your/model/directory
                prefix: Prefix\Namespace # replace with your actual namespace
                alias: Model # replace with the desired alias
                is_bundle: false

dir 参数通知 Doctrine 去哪里寻找映射定义。如果您使用注释,它将是您的模型目录。否则,它将是您的 xml/yml 文件的目录。

实体的名称——从 Doctrine 存储库访问——在这种情况下以 Model 开头,例如 Model:User。它对应于参数alias

编辑配置文件时,不要忘记清除缓存。

此外,在我的问题中,我写道我更改了 Bundle 类中的一些内容,但它没有用,因为该包不会被另一个项目重用。所以我删除了所有内容。


有关详细信息,请参阅此答案: https://stackoverflow.com/a/10001019/2720307

感谢 Elnur Abdurrakhimov!

关于php - 可重用包的 Symfony2/Doctrine2 模型类映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816527/

相关文章:

大型数组的 PHP 内存错误

php - laravel 是否一定需要启用 proc_open() 和 passthru() ?

mongodb - 如何使用 Symfony2 和 Doctrine 配置 Monolog 将日志存储到 MongoDB

Symfony 2.2 和 Monolog 流处理程序 - 自动创建目录

php - 如何在 Symfony 表达式语言中连接字符串、参数和函数返回结果

PHP 警告 : Cannot load module "http" because required module "raphf" is not loaded in Unknown on line 0

php - 在 PHP 中使用 IntlDateFormatter 的本地化(短)月份名称?

java - 获取 JPA 和 Hibernate 继承树中的鉴别器值列表

java - Java 类中的多对多关系,无需使用任何 ORM

.net - 如何微调 FluentNHibernate 的自动映射器?