简体   繁体   English

Symfony2:实体未在数据库中创建

[英]Symfony2 : Entities not created in database

I'm making entities with Symfony2 and Doctrine2. 我正在用Symfony2和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. 我尝试更新我的sql模式,但是没有出现与这些实体相对应的表。 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. 不,您无需在Entity目录中的其他任何地方声明它们。

  • What's the error message you got? 您收到什么错误消息?

  • I guess you added 我想你加了

    use Doctrine\\ORM\\Mapping as ORM; 使用Doctrine \\ ORM \\ Mapping作为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. 我试图通过添加一个简单的Contact&Conference实体来生成您的实体,并且它工作正常。 Here are the code snippets: 以下是代码片段:

Contact_Conference_Invitation.php 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 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 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. 在您提供的代码示例中,我看不到ManyToMany关系的定义。

As an example, here's a ManyToMany relationship I implemented for a project 例如,这是我为项目实现的ManyToMany关系

Entity Project.php 实体Project.php

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

Entity Provider.php 实体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. 如您所见,在这里您可以为ManyToMany关系定义联接表。

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 在您的config.yml中,您应该

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. 之所以遇到这个问题,是因为我的实体也没有生成,这是我的问题,它可以为陷入同一问题的人们节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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