-2

At the beginning sorry for my poor English, I hope you understand me. I'm writing a simple portal in Symfony2 and came to the point where it needs to make relationships between tables with MySQL, all the ways of the internet browsed, tested and nothing came of it. The tables below.

https://i.stack.imgur.com/vR77x.png

https://i.stack.imgur.com/GDXDw.png

Now yes, by getting the user from the database, I would like to once stretched to the profession (vocation), but together with its name, is even an option?

alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
Danon
  • 29
  • 1
  • 7
  • 1
    possible duplicate of [Symfony ONE-TO-ONE relation](http://stackoverflow.com/questions/11190308/symfony-one-to-one-relation) – RiggsFolly Sep 16 '15 at 15:09

1 Answers1

0

If I understand correctly you want to create a OneToOne relationship between your Entities?!

On the Player entity:

/**
 * Player
 *
 * @ORM\Table(name="players")
 * @ORM\Entity()
 */
class Player
{
    /**
     * @ORM\OneToOne(targetEntity="Vocation", inversedBy="player")
     * @ORM\JoinColumn(name="vocation", referencedColumnName="id")
     */
    private $vocation;

    ...
}

And at the Vocation one

/**
 * Vocation
 *
 * @ORM\Table(name="vocations")
 * @ORM\Entity()
 */
class Vocation
{
    /**
     * @ORM\OneToOne(targetEntity="Player", mappedBy="vocation")
     */
    private $player;

    /**
     * @var string
     */
    private $vocationName;

    /**
     * @var integer
     */
    private $id;

    ...
}

Something like this?

Also (from looking at your tables) maybe you possibly want a ManyToOne relationship instead of a OneToOne?

Restless
  • 538
  • 1
  • 15
  • 29
  • Still it does not work, I do not know what I'm doing wrong, below code http://wklej.org/id/1797836/ http://wklej.org/id/1797837/ – Danon Sep 16 '15 at 15:42
  • You're using `inversedBy="player"` but your "DanonaccVocations" entity does not contain that property. Also, adding the `@ORM\Entity()` annotation to your entities wouldn't hurt. – Restless Sep 16 '15 at 15:50
  • Changed `inversedBy="player"` to `inversedBy="vocationName"`, added `@ORM\Entity()` annotation and nothing, i test it so http://wklej.org/id/1797866/ and get `int 2` in answer. – Danon Sep 16 '15 at 16:01
  • Can you share how your Vocations entity currently looks like (after the last change)? – Restless Sep 16 '15 at 16:06
  • You should add a "player" property and not use the "vocationName" one for the relation. Check my edited answer. Also, where is your columns mapping (`@ORM\Column()`)? – Restless Sep 16 '15 at 16:31
  • What it is `@ORM\Column()`? Im a begginer in sf2 and annotations. – Danon Sep 16 '15 at 16:41
  • You might want to take a look at [Doctrine Documentation](http://doctrine-orm.readthedocs.org/en/latest/index.html) then. Also this might be helpful: http://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class – Restless Sep 16 '15 at 16:51
  • There also a non-official [hands-on tutorial called Jobeet](http://intelligentbee.com/blog/2013/08/07/symfony2-jobeet-day-1-starting-up-the-project/) That might be helpful for you to start with SF2. It was written while ago though, so the symfony version 2.3 was used at that time. – Restless Sep 16 '15 at 16:54
  • I read the documentation doctrine and even the instance of the parties did not want me to work, any suggestions? I will be very grateful, I appreciate anyone's knowledge. http://wklej.org/id/1798121/ http://wklej.org/id/1798122/ – Danon Sep 16 '15 at 23:33