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

时间:2019-03-15 00:09:12

标签: php sql doctrine-orm

我对doctrine2有疑问,两个模型之间的关系很简单 下面我准备了一个简单的例子

k2 <- kmeans(Scaled_data, centers = 2, nstart = 25)
k2$Time <- as.factor(time)
print(names(k2))
print(length(k2$Time))
print(length(k2$cluster))

plot_Obj <- fviz_cluster(k2, data = Scaled_data,
         stand = FALSE,
         ellipse.type = "norm",
         geom = "point",
         alpha=0.5,
         ggtheme = theme_minimal(),
         repel = FALSE,
         shape=Time)
print(plot_Obj)

Output: 
      [1] "cluster"      "centers"      "totss"        "withinss"     
      "tot.withinss"  [6] "betweenss"    "size" "iter"  "ifault"    "Time"  

  [1] 783
  [1] 783

 Error: 
    Error in fviz_cluster(k2, data = Scaled_data, stand = FALSE, 
    ellipse.type = "norm",  : object 'Time' not found
     Execution halted

第二等

/**
 * @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;
}
}

对于以下代码执行,我无法将“播放器”对象关系添加到电子钱包:

/**
 * @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;
}
}

也许在所附图片中会更好地看到: enter image description here

1 个答案:

答案 0 :(得分:1)

据我所知,您必须在owning site(在本例中为Wallet)上进行设置,试试看:

$player = new Player();

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

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

$entityManager->flush();