infinite-networks / InfiniteFormBundle

A collection of useful form types and extensions for Symfony.
169 stars 40 forks source link

SubType #50

Closed 3kynox closed 8 years ago

3kynox commented 8 years ago

Hello again,

I face currently another problem about defining "_sub-sub-type_".

If i re-take my last example available at https://github.com/infinite-networks/InfiniteFormBundle/issues/48, I got a main formType MoyenCommType that countains childrens :

Now I wish to extends TelephoneType -> MobileType

Then :

// brunoBundle/Entity/Mobile.php

namespace brunoBundle\Entity;

/**
 * Mobile
 */
class Mobile extends Telephone
{
    /**
     * @var boolean
     */
    protected $smsing;

    /**
     * Set smsing
     *
     * @param boolean $smsing
     *
     * @return Mobile
     */
    public function setSmsing($smsing)
    {
        $this->smsing = $smsing;

        return $this;
    }

    /**
     * Get smsing
     *
     * @return boolean
     */
    public function getSmsing()
    {
        return $this->smsing;
    }
}
#brunoBundle/Resources/config/doctrine/Telephone.orm.yml

brunoBundle\Entity\Telephone:
    type: entity
    table: null
    repositoryClass: brunoBundle\Entity\TelephoneRepository
    inheritanceType: JOINED
    discriminatorColumn:
        name: type
        type: integer
    discriminatorMap:
        1: Mobile
    fields:
        numero:
            type: string
            length: 255

    lifecycleCallbacks: {  }
// brunoBundle/Form/ContactType
// ...
->add('moyensComm', 'infinite_form_polycollection', array(
    'types' => array(
        'brunobundle_moyencomm',
        'brunobundle_telephone',
        'brunobundle_email',
        'brunobundle_adresse',
        'brunobundle_mobile'
    ),
// brunoBundle/Form/MobileType.php

namespace brunoBundle\Form;

use Symfony\Component\Form\FormBuilderInterface;

class MobileType extends TelephoneType
{
    protected $dataClass = 'brunoBundle\Entity\Mobile';

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder
            ->add('smsing')
        ;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'brunobundle_mobile';
    }
}

I finally updated database and added a first entry in the mobile table.

Now I try to get that value from my polycollection theme, prototype exists but that children appears nowhere from my dumps.

Any idea ?

jmclean commented 8 years ago

That all seems to work for me, but my tests aren't using a database. Are you sure Doctrine is giving you a Mobile object? If you dump it before the form is created, what do you get?

3kynox commented 8 years ago

Hello and thanks for help (again ^^)

Doctrine not giving Mobiles but Telephones, that explain why there is nothing related to Mobile.

What I first did is to set a fourth type in MoyenComm.orm.yml :

brunoBundle\Entity\MoyenComm:
    type: entity
    table: null
    repositoryClass: brunoBundle\Entity\MoyenCommRepository
    inheritanceType: JOINED
    discriminatorColumn:
        name: type
        type: integer
    discriminatorMap:
        1: Telephone
        2: Email
        3: Adresse
        4: Mobile
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    manyToOne:
        contact:
            targetEntity: Contact
            inversedBy: moyensComm

    lifecycleCallbacks: {  }

After I modify in database one Telephone entry, to set it with type Mobile (type 4) and that phone number got a smsing checkbox now in form.

But I'm kind of out of logic here, because how I set other telephone numbers to mobile or the inverse ? No checkbox available for other phone numbers so not possible.

I'm looking actually at another inheritance doctrine type : MappedSuperClass.

With this, Telephones will have Mobile field 'smsing'. But that don't work.

Here is the mapping info now :

// Back to initial state

brunoBundle\Entity\MoyenComm:
    type: entity
    table: null
    repositoryClass: brunoBundle\Entity\MoyenCommRepository
    inheritanceType: JOINED
    discriminatorColumn:
        name: type
        type: integer
    discriminatorMap:
        1: Telephone
        2: Email
        3: Adresse
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    manyToOne:
        contact:
            targetEntity: Contact
            inversedBy: moyensComm

    lifecycleCallbacks: {  }

and Telephone orm :

brunoBundle\Entity\Telephone:
    type: MappedSuperClass
    table: null
    repositoryClass: brunoBundle\Entity\TelephoneRepository
    fields:
        numero:
            type: string
            length: 255
    OneToOne:
        mappedRelated1:
            targetEntity: brunoBundle\Entity\Mobile
            joinColumn:
              name: id
              referencedColumnName: id
              onDelete: cascade

    lifecycleCallbacks: {  }

But it returns error :

> C:\wamp\bin\php\php5.5.12\php.exe app/console doctrine:mapping:info
Found 7 mapped entities:
[OK]   brunoBundle\Entity\Adresse
[OK]   brunoBundle\Entity\Contact
[OK]   brunoBundle\Entity\Email
[FAIL] brunoBundle\Entity\Mobile
Class "brunoBundle\Entity\Telephone" sub class of "brunoBundle\Entity\MoyenComm" is not a valid entity or mapped super class.

[OK]   brunoBundle\Entity\MoyenComm
[FAIL] brunoBundle\Entity\Telephone
Class "brunoBundle\Entity\Telephone" sub class of "brunoBundle\Entity\MoyenComm" is not a valid entity or mapped super class.

[OK]   brunoBundle\Entity\WebLink

Process finished with exit code 1 at 11:38:14.
Execution time: 3 126 ms.
jmclean commented 8 years ago

I can't provide support for Doctrine or Symfony in general. Do you have any more questions about InfiniteFormBundle specifically?

3kynox commented 8 years ago

Not yet. Thanks anyway, I'll sort this doctrine issue out.