【发布时间】:2014-04-08 07:46:51
【问题描述】:
我找到并成功使用了有关如何在 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 dictionary:schema:update --dump-sql 并在我的数据库中看到名为“Locations”的新表,但取而代之的是我明白了:
无需更新 - 您的数据库已与当前实体元数据同步。
我在 app/console container:debug 中注意到我有 以下服务:
震惊.controller.location
容器 Sylius\Bundle\ResourceBundle\Controller\ResourceController震惊.manager.location
不适用于 dictionary.orm.default_entity_manager 的别名震惊.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 听起来实体文件夹中的 php 类应该神奇地出现在 app/console 学说:映射:信息 或“链配置的命名空间”?,但 Sylius 在任何地方都没有实体文件夹,所以一定有一些隐藏的魔法正在发生……我猜它在基本捆绑文件中?我尽力复制 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: 你请求了一个不存在的 参数“astound_location.driver”。
我尝试在我的配置中添加这个:
astound_location:
driver: doctrine/orm
然后我得到这个错误:
FileLoaderLoadException:无法导入资源 “.../app/config/astound.yml”来自 “.../app/config/config.yml”。 (没有 扩展能够加载“astound_location”的配置(在 .../app/config/astound.yml)。寻找命名空间 “惊人的位置”
感谢所有阅读上述小说的人!答案一定很简单?!缺少什么?
【问题讨论】:
标签: php symfony doctrine-orm sylius