2
votes

Currently using: base install of sylius-standard, for an ecommerce website.

Here is what I would like to accomplish : Basically we know there is an User Entity which resides in

vendor/sylius/sylius/src/Sylius/Component/Core/Model/User.php

This Entity is actually extending the FOS\UserBundle\Model\User and it also has a linked UserInterface which is implementing FOS\UserBundle\Model\UserInterface.

My goal is to create another entity UserData which can be linked via the Doctrine OneToMany field relation to the above sylius User Entity which is basically the table sylius_user.

So effectively User Entity linked to UserData Entity via oneToMany ( For each user there can be multiple UserData entity instances.)

Sylius documentation (http://sylius.readthedocs.org) does not have anything related to adding an entity - If I am mistaken, please guide me to the correct links.

I have read overriding models at - > sylius models override also have gone through another sort of related question on stackoverflow How to create a new model with Sylius via SyliusResourceBundle but it is not clear as he is using the SyliusResourceBundle while I want to extend the SyliusCoreBundle (not sure if we can extend it in the first place or not)

How do I achieve this in the sylius framework ?

1

1 Answers

2
votes

I solved the problems by reading segregated posts, so putting in here the solution for the community (please let me know if this is not the recommended way and the correct way if not):

My goal was to create a oneToMany doctrine relationship between

Sylius\Component\Core\Model\User

And

Acme\Bundle\Entity\UserData

which I had created. Basically for every user there could be multiple userdata instances pointing back to that user. To do this, at first I followed the docs at sylius documentation to override the sylius user class which is infact an extended class of fosuserbundle.

At some stage I kept getting the error [Doctrine\DBAL\Schema\SchemaException]
The table with name 'astrohealing_dev.sylius_user' already exists.

Based on Winzou's suggestion : To fix this error I changed the user class locations in sylius.yml (Sylius/Bundle/CoreBundle/Resources/config/app/sylius.yml) which is part of the core bundle and declared my own class as the user model like this : sylius.yml

This resolved the sylius_user table exists error

But then I got another error :

[Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException]
Configuration path "sylius_core.driver" cannot be overwritten. You have to define all options for this path, and any of its sub
-paths in one configuration section.

Next I commented out drive : doctrine/orm line for the relevant entry in

app/config/config.yml

    sylius_core:
#    driver: doctrine/orm
    classes:
        user:
            model: Acme\Bundle\Entity\User
            
sylius_resource:
    resources:
        sylius.userdata:
            driver: doctrine/orm
            classes:
                model: Acme\Bundle\Entity\UserData

The above few lines also show :

  • The override for sylius_core with my new User class
  • The new resource - which is bascially just the userdata class

Now for the Doctrine Mapping (User.orm.xml):

<?xml version="1.0" encoding="UTF-8"?>

<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://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="Acme\Bundle\Entity\User" table="sylius_user">
        <one-to-many
            field="userdata"
            target-entity="Acme\Bundle\Entity\UserData"
            mapped-by="userid" />
    </entity>
</doctrine-mapping>

And the mapping for UserData (UserData.orm.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<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://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="Acme\Bundle\Entity\UserData" table="sylius_userdata">
        <id name="id" type="integer">
            <generator strategy="AUTO" />
        </id>
        <many-to-one 
            field="userid" 
            target-entity="Acme\Bundle\Entity\User"
            inversed-by="userdata"
            join-column="userid">
            <join-column name="user_id" referenced-column-name="id" nullable="false" />
        </many-to-one>

        <field name="name" type="string" length="150" />
        <field name="datetime" type="datetime" />
    </entity>
</doctrine-mapping>