【发布时间】:2016-10-16 12:36:48
【问题描述】:
我尝试过类似“Use-Case 3: Join-Table with Metadata”的方法。 我用过“Cart”、“Item”和“CartItem”对象。
我得到了像$cart->getItems(); 这样的项目。
假设有 4 个项目。现在我删除一个像$items->remove(1); 这样的项目来删除一个项目。
$items->count() 告诉我一个项目已被删除(它说 3)。另外,在查看代码时,我看到集合已更改。
因此,当我刷新时,我希望 CartItem 将被删除,但没有任何反应。即使我像$cart->setItems($items); 这样再次添加项目,刷新时也没有任何反应。
删除项目应该是直截了当的,不是吗?我做错了什么?
编辑编辑编辑
即使使用“普通”键也不起作用。我创建了一些新对象:
汤
/**
* @Entity
* @Table(name="Soups")
**/
class Soup {
/**
* @Id @Column(type="integer")
* @GeneratedValue
**/
protected $id;
/**
* @Column(type="string", length=50)
*/
protected $name;
/**
* @OneToMany(targetEntity="SoupIngredient", mappedBy="soup", cascade={"persist"});
*/
protected $soupIngredients;
public function __construct() {
$this->soupIngredients = new ArrayCollection();
}
public function getId() {
// Return id.
return $this->id;
}
public function getName() {
// Return value.
return $this->name;
}
public function setName($value) {
// Set value.
$this->name = $value;
// Return.
return $this;
}
public function getSoupIngredients() {
// Return value.
return $this->soupIngredients;
}
public function setSoupIngredients($value) {
// Set value.
$this->soupIngredients = $value;
// Return.
return $this;
}
}
成分
/**
* @Entity
* @Table(name="Ingredients")
**/
class Ingredient {
/**
* @Id @Column(type="integer")
* @GeneratedValue
**/
protected $id;
/**
* @Column(type="text", length=50)
*/
protected $name;
/**
* @OneToMany(targetEntity="SoupIngredient", mappedBy="ingredient", cascade={"persist"});
*/
protected $soupIngredients;
public function __construct() {
$this->soupIngredients = new ArrayCollection();
}
public function getId() {
// Return id.
return $this->id;
}
public function getName() {
// Return value.
return $this->name;
}
public function setName($value) {
// Set value.
$this->name = $value;
// Return.
return $this;
}
public function getSoupIngredients() {
// Return value.
return $this->soupIngredients;
}
public function setSoupIngredients($value) {
// Set value.
$this->soupIngredients = $value;
// Return.
return $this;
}
}
汤料
/**
* @Entity
* @Table(name="SoupIngredients")
**/
class SoupIngredient {
/**
* @Id @Column(type="integer")
* @GeneratedValue
**/
protected $id;
/**
* @Column(type="float")
*/
protected $quantity;
/**
* @ManyToOne(targetEntity="Soup", inversedBy="soupIngredients")
*/
protected $soup;
/**
* @ManyToOne(targetEntity="Ingredient", inversedBy="soupIngredients")
*/
protected $ingredient;
public function __construct() {
}
public function getId() {
// Return id.
return $this->id;
}
public function getQuantity() {
// Return value.
return $this->quantity;
}
public function setQuantity($value) {
// Set value.
$this->quantity = $value;
// Return.
return $this;
}
public function getSoup() {
// Return value.
return $this->soup;
}
public function setSoup($value) {
// Set value.
$this->soups = $value;
// Return.
return $this;
}
public function getIngredient() {
// Return value.
return $this->ingredient;
}
public function setIngredient($value) {
// Set value.
$this->ingredient = $value;
// Return.
return $this;
}
}
我得到一个包含 5 种成分的 Soup 对象。
// Get Soup #1.
$em->getRepository("Soup")->find(1);
// Get ingredients.
$ingredients = $soup->getIngredients();
// Remove ingredient #2.
$ingredients->remove(2);
// Well, let's flush.
$em->flush();
什么都没有发生。我可以用$em 删除SoupIngredient,但我想在函数($soup->removeIngredient($ingredient))中使用它,所以我并不总是有$em 可用。实现这一点的唯一方法是使用$em->remove($soupIngredient)(然后它将在刷新时更新)。
【问题讨论】:
标签: collections orm doctrine foreign-keys composite-key