学说 - 未在数据库中创建关系
我正在使用Symfony和Doctrine。 当架构通过控制台命令php bin/console doctrine:schema:update --force
创建时,它不会创建我想要的关系。学说 - 未在数据库中创建关系
两类:
考生 ORM实体类:
namespace AppBundle\Libraries\Data;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="candidates")
*/
class Candidates
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @ORM\OneToMany(targetEntity="AppBundle\Libraries\Data\Votes", mappedBy="nCandidate")
*/
private $nID;
/**
* Get nID
*
* @return integer
*/
public function getNID()
{
return $this->nID;
}
}
投票 ORM实体类:
namespace AppBundle\Libraries\Data;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="votes")
*/
class Votes
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $nID;
/**
* @ORM\Column(type="integer")
* @ORM\ManyToOne(targetEntity="AppBundle\Libraries\Data\Candidates", inversedBy="nID")
*/
private $nCandidate;
/**
* Get nID
*
* @return integer
*/
public function getNID()
{
return $this->nID;
}
/**
* Set nCandidate
*
* @param integer $nCandidate
*
* @return Votes
*/
public function setNCandidate($nCandidate)
{
$this->nCandidate = $nCandidate;
return $this;
}
/**
* Get nCandidate
*
* @return integer
*/
public function getNCandidate()
{
return $this->nCandidate;
}
}
学说配置部分中config.yml外观像这样:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: false
mappings:
FOSUserBundle: ~
AppBundle:
type: annotation
dir: "%kernel.root_dir%/../src/AppBundle/Libraries/Data"
prefix: AppBundle\Libraries\Data
应该创建的关系在PHPMyadmin或MySQL Workbench中不可见。
每个表的ID列有属性名$nID
的,但它必须是$id
。
我还删除了@ORM\Column(type="integer")
评论上面的每个属性与@ORM\ManyToOne(targetEntity="Class", inversedBy="ID")
关系ID
主键。
即使你已经在正确的Symfony期待,你已经配置学说ORM通过设置自定义映射非标准目录放置这些实体..
问题似乎是因为缺乏在票数主键实体。
每个实体都必须有一个标识符/主键。与* @ORM\Id
AppBundle\Libraries\Data\Votes
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
编辑
投票实体: 这是我得到的SQL(MySQL的)当我运行
php app/console doctrine:schema:update --dump-sql
CREATE TABLE candidates (
n_id INT AUTO_INCREMENT NOT NULL
,PRIMARY KEY (n_id)
) DEFAULT CHAR
SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE votes (
id INT AUTO_INCREMENT NOT NULL
,n_candidate INT NOT NULL
,PRIMARY KEY (id)
) DEFAULT CHAR
SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
尝试以下操作:
候选实体
namespace AppBundle\Libraries\Data;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="candidates")
*/
class Candidates
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $nID;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Libraries\Data\Votes", mappedBy="candidate")
*/
private $vote;
/**
* Get nID
*
* @return integer
*/
public function getNID()
{
return $this->nID;
}
}
投票实体
namespace AppBundle\Libraries\Data;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="votes")
*/
class Votes
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $nID;
/**
* @ORM\Column(type="integer")
*/
private $nCandidate;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Libraries\Data\Candidates", inversedBy="vote")
*/
private $candidate;
/**
* Get nID
*
* @return integer
*/
public function getNID()
{
return $this->nID;
}
/**
* Set nCandidate
*
* @param integer $nCandidate
*
* @return Votes
*/
public function setNCandidate($nCandidate)
{
$this->nCandidate = $nCandidate;
return $this;
}
/**
* Get nCandidate
*
* @return integer
*/
public function getNCandidate()
{
return $this->nCandidate;
}
}
然后运行php bin/console doctrine:schema:update --dump-sql
,看是否学说是知道的关系
这会引发一个错误:'[Doctrine \ ORM \ ORMException]从AppBundle \ Libraries \数据\投票AppBundle \ Libraries \ Data \ Candidates不存在.'当我运行控制台命令。当我在关系之上添加'@ORM \ Column(type =“integer”)'时,它会识别更改,但不会创建关系。 – Orlando
是启用了Doctrine元数据缓存吗? – Vamsi
@Vamsi我没有特别启用任何东西,这是在配置? – Orlando
你有没有得到任何错误或只是没有创建?输出命令是什么php bin/console doctrine:schema:update --dump-sql? – Thomas