简体   繁体   中英

Symfony2 : Entities not created in database

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!

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:

在此处输入图片说明

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM