[英]Symfony 2 : Doctrine OneToMany relation
I've 2 entities : 我有2个实体:
class Client
{
/**
* @var integer $mainId
*
* @ORM\Column(name="main_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $mainId;
/**
* @var string $id
*
* @ORM\Column(name="id", type="string", length=10, nullable=false, unique=true)
*/
private $id;
...
}
class Child
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var Client
*
* @ORM\ManyToOne(targetEntity="Client")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
* })
*/
private $clientId;
....
}
For some reason the foreign key from the child table doesn't point to the primary key of the Client table, but anyway I don't think that it should be a problem. 出于某种原因,子表中的外键没有指向Client表的主键,但是无论如何我都不认为这应该是一个问题。
When I try to do the following action in my controller : 当我尝试在控制器中执行以下操作时:
$child->set... $ child-> set ...
$child->setClientId($client); $ child-> setClientId($ client);
$em->persist($child); $ em-> persist($ child);
I get the following error : 我收到以下错误:
Notice: Undefined index: id in C:\wamp\www\myApp\vendor\doctrine\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 510
I tried to add @Index anntotation in my Client Table but I still get the error... 我试图在我的客户表中添加@Index注释,但仍然出现错误...
I've never had that problem before, any help would be welcome. 我以前从未遇到过这个问题,欢迎任何帮助。
EDIT 编辑
If I move the "@ORM\\Id" annotation from $mainId to $id in my Client class, that works. 如果将Client类中的“ @ORM \\ Id”注释从$ mainId移至$ id,则可以。 Would that mean that Doctrine 2 doesn't allow foreign keys that points on 'non primary key' fields ?
这是否意味着Doctrine 2不允许指向“非主键”字段的外键?
referencedColumnName
should be set to the column that has @ORM\\Id
set, so in your case referencedColumnName="main_id"
should work referencedColumnName
应该设置为设置了@ORM\\Id
的列,因此在您的情况下, referencedColumnName="main_id"
应该可以工作
(don't forget to clear cache and run doctrine:schema:update --force
after you change these settings) (不要忘记更改这些设置后清除缓存并运行
doctrine:schema:update --force
)
why don't you simply design everything on your Database, including foreign keys, and so on...and simply create your Entities based on the database using this here? 您为什么不简单地在数据库中设计所有内容,包括外键,等等……并在此处使用基于数据库的实体来创建实体?
http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
worked perfectly for me. 对我来说效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.