我找到并成功使用了有关如何在 Sylius 中覆盖现有模型的文档,但我无法使用 SyliusResourceBundle 创建一个全新的模型。如果您已经了解 Symfony2,我猜这很容易?我还在学习,这就是我所拥有的……我缺少什么?
我使用完整的 Sylius 安装作为我的基础,所以我从这里开始 http://sylius.org/blog/simpler-crud-for-symfony2我有自己的“Astound Bundle”设置和几个覆盖和 Controller 。我将此添加到我的配置中:
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”的新表,但是相反我得到:
Nothing to update - your database is already in sync with the current entity metadata.
我注意到在 app/console container:debug 中我有 以下服务:
astound.controller.location
container Sylius\Bundle\ResourceBundle\Controller\ResourceControllerastound.manager.location
n/a alias for doctrine.orm.default_entity_managerastound.repository.location
container Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
所以我尝试在 Controller 上添加一个到 indexAction 的路由。添加到我的管理主路由配置文件:
astound_location_index:
pattern: /location
methods: [GET]
defaults:
_controller: astound.controller.location:indexAction
但是,当我尝试在浏览器中访问路由 *app_dev.php/administration/location* 时,我得到:
The class 'Astound\Bundle\LocationBundle\Model\Location' was not found in the chain configured namespaces
在写这篇文章的同时进行了更多搜索,我发现了 http://brentertainment.com/other/docs/book/doctrine/orm.html听起来 Entities 文件夹中的 php 类应该神奇地出现在 app/console doctrine:mapping:info 或“链式配置的命名空间”?,但是 Sylius 在任何地方都没有 Entity 文件夹,所以必须有一些隐藏的魔法正在发生……我猜它在 Base Bundle File 中?我尽力复制 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: You have requested a non-existent parameter "astound_location.driver".
我试着在我的配置中添加这个:
astound_location:
driver: doctrine/orm
但是我得到了这个错误:
FileLoaderLoadException: Cannot import resource ".../app/config/astound.yml" from ".../app/config/config.yml". (There is no extension able to load the configuration for "astound_location" (in .../app/config/astound.yml). Looked for namespace "astound_location"
感谢所有阅读以上小说的人!答案必须很简单?!缺少什么?
最佳答案
我刚刚遇到了同样的需求,即在扩展 Sylius 的同时创建一个新的模型/实体。我的第一次尝试也是将我的新模型添加到 sylius_resource 的配置中。这导致在运行 doctrine:schema:update 时出现相同的“Nothing to update”消息。
经过一些挖掘后,我发现我定义为“映射父类(super class)”的新模型与其他 Sylius 模型不同,并没有被“神奇地”转换为“实体”,因此学说认为没有必要产生它的数据库表。
所以我想快速的解决方案是简单地将原则映射从“映射父类(super class)”更改为“实体”。例如。在你的例子中:
改变: 型号:Astound\Bundle\LocationBundle\Model\Location to
模型:Astound\Bundle\LocationBundle\Entity\Location
并改变: mapped-superclass name="Location"table="Locations"到
entity name="Location"table="Locations"
但是,如果您更喜欢将模型保留为映射父类(super class)(并让 sylius 决定是否应将其转换为实体,这样您就可以保持灵 active 以便以后轻松扩展它),则需要仔细查看sylius 如何声明他们的包。
遵循SyliusResourceBundle的“高级配置”对我有用。
关于php - 如何通过 SyliusResourceBundle 使用 Sylius 创建新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211530/