Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top