繁体   English   中英

如何通过SyliusResourceBundle使用Sylius创建新模型

[英]How to create a new model with Sylius via SyliusResourceBundle

我发现并成功使用了有关如何覆盖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 doctrine:schema:update --dump-sql并在我的数据库中看到我的新表名为“Locations”,但我得到:

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

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

astound.controller.location
容器Sylius \\ Bundle \\ ResourceBundle \\ Controller \\ ResourceController

astound.manager.location
不适用于doctrine.orm.default_entity_manager的别名

astound.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听起来像Entities文件夹中的php类应该神奇地出现在app / console学说中:映射:信息或“链配置名称空间”?,但Sylius在任何地方都没有实体文件夹,所以必须有一些隐藏的魔法发生...我猜它在基础捆绑文件中? 我尽力复制Sylius的其他Bundles,我做了这个:

<?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 / config.yml”导入资源“... / app / config / astound.yml”。 (没有扩展能够加载“astound_location”的配置(在... / app / config / astound.yml中)。查找命名空间“astound_location”

感谢阅读上述小说的人! 答案必须简单吗?! 缺少了什么?

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

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

所以我想快速解决方案就是简单地将学说映射从“映射超类”改为“实体”。 例如在你的例子中:

更改:型号:Astound \\ Bundle \\ LocationBundle \\ Model \\ Location to

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

并更改:mapped-superclass name =“Location”table =“Locations”to

entity name =“Location”table =“Locations”

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

遵循SyliusResourceBundle的“高级配置”为我做了诀窍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM