[Answered] Unnecessary mapped-superclass problem

0 votes
asked Nov 21, 2014 in Solved by quleczka (160 points)

Hi!

I try to bulid my own schema. I have classes from ApplicationSonataUserBulnde whch extends SonataUserBundle.

Original vendor\sonata-project\user-bundle\Resources\config\doctrine\ BaseUser.orm.xml is:

<?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">

    <mapped-superclass name="Sonata\UserBundle\Entity\BaseUser">
        <field name="createdAt"    type="datetime"   column="created_at" />
        <field name="updatedAt"    type="datetime"   column="updated_at" />

        <!-- Profile fields -->
        <field name="dateOfBirth"  type="datetime"   column="date_of_birth"            nullable="true" />
        <field name="firstname"    type="string"     column="firstname" length="64"    nullable="true" />
        <field name="lastname"     type="string"     column="lastname"  length="64"    nullable="true" />
        <field name="website"      type="string"     column="website"   length="64"    nullable="true" />
        <field name="biography"    type="string"     column="biography" length="255"   nullable="true" />
        <field name="gender"       type="string"     column="gender"    length="1"     nullable="true" />
        <field name="locale"       type="string"     column="locale"    length="8"     nullable="true" />
        <field name="timezone"     type="string"     column="timezone"  length="64"    nullable="true" />
        <field name="phone"        type="string"     column="phone"     length="64"    nullable="true" />

        <!-- social fields -->
        <field name="facebookUid"     type="string"     column="facebook_uid"  length="255"    nullable="true" />
        <field name="facebookName"    type="string"     column="facebook_name"  length="255"    nullable="true" />
        <field name="facebookData"    type="json"       column="facebook_data"  nullable="true" />
        <field name="twitterUid"      type="string"     column="twitter_uid"  length="255"    nullable="true" />
        <field name="twitterName"     type="string"     column="twitter_name"  length="255"    nullable="true" />
        <field name="twitterData"     type="json"       column="twitter_data"  nullable="true" />
        <field name="gplusUid"        type="string"     column="gplus_uid"  length="255"    nullable="true" />
        <field name="gplusName"       type="string"     column="gplus_name"  length="255"    nullable="true" />
        <field name="gplusData"       type="json"       column="gplus_data"  nullable="true" />

        <!-- extra security fields -->
        <field name="token"                   type="string" length="255" column="token" nullable="true" />
        <field name="twoStepVerificationCode" type="string" length="255" column="two_step_code" nullable="true" />

        <lifecycle-callbacks>
            <lifecycle-callback type="prePersist" method="prePersist" />
            <lifecycle-callback type="preUpdate" method="preUpdate" />
        </lifecycle-callbacks>
    </mapped-superclass>

</doctrine-mapping>

ApplicationSonataUserBundle has original src\Application\Sonata\UserBundle\Resources\config 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="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">

        <id name="id" column="id" type="integer">
            <generator strategy="AUTO" />
        </id>

    </entity>

</doctrine-mapping>

When I import it and my one association and then export I get User.orm.xml:

<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <mapped-superclass name="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
    <id name="id" type="integer" column="id">
      <generator strategy="AUTO"/>
    </id>
    <field name="username" type="string" length="255" nullable="true" column="username"/>
    <field name="usernameCanonical" type="string" unique="true" length="255" nullable="true" column="username_canonical"/>
    <field name="email" type="string" length="255" nullable="true" column="email"/>
    <field name="emailCanonical" type="string" unique="true" length="255" nullable="true" column="email_canonical"/>
    <field name="enabled" type="boolean" nullable="true" column="enabled"/>
    <field name="salt" type="string" nullable="true" column="salt"/>
    <field name="password" type="string" nullable="true" column="password"/>
    <field name="lastLogin" type="datetime" nullable="true" column="last_login"/>
    <field name="locked" type="boolean" nullable="true" column="locked"/>
    <field name="expired" type="boolean" nullable="true" column="expired"/>
    <field name="expiresAt" type="datetime" nullable="true" column="expires_at"/>
    <field name="confirmationToken" type="string" nullable="true" column="confirmation_token"/>
    <field name="passwordRequestedAt" type="datetime" nullable="true" column="password_requested_at"/>
    <field name="roles" type="array" nullable="true" column="roles"/>
    <field name="credentialsExpired" type="boolean" nullable="true" column="credentials_expired"/>
    <field name="credentialsExpireAt" type="datetime" nullable="true" column="credentials_expire_at"/>
    <one-to-many field="store" target-entity="Krawet\AppBundle\Entity\Store" mapped-by="user"/>
  </mapped-superclass>
</doctrine-mapping>

When I try to update doctrine schema I get error about duplicate username as it apparently is duplicate.

I also can't use bidirectional association when User is change to mapped-superclass (http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html).

Is there a way to and bidirectional reference to my Krawet\AppBundle\Entity\Store and have entrity in User.orm.xml instead od mapped-superclass?

1 Answer

+1 vote
answered Nov 21, 2014 by ludek.vodicka Skipper developer (140,450 points)
selected Nov 21, 2014 by quleczka
 
Best answer

Hi,

I have tried to import your two files but I didn't see any problem. When I imported and exported the files, content of file User.orm.xml remains the same (only reformated).
This is your project:

enter image description here

and this is User.orm.xml

<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
    <id name="id" type="integer" column="id">
      <generator strategy="AUTO"/>
    </id>
  </entity>
</doctrine-mapping>

I don't understand what you mean by "When I import it and my one association and then export", could you please explain it?

Also you post user and userBase class and in the bottom part of your post you're talking about Store.

I'm not sure if this is a single question or multiple questions, but could you please describe it more in details and each question separately?

Thanks

commented Nov 21, 2014 by quleczka (160 points)

It's difficult to explain it here as there is no way to attach files. I uploaded it to dropbox - https://www.dropbox.com/sh/2d5neb85or3id7o/AAAnBhTWtuPIiQ74gGRpCFbHa?dl=0

The problem is that when I select whole main folder of my project I get project as in "magazyn_wrong.skipper". When I export these back to ORM I have the problem which I described.

When I copied only part of magazyn to another location ("before" for dropbox) and then I created project again I got "magazyn_correct.skipper".

Looks like some other sonata/symfony/fos files are causing the problem.

If I make model only from "src" folder than it works fine for my purpose. I don't see parent class but at least I can save it back correctly.

commented Nov 21, 2014 by ludek.vodicka Skipper developer (140,450 points)

Thanks for projects. From what you're describing and what I see in the project files, it's a probably a problem with a duplicated entity names differentiated only by namespace.

Unfortunately Skipper currently can't handle multiple entities with the same name in different namespaces and if this situation occurs, entities are merged by name.

It's one of our top priority task we have in our todo list, but currently there isn't any other workaround than making entity names unique.

commented Nov 21, 2014 by quleczka (160 points)

Ok, that makes clear why sometimes I have just one id and sometimes id1, id2 as well. Thanks for explaining what's going on.

Now I know it was FOS user bundle which made that "mess" as it uses the same names as my AppUserBundle. Unfortunately can't change names easily without messing everything in Sonata Project. I'll just skip FOS user bundle while importing to sipper.

I also noticed another problem - sometimes I select export as Doctrine2 XML and content of the files is XML but the name is User.php.

Any idea what is causing this?

commented Nov 21, 2014 by ludek.vodicka Skipper developer (140,450 points)

This is because you originally imported your project as php files, and after that you probably changed export output to XML.

But during the import process each entity has configured it's own output file name because of correct export consistency. For new entities skipper automatically creates file names, but for existing ones it's necessary to keep naming-convention introduced by user. Because of that file name is forced by import.

For your changed entities i's sufficient to clear "export-file-name" property.

Doctrine2 properties

commented Dec 13, 2014 by quleczka (160 points)

Thanks a lot for this hint. It was exactly this case.

commented Feb 21, 2015 by Doug (200 points)

Would you say the best way around this, when building ER's would be to use "Application/Sonata/UserBundle/Resources/config/Doctrine/user.orm.xml" as created by SonataEasyExtends as our base user entity? Leaving it blank and setting relation's to it?

...