草庐IT

php - 如何通过 SyliusResourceBundle 使用 Sylius 创建新模型

coder 2024-04-21 原文

我找到并成功使用了有关如何在 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\ResourceController

astound.manager.location
n/a alias for doctrine.orm.default_entity_manager

astound.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/

有关php - 如何通过 SyliusResourceBundle 使用 Sylius 创建新模型的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  4. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  5. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  6. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  7. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  8. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  9. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  10. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

随机推荐