【问题标题】:How to create a new model with Sylius via SyliusResourceBundle如何通过 SyliusResourceBundle 使用 Sylius 创建新模型
【发布时间】:2014-04-08 07:46:51
【问题描述】:

我找到并成功使用了有关如何在 Sylius 中覆盖现有模型的文档,但我无法使用 SyliusResourceBundle 创建一个全新的模型。我猜如果您已经了解 Symfony2,这很容易?我还在学习,所以这就是我所拥有的......我错过了什么?

我使用完整的 Sylius 安装作为我的基础,所以我从这里开始 http://sylius.org/blog/simpler-crud-for-symfony2 我有我自己的“Astound Bundle”设置和几个覆盖和控制器工作。我将此添加到我的配置中:

sylius_resource:
    resources:
        astound.location:
            driver: doctrine/orm
            templates: AstoundWebBundle:Location
            classes:
                model: Astound\Bundle\LocationBundle\Model\Location

然后我做了:

<?php

namespace Astound\Bundle\LocationBundle\Model;

class Location implements LocationInterface
{
    /**
     * @var mixed
     */
    protected $id;

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

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

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * {@inheritdoc}
     */
    public function setName($name)
    {
        $this->name = $name;
    }
}

随着:

<?php

namespace Astound\Bundle\LocationBundle\Model;


interface LocationInterface
{
    /**
     * Get Id.
     *
     * @return string
     */
    public function getId();

    /**
     * Get name.
     *
     * @return string
     */
    public function getName();

    /**
     * Set name.
     *
     * @param string $name
     */
    public function setName($name);
}

基于研究 Sylius 中现有的模型并查看 Doctrine 文档,我也做了这个:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Astound/Bundle/LocationBundle/Resources/config/doctrine/model/Location.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                    http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

      <mapped-superclass name="Location" table="Locations">
          <id name="id" type="integer">
              <generator strategy="AUTO" />
          </id>

          <field name="name" type="string" />
      </mapped-superclass>
</doctrine-mapping>

有了这个,我希望能够运行 app/console dictionary:schema:update --dump-sql 并在我的数据库中看到名为“Locations”的新表,但取而代之的是我明白了:

无需更新 - 您的数据库已与当前实体元数据同步。

我在 app/console container:debug 中注意到我有 以下服务:

震惊.controller.location
容器 Sylius\Bundle\ResourceBundle\Controller\ResourceController

震惊.manager.location
不适用于 dictionary.orm.default_entity_manager 的别名

震惊.repository.location
容器 Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository

所以我尝试在控制器上添加一个到 indexAction 的路由。添加到我的管理主路由配置文件:

astound_location_index:
    pattern: /location
    methods: [GET]
    defaults:
        _controller: astound.controller.location:indexAction

但是,当我尝试在浏览器中转到 *app_dev.php/administration/location* 路线时,我得到:

找不到类“Astound\Bundle\LocationBundle\Model\Location” 在链中配置的命名空间

在写这篇文章的同时进行了一些搜索,我发现http://brentertainment.com/other/docs/book/doctrine/orm.html 听起来实体文件夹中的 php 类应该神奇地出现在 app/console 学说:映射:信息 或“链配置的命名空间”?,但 Sylius 在任何地方都没有实体文件夹,所以一定有一些隐藏的魔法正在发生……我猜它在基本捆绑文件中?我尽力复制 Sylius 中其他 Bundle 所做的,我做了这个:

<?php

namespace Astound\Bundle\LocationBundle;

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\ResolveDoctrineTargetEntitiesPass;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
 * Astound LocationBundle
 */

class AstoundLocationBundle extends Bundle
{
    /**
     * Return array with currently supported drivers.
     *
     * @return array
     */
    public static function getSupportedDrivers()
    {
        return array(
            SyliusResourceBundle::DRIVER_DOCTRINE_ORM
        );
    }

    /**
     * {@inheritdoc}
     */
    public function build(ContainerBuilder $container)
    {
        $interfaces = array(
            'Astound\Bundle\LocationBundle\Model\LocationInterface'         => 'astound.model.location.class',
        );

        $container->addCompilerPass(new ResolveDoctrineTargetEntitiesPass('astound_location', $interfaces));

        $mappings = array(
            realpath(__DIR__ . '/Resources/config/doctrine/model') => 'Astound\Bundle\LocationBundle\Model',
        );

        $container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, array('doctrine.orm.entity_manager'), 'astound_location.driver.doctrine/orm'));
    }
}

但这给了我这个:

ParameterNotFoundException: 你请求了一个不存在的 参数“astound_location.driver”。

我尝试在我的配置中添加这个:

astound_location:
    driver: doctrine/orm

然后我得到这个错误:

FileLoaderLoadException:无法导入资源 “.../app/config/astound.yml”来自 “.../app/config/config.yml”。 (没有 扩展能够加载“astound_location”的配置(在 .../app/config/astound.yml)。寻找命名空间 “惊人的位置”

感谢所有阅读上述小说的人!答案一定很简单?!缺少什么?

【问题讨论】:

    标签: php symfony doctrine-orm sylius


    【解决方案1】:

    我刚刚遇到了同样的需求,即在扩展 Sylius 的同时创建一个新模型/实体。我的第一次尝试是将我的新模型添加到 sylius_resource 的配置中。这导致在运行教义:schema:update 时出现相同的“Nothing to update”消息。

    经过一番挖掘,我发现我的新模型,我定义为“映射超类”,与其他 Sylius 模型不同,并没有被“神奇地”转换为“实体”,因此学说没有必要产生它的数据库表。

    所以我想快速的解决方案是将教义映射从“映射超类”简单地更改为“实体”。例如。在您的示例中:

    改变: 型号:Astound\Bundle\LocationBundle\Model\Location 到

    模型:Astound\Bundle\LocationBundle\Entity\Location

    并改变: mapped-superclass name="Location" table="Locations" 到

    实体名称="位置" 表="位置"

    但是,如果您希望将模型保留为映射超类(并让 sylius 决定是否应将其转换为实体,以便您可以保持以后轻松扩展它的灵活性),您需要仔细查看sylius 如何声明他们的捆绑包。

    遵循SyliusResourceBundle 的“高级配置”对我有用。

    【讨论】:

    • 我认为你是对的......实际上,Sylius 创始人在一次咨询会议上在堆栈溢出之外为我回答了这个问题,他几乎说出了你的建议:对于一个基于 Sylius 构建的应用,使用实体,而不是模型/映射的超类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多