OnteToOne识别关系 [英] OnteToOne Identifying relationship

查看:154
本文介绍了OnteToOne识别关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了以下两个实体:

I set up the two following entities:

/**
 * Post
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="PostRepository")
 */
class Post
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer", options={"unsigned":true})
     */
    protected $id;

    // ...
###################

/**
 * Push
 *
 * @ORM\Table(name="push")
 * @ORM\Entity()
 */
class Push
{
    /**
     * @ORM\id @ORM\OneToOne(targetEntity="Post")
     * @ORM\JoinColumn(name="post_id", nullable=false)
     */
    protected $post;

    // ...
}

我知道我应该持续冲洗一个Post Entity,以便坚持推一个,这不是:p

I know I should persist and flush a Post Entity to be able to persist a push one, that's not the point :p.

schema:create 中,我希望MySQL在推送表中的 post_id 上创建一个外键约束,但所有我可以通过PMA看到一个主键。

On schema:create, I expected MySQL to create a foreign key constraint on post_id in the push table, but all I can see through PMA is a primary key.

为什么没有创建外键?我有没有想过什么,我的意思是有没有任何注释标志/选项来设置,以解决这个问题?

Why ain't there any foreign key created? Did I miss something, I mean is there any annotation flag/option to setup in order to solve this issue?

推荐答案

其实,事实证明,外键是创建的,但由于它被定义为主键,因此也不需要其他索引!
Wee可以看到外键在上面的关系视图按钮:

Actually, it turns out that the foreign key is created but since it is defined as the primary key as well no other index is needed ! Wee can see the foreign key looking at the "relation view" button above:

class Push
{
    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="Post")
     * @ORM\JoinColumn(name="post_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
     */
    protected $post;

希望这将有助于澄清事情...

Hope it will help clarify things ...

这篇关于OnteToOne识别关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆