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

Php 如何通过SyliusResourceBundle使用Sylius创建新模型,php,symfony,doctrine-orm,sylius,Php,Symfony,Doctrine Orm,Sylius,我发现并成功地使用了关于如何覆盖Sylius中现有模型的文档,但我还没有能够利用SyliusResourceBundle创建一个全新的模型。如果你已经知道Symfony2,我猜这很容易?我还在学习,所以这里是我所拥有的。。。我错过了什么 我使用一个完整的Sylius安装作为我的基础,所以我从这里开始,我有我自己的“惊奇包”设置和几个覆盖和控制器。我将此添加到我的配置中: sylius_resource: resources: astound.location:

我发现并成功地使用了关于如何覆盖Sylius中现有模型的文档,但我还没有能够利用SyliusResourceBundle创建一个全新的模型。如果你已经知道Symfony2,我猜这很容易?我还在学习,所以这里是我所拥有的。。。我错过了什么

我使用一个完整的Sylius安装作为我的基础,所以我从这里开始,我有我自己的“惊奇包”设置和几个覆盖和控制器。我将此添加到我的配置中:

sylius_resource:
    resources:
        astound.location:
            driver: doctrine/orm
            templates: AstoundWebBundle:Location
            classes:
                model: Astound\Bundle\LocationBundle\Model\Location
astound_location:
    driver: doctrine/orm
然后我提出:

<?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;
    }
}
但是,当我尝试在浏览器中转到route*app_dev.php/administration/location*时,我得到:

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

在写这篇文章的时候,我又做了几次搜索,发现Entities文件夹中的php类应该神奇地出现在app/console doctrine:mapping:info或“chain-configured namespace”中,但是Sylius在任何地方都没有实体文件夹,所以一定发生了一些隐藏的魔法。。。我猜它在基本包文件中?我尽了最大努力复制Sylius中其他捆绑包的功能,我做到了:

<?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'));
    }
}
但是我得到了这个错误:

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


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

我遇到了同样的需求,即在扩展Sylius时创建一个新模型/实体。我的第一次尝试是将我的新模型添加到Sylius_资源的配置中。这导致了相同的“无需更新”运行条令:模式:更新时的消息

经过一番挖掘,我发现我的新模型(我将其定义为“映射超类”)与其他Sylius模型不同,没有被“神奇地”转换为“实体”,因此条令认为没有必要为其生成数据库表

因此,我想快速的解决方案是简单地将原则映射从“映射超类”更改为“实体”。例如,在您的示例中:

更改: 型号:Astund\Bundle\LocationBundle\model\Location to

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

和变化: 将超类name=“Location”table=“Locations”映射到

实体名称=“位置”表=“位置”

但是,如果您希望将模型保持为映射超类(并让sylius决定是否将其转换为实体,这样您就可以保持灵活性,以便在以后轻松扩展它),那么您需要更仔细地了解sylius是如何声明其包的


按照的“高级配置”为我做了这个把戏。

我想你是对的……实际上我刚刚在一次咨询会议上得到了Sylius创始人在堆栈溢出之外的答案,他几乎说出了你的建议:对于基于Sylius构建的应用程序,使用实体,而不是模型/映射的超类
astound_location_index:
    pattern: /location
    methods: [GET]
    defaults:
        _controller: astound.controller.location:indexAction
<?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'));
    }
}
astound_location:
    driver: doctrine/orm