2014-06-13 34 views
0

我在教条中遇到外键问题。我有一个“用户” - 表和一个“订单” - 表。现在我想从订单表中的用户标识到用户表中的标识。该订单实体的样子:主义创造外键

<?php 

namespace Application\TestBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(
* name="Orders", 
* options={"collate"="utf8_general_ci", 
* "charset"="utf8", 
* "engine"="InnoDB" 
* } 
*) 
* @ORM\Entity(repositoryClass="Application\TestBundle\Entity\OrdersRepository") 
*/ 
class Orders 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(
    * name="id", 
    * unique=true, 
    * type="integer", 
    * options={"unsigned"=true} 
    *) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(
    * name="userID", 
    * type="integer", 
    * nullable=false, 
    * options={"unsigned"=true} 
    *) 
    * @ORM\OneToOne(targetEntity="User") 
    * @ORM\JoinColumn(name="userID", referencedColumnName="id") 
    */ 
    private $userID; 

    // ... 
} 

用户实体的样子:

<?php 

namespace Application\TestBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table(
* name="User", 
* options={"collate"="utf8_general_ci", 
* "charset"="utf8", 
* "engine"="InnoDB" 
* } 
*) 
* @ORM\Entity(repositoryClass="Application\TestBundle\Entity\UserRepository") 
*/ 
class User 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(
    * name="id", 
    * unique=true, 
    * type="integer", 
    * options={"unsigned"=true} 
    *) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    // ... 
} 

可惜任何外键将设置。它不在SQL查询中。我究竟做错了什么?

+0

你如何坚持你的实体?你可以粘贴控制器动作中的*相关*部分吗? –

+0

看到我更新的问题 – user3631654

回答

2

解决方案很简单。它只能是关系或数据类型(string,object,int)。所以我的例子需要一个关系,所以我必须删除@ORM\Column-annoation,它的工作!

0

这通常发生在数据库表引擎类型不支持外键(如MyISAM)的情况下。尝试将您的引擎类型更改为InnoDB,然后运行

console doctrine:schema:update 
+0

不幸的是它已经InnoDB – user3631654

+0

我怎么能看到出了什么问题? – user3631654