Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OneToOne: Support for polymorphic associations #6140

Open
ihorsamusenko opened this issue Nov 20, 2016 · 1 comment
Open

OneToOne: Support for polymorphic associations #6140

ihorsamusenko opened this issue Nov 20, 2016 · 1 comment

Comments

@ihorsamusenko
Copy link

ihorsamusenko commented Nov 20, 2016

Case:
I have plenty of entities in the system. I also have entity_alias table which consist of two fields: alias (pk), entity_id. (NOTE: in my database all the entities have a unique id across the whole database, that is why I do not have entity_type field in entity_alias table).

Goal:
Create a trait which can be used to any entity and which provides an ability to set, get and drop alias for an entity.

Code example:
Alias entity:

/**
 * Class Alias
 *
 * @ORM\Entity()
 * @ORM\Table("entity_alias")
 * @UniqueEntity("entityId")
 */
class Alias
{
    /**
     * @var string
     * @Assert\NotBlank()
     * @ORM\Column(type="bigint", nullable=false)
     */
    private $entityId;
    /**
     * @var string
     *
     * @ORM\Id
     * @ORM\Column(type="string", nullable=false)
     */
    private $alias;

    /**
     * Alias constructor.
     *
     * @param $objectId
     * @param $alias
     */
    public function __construct($objectId, $alias)
    {
        $this->alias = $alias;
        $this->objectId = $objectId;
    }
    /**
     * @return mixed
     */
    public function __toString()
    {
        $sting = $this->alias;
        return (string) $sting;
    }
}

AliasableTrait:

trait AliasableTrait
{
   /**
    * @ORM\OneToOne(targetEntity="Alias", cascade={"persist", "remove"}, orphanRemoval=true)
    * @ORM\JoinColumn(name="id", referencedColumnName="entity_id", onDelete="CASCADE")
    * @var Alias
    */
    protected $alias;
    /**
     * @return string|int
     */
    abstract public function getId();
    /**
     * @param string $alias
     */
    public function setAlias(string $alias)
    {
        $this->alias = new Alias($this->getId(), $alias);
    }
    /**
     * @return string
     */
    public function getAlias() : string
    {
        return (string) $this->alias;
    }
    public function dropAlias()
    {
        $this->alias = null;
    }
}

Right now I am getting Missing value for primary key alias on Alias.

Question:
Is it possible to do?

@malutanpetronel
Copy link

did you manage to make it work ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants