1
votes

I have problem with doctrine 2. I have following db tables : So, Doctrine generate entities which retrive data from desk settings for site, but I need, to retrieve all settings from desk_settings table and overwrite its values from desk_settings_values table using desk_id

DB image -> https://docs.google.com/file/d/0B7rOFTJGfJwTWEQ3bXZFU1hXZlU/edit?usp=sharing

Doctrine entities which was generate with script:

/**
 * Desk
 *
 * @ORM\Table(name="desk")
 * @ORM\Entity
 */
class Desk
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", length=100, nullable=false)
     */
    private $code;

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="PayBox\Entity\DeskSettings", mappedBy="desk")
     */
    private $deskSettings;
}


use Doctrine\ORM\Mapping as ORM;

/**
 * DeskSettings
 *
 * @ORM\Table(name="desk_settings")
 * @ORM\Entity
 */
class DeskSettings
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="setting_key", type="string", length=100, nullable=false)
     */
    private $settingKey;

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="PayBox\Entity\Desk", inversedBy="deskSettings")
     * @ORM\JoinTable(name="desk_settings_values",
     *   joinColumns={
     *     @ORM\JoinColumn(name="desk_settings_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="desk_id", referencedColumnName="id")
     *   }
     * )
     */
    private $desk;
 }

1
Can you remove the SQL schema definition and the Getters & Setters and post your 3 entities in full but without methods (getters & setters) ?Flip
Done. Doctrine generate only 2 entities, its skip DeskSettingsValues EntityDmitry
"overwrite its values from desk_settings_values table using desk_id". Why does desk_settings_values got to have a relations with desk? This seems to more obvious choice for a schema: Many Desks to Many Desk_settings, One Desk_setting to Many Desk_settings_values. By the way a picture of your database schema would be nice also.Flip
Actually .. on reconsideration .. if DeskSettings is like a DeskSettingsDefinition then desk_settings_values could be DeskSettings. Then it would make sense to have Many Desks to Many DeskSettings, One DeskSettings to Many DeskSettingsDefinitions. So then DeskSettingsDefinitions (which is in your schema: DeskSettings) would not have a direct relationship to desk. The DeskSettingsDefinitions would hold things unique for each DeskSetting. And the DeskSetting would hold values which are unique for the Desk.Flip
Sorry, can't add image, I haven't 10 posts yet. Whith this aproach, values get from desk_setting, so it's default values, that can be redifined in desk_settings_values or can be used defaultsDmitry

1 Answers

2
votes

From Doctrine documentation:

Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.

You need to change your class structure to one-to-many/many-to-one. See this blog.