doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4.03k stars 1.27k forks source link

[Sluggable] Inheritance #1616

Closed faabiosr closed 2 years ago

faabiosr commented 8 years ago

[Sluggable] Inheritance

When I tried to use the slug feature with class inheritance, one issue happened, this issue is related when the slug was generated, because the sluggable is not able to check the class inheritance.

Look at the scenario below:

Field - Doctrine Mapping

AppBundle\Document\Field:
    type: mappedSuperclass
    fields:
        id:
            id:  true
        name:
            type: string
            length: 255
            nullable: false
        slug:
            type: string
            length: 255
            gedmo:
                slug:
                    separator: -
                    updatable: false
                    fields:
                        - name

Field - Document Class

<?php

namespace AppBundle\Document;

abstract class Field
{
    protected $id;

    protected $name;

    protected $slug;

    public function __construct($name)
    {
        $this->name = $name;
    }
}

Textarea - Document Mapping

AppBundle\Document\Textarea:
    type: embeddedDocument
    fields:
        value:
            type: string
            length: 255
            nullable: false

Textarea - Document Class

<?php

namespace AppBundle\Document;

class Textarea extends Field
{
    protected $value = ''

    public function setValue($value)
    {
        $this->value = $value;
    }
}

ComboBox - Doctrine Mapping

AppBundle\Document\ComboBox:
    type: embeddedDocument
    fields:
        values:
            type: collection
            nullable: false

ComboBox - Document Class

<?php

namespace AppBundle\Document;

class ComboBox extends Field
{
    protected $values = [];

    public function setValues(array $values)
    {
        $this->values = $values;
    }
}

Form - Doctrine Mapping

AppBundle\Document\Form:
    type: document
    collection: form
    fields:
        id:
            id:  true
        name:
            type: string
            length: 255
            nullable: false
        slug:
            type: string
            length: 255
            gedmo:
                slug:
                    separator: -
                    updatable: false
                    fields:
                        - name
    embedMany:
        fields:
            discriminatorField:
                name: type
                type: string
            discriminatorMap:
                textarea: AppBundle\Document\Textarea
                combobox: AppBundle\Document\ComboBox
            defaultDiscriminatorValue: textarea
            strategy: set

Form - Document Class

<?php

namespace AppBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;

class FormRules
{
    protected $id;

    protected $name;

    protected $slug;

    protected $fields;

    public function __construct($name)
    {
        $this->name = $name;
        $this->fields = new ArrayCollection();
    }

    public function addField(Field $field)
    {
        $this->fields->add($field);
    }
}

Controller Testing

$dm = $this->get('doctrine.odm.mongodb.document_manager');

$form = new Form('Info');

$textarea1 = new Textarea('Age');
$textarea1->setValue('Lorem ipsum');
$form->addField($textarea1);

$textarea2 = new Textarea('Age');
$textarea2->setValue('Lorem ipsum');
$form->addField($textarea2);

$combobox1 = new ComboBox('Age');
$combobox1->setValues([10, 16, 21, 43]);
$form->addField($combobox1);

$dm->persist($form);
$dm->flush();

Database data:

{
    "_id": ObjectId("5763d35ba5439a00120628f9"),
    "name": "Info",
    "slug": "info",
    "fields": [{
        "_id": ObjectId("5763d35ba5439a00120628fa"),
        "name": "Age",
        "slug": "age",
        "value": "Lorem ipsum",
        "type": "textarea"
    }, {
        "_id": ObjectId("5763d35ba5439a00120628fb"),
        "name": "Age",
        "slug": "age-1",
        "value": "Lorem ipsum",
        "type": "textarea"
    }, {
        "_id": ObjectId("5763d35ba5439a00120628fc"),
        "name": "Age",
        "slug": "age",
        "values": [10, 16, 21, 43],
        "type": "combobox"
    }]
}

The slug generator works fine with same class type, but not with inheritance.

How can I fix this problem?

l3pp4rd commented 8 years ago

well, I'm personally haven't worked with php in a year and adding new features for embedded fields probably is out of my scope for good. unless someone contributes to this library.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.