我发现并成功使用了有关如何覆盖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 并看到我的新表名为&#34; Locations&#34;在我的数据库中,但我得到:
无需更新 - 您的数据库已与当前实体元数据同步。
我在 app / console容器中注意到:debug 我有 以下服务:
astound.controller.location
容器Sylius \ Bundle \ ResourceBundle \ Controller \ ResourceControllerastound.manager.location
的别名
不适用于doctrine.orm.default_entity_managerastound.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 *时,我得到:
班级&#39; Astound \ Bundle \ LocationBundle \ Model \ Location&#39;没找到 在链配置的命名空间
在写这篇文章的同时做了一些搜索我发现http://brentertainment.com/other/docs/book/doctrine/orm.html听起来像Entities文件夹中的php类应该神奇地出现在 app / console doctrine:mapping:info 或&#34;链配置的命名空间&#34;?,但是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:您已请求不存在 参数&#34; astound_location.driver&#34;。
我尝试在我的配置中添加它:
astound_location:
driver: doctrine/orm
但后来我收到了这个错误:
FileLoaderLoadException:无法导入资源 &#34; ... /应用/配置/ astound.yml&#34;从 &#34; ... /应用/配置/ config.yml&#34 ;. (没有 扩展能够加载&#34; astound_location&#34;的配置。 (在 ... /应用/配置/ astound.yml)。寻找命名空间 &#34; astound_location&#34;
感谢所有阅读上述小说的人!答案必须简单吗?!什么不见了?
答案 0 :(得分:0)
我刚刚遇到了同样的需求,即在扩展Sylius的同时创建一个新的模型/实体。我的第一次尝试是将我的新模型添加到sylius_resource的配置中。这在运行doctrine:schema:update。
时产生了相同的“Nothing to update”消息经过一番挖掘后,我发现我的新模型,我定义为“映射超类”,与其他Sylius模型不同,并没有“神奇地”转换为“实体”,因此学说没有任何需要生成它的数据库表。
所以我想快速的解决方案就是简单地将学说映射从“映射超类”改为“实体”。例如。在您的示例中:
变化: 型号:Astound \ Bundle \ LocationBundle \ Model \ Location to
model:Astound \ Bundle \ LocationBundle \ Entity \ Location
并改变: mapped-superclass name =“Location”table =“Locations”to
entity name =“Location”table =“Locations”
但是如果您希望将模型保持为映射超类(并且让sylius决定是否应将其转换为实体,以便您可以保持以后轻松扩展它的灵活性),则需要仔细查看sylius如何宣布他们的捆绑。
遵循SyliusResourceBundle的“高级配置”为我做了诀窍。