如何通过SyliusResourceBundle与Sylius创建一个新的模型 [英] How to create a new model with Sylius via SyliusResourceBundle
问题描述
我发现并成功地使用了有关如何覆盖Sylius中现有模型的文档,但是我无法使用SyliusResourceBundle创建一个全新的模型。我猜你很容易,如果你已经知道Symfony2?我仍然在学习,所以这里是我有什么...我失踪了什么?
我使用一个完整的Sylius安装作为我的基础,所以我从这里开始 http://sylius.org/blog/simpler-crud-for-symfony2 我'我有自己的Astound Bundle设置和几个覆盖和控制器。我把它添加到我的配置中:
sylius_resource:
resources:
astound.location:
驱动程序:doctrine / orm
模板:AstoundWebBundle:位置
类:
模型:Astound\Bundle\LocationBundle\Model\Location
然后我做了:
php
命名空间Astound\Bundle\LocationBundle\Model;
class位置实现LocationInterface
{
/ **
* @var mixed
* /
protected $ id;
/ **
* @var string
* /
protected $ name;
public function getId()
{
return $ this-> id;
}
/ **
* {@inheritdoc}
* /
public function getName()
{
return $这个 - >名称;
}
/ **
* {@inheritdoc}
* /
public function setName($ name)
{
$ this-> name = $ name;
}
}
随着:
<?php
命名空间Astound\Bundle\LocationBundle\Model;
接口LocationInterface
{
/ **
* Get Id。
*
* @return string
* /
public function getId();
/ **
*获取名称。
*
* @return string
* /
public function getName();
/ **
*设置名称。
*
* @param string $ name
* /
public function setName($ name);
}
根据研究Sylius的现有模型,并通过Doctrine文档查看,我做出了以及
<?xml version =1.0encoding =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 =Locationtable =Locations>
< id name =idtype =integer>
< generator strategy =AUTO/>
< / id>
< field name =nametype =string/>
< / mapped-superclass>
< / doctrine-mapping>
有了这个,我期待能够运行应用程序/控制台原则:模式:update --dump-sql ,并在我的数据库中看到我的新表,名为Locations,但是我得到:
没有更新 - 您的数据库已经与当前的实体元数据同步。
我注意到在应用程序/控制台容器:调试,我有
以下服务:
astound.controller.location
容器Sylius\Bundle\ResourceBundle\Controller\ResourceController
astound.manager.location
n / a docirine.orm的别名.default_entity_manager
astound.repository.location
容器Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
所以我试图添加一个路由到indexAction on控制器。添加到我的管理主路由配置文件:
astound_location_index:
pattern:/ location
methods: GET]
默认值:
_controller:astound.controller.location:indexAction
但是,当我尝试去浏览我的路线* app_dev.php / administration / location *时,我得到:
在链配置命名空间中没有找到\Bundle\LocationBundle\Model\Location'
而且在在撰写本文时再进行一些搜索,我发现 http://brentertainment.com/other /docs/book/doctrine/orm.html 它听起来像实体文件夹中的php类应该神奇地显示在应用程序/控制台原则:映射:信息或链式配置命名空间? ,但Sylius没有任何实体文件夹,所以th ere必须是一些隐藏的魔法发生...我猜这是在Base Bundle文件?我尽力复制Sylius的其他软件包,我做了这个:
<?php
命名空间Astound\Bundle\LocationBundle;
使用Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
使用Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\ResolveDoctrineTargetEntitiesPass;
使用Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
使用Symfony\Component\DependencyInjection\ContainerBuilder;
使用Symfony\Component\HttpKernel\Bundle\Bundle;
/ **
* Astound LocationBundle
* /
class AstoundLocationBundle extends Bundle
{
/ **
*返回数组与当前支持的驱动程序。
*
* @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:但是,我收到这个错误:
$ b $($)
$ b FileLoaderLoadException:无法从
... / app / config / config.yml导入资源
... / app / config / astound.yml 。 (没有
扩展能够加载astound_location的配置(在
... / app / config / astound.yml)。查找命名空间
astound_location
感谢任何阅读上述小说的人!答案必须简单吗?什么缺少?
解决方案我刚刚遇到同样的需求,即在扩展Sylius时创建一个新的模型/实体,我的第一个尝试是将我的新模型添加到配置的sylius_resource,这导致了运行原则时相同的没有更新消息:schema:update。
在一些挖掘之后,我发现我定义的新模型作为映射超类,与其他Sylius模型不同,没有被神奇地转换为实体,因此教义没有看到需要为其生成数据库表。
所以我想快速的解决方案是简单地将映射超类中的教义映射到实体。例如。在你的例子中:
change:
model:Astound\Bundle\LocationBundle\Model\Location to
模型:Astound\Bundle\LocationBundle\Entity\Location
并更改:
mapped-superclass name =位置table =Locationsto
entity name =Locationtable =Locations
但是您更希望将模型保留为映射超类(并且让sylius决定是否将其转换为实体,因此您可以保持灵活性以便以后轻松扩展),您需要仔细观察sylius如何声明其捆绑。
遵循 SyliusResourceBundle 为我做了一个伎俩。
I found and successfully used documentation on how to override an existing model in Sylius, but I haven't been able to create a completely new one making use of the SyliusResourceBundle. Im guessing it's easy if you already know Symfony2? Im still learning, so here is what I have... what am I missing?
Im using a working full Sylius install as my base, so I started here http://sylius.org/blog/simpler-crud-for-symfony2 I've got my own "Astound Bundle" setup and several overrides and controllers working out of that. I added this to my config:
sylius_resource:
resources:
astound.location:
driver: doctrine/orm
templates: AstoundWebBundle:Location
classes:
model: Astound\Bundle\LocationBundle\Model\Location
Then I made:
<?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;
}
}
Along With:
<?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);
}
Based on studying existing Models in Sylius and looking through Doctrine documentation, I made this as well:
<?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>
With that in place I was expecting to be able to run app/console doctrine:schema:update --dump-sql and see my new table called "Locations" in my database, but instead I get:
Nothing to update - your database is already in sync with the current entity metadata.
I noticed in the app/console container:debug that I have the
following services:
astound.controller.location
container Sylius\Bundle\ResourceBundle\Controller\ResourceController
astound.manager.location
n/a alias for doctrine.orm.default_entity_manager
astound.repository.location
container Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
So I attempted to add a route to the indexAction on the controller. Added to my administration main routing config file:
astound_location_index:
pattern: /location
methods: [GET]
defaults:
_controller: astound.controller.location:indexAction
However when I try to go to the route *app_dev.php/administration/location* in my browser I get:
The class 'Astound\Bundle\LocationBundle\Model\Location' was not found
in the chain configured namespaces
And in doing a few more searches while writing this I found http://brentertainment.com/other/docs/book/doctrine/orm.html it sounds like php classes in the Entities folder should magically show up in app/console doctrine:mapping:info or "chain configured namespaces"?, but Sylius has no Entity folders anywhere, so there must be some hidden magic happening... Im guessing it's in the Base Bundle File? I tried my best to copy what other Bundles in Sylius do, I made this:
<?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'));
}
}
But that gives me this:
ParameterNotFoundException: You have requested a non-existent
parameter "astound_location.driver".
I tried adding this in my config:
astound_location:
driver: doctrine/orm
But then I get this error:
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"
Thank you to anyone who read the above novel! The answer has got to be simple?! Whats missing?
解决方案 I just came across the same need, i.e. to create a new model / entity while extending Sylius. My first attempt was to also add my new model to the config of sylius_resource. This resulted in the same "Nothing to update" message when running doctrine:schema:update.
After some digging I found that my new model, which I defined as "mapped-superclass", unlike other Sylius models, was not being "magically" converted into an "entity", thus doctrine did not see any need to produce a database table for it.
So I guess the quick solution would be to simply change the doctrine mapping from "mapped-superclass" into "entity". E.g. In your example:
change:
model: Astound\Bundle\LocationBundle\Model\Location to
model: Astound\Bundle\LocationBundle\Entity\Location
and change:
mapped-superclass name="Location" table="Locations" to
entity name="Location" table="Locations"
But if you prefer keeping your model a mapped-superclass (and let sylius decide if it should be converted it into an entity, so you can maintain the flexibility to easily extend it later), you need to take a closer look at how sylius declares their bundles.
Following the "Advanced configuration" of SyliusResourceBundle did the trick for me.
这篇关于如何通过SyliusResourceBundle与Sylius创建一个新的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!