Search code examples
phppostgresqlsymfonydoctrine-ormdoctrine-mapping

Symfony One-To-Many unidirectional relation without JOIN table


I have the following tables:

strings
------
"id" int not null primary key

string_texts
------
"id"          int not null primary key
"string_id"   int not null fk(strings.id)
"language_id" int not null fk(languages.id)
"text"        text

"countries"
------
"id"      int not null primary key,
"name_id" int not null fk(strings.id)

All localizable text stored in a single table, and every other table connected to that table.

What I dont know is how to write the Country model? This is how I got so far.

namespace LoginHood\HoodBundle\Entity\Geo;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

class Country
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     */
    protected $id;

    /**
     * @var Collection
     *
     * @ORM\OneToMany(targetEntity="StringText", mappedBy="")
     */
    protected $names;

    /*
    ...
    */


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

Because of the structure you can't get the Country or any Entity from the StringText entity. But I don't want to make a join table, because its kind of an overkill, and totally meaningless.


Solution

  • You are reinventing the wheel, you have to use the DoctrineExtensions / Translatable behavior that will do the whole job for you:

    • Deal with model modifications, almost like what you are doing
    • Get directly the string in the correct language, when getting it
    • ...