0

I'm making entities with Symfony2 and Doctrine2. I made some entities that represent a many-to-many relation between two of my entities.

An example of one of these entities :

/**
 * @ORM\Entity
 */
class Contact_Conference_Invitation
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Aurae\UserBundle\Entity\Contact")
     */
    private $contact;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Aurae\ConferenceBundle\Entity\Conference")
     */
    private $conference;

    /**
     * @var datetime dateInvitation
     *
     * @ORM\Column(name="dateInvitation", type="datetime")
     */
    private $dateInvitation;

    //Getters and setters
}

I have tried updating my sql schema, but the tables corresponding to these entities do not appear. Is there somewhere I have to declare them (config or such)? If not, what's wrong?

Thanks a lot

Edit : I had forgotten the namespace for these class, and that's why they were omitted by Doctrine. Another case closed :) thanks for the answers!

6
  • do you have the mapping class as a use statement in the top use Doctrine\ORM\Mapping as ORM; Commented Jun 11, 2012 at 13:45
  • Yep I have... Do I have to use the two classes I link as well? Commented Jun 11, 2012 at 13:47
  • No you don't. You don't seem to have any inversedBy="x" statements in your manytoone declarations. Not sure that would fix it but its definately good practice. Commented Jun 11, 2012 at 13:49
  • And some join column statements too i guess: @ORM\JoinColumn(name="conference_id", referencedColumnName="id", onDelete="SET NULL", nullable=true) Commented Jun 11, 2012 at 13:50
  • Though these aren't inversed by anything at the moment. Should I make a $invitations array in contact and make it the inverse of $contact here? Commented Jun 11, 2012 at 13:52

3 Answers 3

3

Assumptions ...

No, you don't need to declare them anywhere else than in your Entity directory.

  • What's the error message you got?

  • I guess you added

    use Doctrine\ORM\Mapping as ORM;

on the top of your classes to let them be mapped.

I tried ...

I tried to generate your entities by adding a simple Contact & Conference entities and it's working fine. Here are the code snippets:

Contact_Conference_Invitation.php

namespace Ahsio\StackBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Contact_Conference_Invitation
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Ahsio\StackBundle\Entity\Contact")
     */
    private $contact;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Ahsio\StackBundle\Entity\Conference")
     */
    private $conference;

    /**
     * @var datetime dateInvitation
     *
     * @ORM\Column(name="dateInvitation", type="datetime")
     */
    private $dateInvitation;

    //Getters and setters
}

Contact.php

namespace Ahsio\StackBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Contact
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @param $id
     */    
    public  function setId($id)
    {
        $this->id = $id;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}

Conference.php

namespace Ahsio\StackBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Conference
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @param $id
     */
    public  function setId($id)
    {
        $this->id = $id;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}

Here are the generated tables:

enter image description here

NB: I used a specific namespace for the entities generation to work fine, you need to change them.

Sign up to request clarification or add additional context in comments.

Comments

1

Also don't forget to check that you have automapping enabled.

In your config.yml you should have

doctrine:
    orm:
        auto_mapping: true

Came across this question because my entities weren't generated as well, this was my issue, it could save some time to people struggling with the same issue.

Comments

0

I don't see the definition of your ManyToMany relation in the sample of code you provided.

As an example, here's a ManyToMany relationship I implemented for a project

Entity Project.php

/**
 * @var Provider[]
 *
 * @ORM\ManyToMany(targetEntity="Provider", mappedBy="projects")
 */
protected $providers = null;

Entity Provider.php

/**
 * @var Project[]
 *
 * @ORM\ManyToMany(targetEntity="Project", inversedBy="providers")
 * @ORM\JoinTable(name="PROVIDER_PROJECT")
 */
protected $projects = null;

As you can see, here you define the join table for your ManyToMany relationship.

Of course those entities are specific for my particular project but you get the idea and you can adapt it easily for your needs.

1 Comment

The OP's post uses an extra field in his many to many table so he cannot do it this way, his post is of his actual many to many table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.