简体   繁体   English

Doctrine2-没有在表中插入具有OneToMany关系的外键(

[英]Doctrine2 - No foreign key inserted in the table with relation OneToMany(

I have a problem with doctrine2 with a simple relationship between the two models Below I have prepared a simple example 我对doctrine2有问题,两个模型之间存在简单关系下面我准备了一个简单示例

/**
 * @Entity(repositoryClass="PlayerRepository") @Table(name="players")
 */
class Player {
/**
 * @Id @Column(type="integer") @GeneratedValue
 */
protected $id;

/**
 * @OneToMany(targetEntity="Wallet", mappedBy="player", cascade={"persist"})
 * @var Wallet[]
 */
private  $wallets;

public function __construct() {
    $this->wallets = new ArrayCollection();
}
public function getId() {
    return $this->id;
}

public function setId($id) {
    $this->id = $id;
}

public function getWallets() {
    return $this->wallets;
}
public function addWallets($wallets) {
    $this->wallets[] = $wallets;
}
}

And second class 第二等

/**
 * @Entity(repositoryClass="WalletRepository") @Table(name="wallets")
 */

class Wallet
{
/**
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;

/**
 * @ManyToOne(targetEntity="Player", inversedBy="wallets")
 */
private $player;
public function getId() {
    return $this->id;
}
public function setId($id) {
    $this->id = $id;
}
public function getPlayer() {
    return $this->player;
}
public function setPlayer($player) {
    $this->player = $player;
}
}

For the following code execution, I am not able to add Player object relations to Wallet: 对于以下代码执行,我无法将Player对象关系添加到Wallet:

player = new Player();
$player->addWallets(new Wallet);
$player->addWallets(new Wallet);
$entityManager->persist($player);
$entityManager->flush();

Maybe it will be better seen in the attached picture: 也许在所附图片中会更好看: 在此处输入图片说明

As far as I know you have to set this on the owning site , in this case Wallet , give it a try: 据我所知,您必须在所有者站点 (在本例中为Wallet ,试试看:

$player = new Player();

$wallet = new Wallet();
$wallet->setPlayer($player);

$entityManager->persist($player);
$entityManager->persist($wallet);

$entityManager->flush();

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

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