我找到并成功使用了有关如何覆盖 Sylius 中现有模型的文档,但我无法利用 SyliusResourceBundle 创建一个全新的模型。我猜如果你已经了解 Symfony2 的话这很容易吗?我仍在学习,所以这就是我所拥有的......我缺少什么?
我使用完整的 Sylius 安装作为我的基础,所以我从这里开始http://sylius.org/blog/simler-crud-for-symfony2 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>
有了这个,我期望能够运行应用程序/控制台学说:架构:更新--dump-sql并在数据库中查看名为“Locations”的新表,但我得到:
无需更新 - 您的数据库已与当前实体元数据同步。
我注意到在应用程序/控制台容器:调试我有
以下服务:
令人震惊的控制器位置
容器 Sylius\Bundle\ResourceBundle\Controller\ResourceController
令人震惊的经理位置
不适用doctrine.orm.default_entity_manager的别名
令人震惊的存储库位置
容器 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 http://brentertainment.com/other/docs/book/doctrine/orm.html听起来 Entities 文件夹中的 php 类应该神奇地显示在应用程序/控制台学说:映射:信息或“链配置的命名空间”?,但 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'));
}
}
但这给了我这个:
ParameterNotFoundException:您请求的参数不存在
参数“astound_location.driver”。
我尝试将其添加到我的配置中:
astound_location:
driver: doctrine/orm
但后来我得到这个错误:
FileLoaderLoadException:无法导入资源
“.../app/config/astound.yml”来自
“.../app/config/config.yml”。 (没有
扩展能够加载“astound_location”的配置(在
.../app/config/astound.yml)。寻找命名空间
“令人震惊的位置”
感谢所有阅读以上小说的人!答案一定很简单吧?!少了什么东西?