Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

Symfony 4 (Doctrine 2.7) Joined inheritance

$
0
0

I would like to split users per domain basis in my Symfony project.

My Classes so far:

<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Domain
{
    /**
     * @ORM\Id
     * @ORM\Column(type="smallint")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", unique=true, length=255, nullable=false)
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="domain")
     */
    private $users;
}

<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="domain_id", type="smallint")
 * @ORM\DiscriminatorMap({"0":"App\Entity\User","1":"App\Entity\User1","2":"App\Entity\User2","3":"App\Entity\User3"})
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", options={"unsigned":true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Domain", inversedBy="users")
     * @ORM\JoinColumn(name="domain_id", referencedColumnName="id")
     */
    private $domain;
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 */
class User1 extends \App\Entity\User
{
    /**
     * @ORM\Column(type="string", unique=true, length=255, nullable=false)
     */
    private $email;
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 */
class User2 extends \App\Entity\User
{
    /**
     * @ORM\Column(type="string", unique=true, length=255, nullable=false)
     */
    private $email;
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 */
class User3 extends \App\Entity\User
{
    /**
     * @ORM\Column(type="string", unique=true, length=255, nullable=false)
     */
    private $email;
}

when I run:

C:\Users\user\PhpstormProjects\test>php bin\console doctrine:schema:create --dump-sql
In SchemaException.php line 119:
  The column 'domain_id' on table 'user' already exists.
doctrine:schema:create [--dump-sql] [--em [EM]] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
C:\Users\user\PhpstormProjects\test>

As we can see, domain_id is conflicting with discriminator field and I there is no point to duplicate it with the same value. The general idea is to separate users and domains by using different tables. For single table inheritance it would be easy, but for performance reasons I must to split them. I thought, maybe there is any other solution?


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>