我很难弄清楚如何正确使用Doctrine 2和zend框架。我正在阅读文档并基于我迄今为止所做的和zendcasts。当我尝试用我的数据库做关系时,问题实际上就开始了,因为我不太确定如何使用doctrine集合。在我的测试用例中,我有一个用户实体:
class User
{
/**
* @var integer
* @Column (name="id", type="integer", nullable=false)
* @Id
* @GenerateValue(strategy="IDENTIY")
*
*/
private $id;
/**
* @Column(type="string",length=60,nullable=true)
* @var string
*/
private $email;
/**
*
* @param \Doctring\Common\Collections\Collection $property
* @OneToMany(targetEntity="Countries",mappedBy="user", cascade={"persist", "remove"})
*/
private $countries;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
与国家实体有关:
class Countries {
/**
* @var integer
* @Column (name="id", type="integer", nullable=false)
* @Id
* @GenerateValue(strategy="IDENTIY")
*
*/
private $id;
/**
*
* @var string
* @Column(type="string")
*/
private $countryName;
/**
*
* @var User
* @ManyToOne(targetEntity="User")
* @JoinColumns({
* @JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
现在我可以从控制器分配这样的国家:
$p1 = new \Federico\Entity\Countries();
$p1->countryName = 'Argentina';
$p2 = new \Federico\Entity\Countries();
$p2->countryName = 'España';
$u = new \Federico\Entity\User();
$u->firstname = 'John';
$u->lastname = 'Doe';
$u->id = 1;
会告诉我这个对象:
object(Federico\Entity\User)[109]
private 'id' => int 1
private 'email' => null
private 'countries' =>
array
0 =>
object(Federico\Entity\Countries)[107]
private 'id' => null
private 'countryName' => string 'Argentina' (length=9)
private 'user' => null
1 =>
object(Federico\Entity\Countries)[108]
private 'id' => null
private 'countryName' => string 'España' (length=7)
private 'user' => null
public 'firstname' => string 'John' (length=4)
public 'lastname' => string 'Doe' (length=3)
如果您注意这一点,您会看到国家/地区对象中的用户属性设置为null。我不明白这是否应该是这样发生的。此外,由于用户将被允许从复选框列表中选择国家/地区,并且他们将能够选择多个国家/地区,...不应该以某种方式将国家/地区存储在Db中吗?
答案 0 :(得分:0)
我看不到您在代码中为用户分配国家/地区的位置。无论如何,你需要做两件事:
在“用户”构造函数中将$countries
变量初始化为新的Doctrine\Common\Collections\ArrayCollection
。
手动将国家/地区与用户连接:
public function addCountry(Country $c)
{
if (!$this->countries->contains($c)) {
$this->countries->add($c);
$c->user = $user;
}
}
答案 1 :(得分:0)
在学说2中,不鼓励使用魔法吸气剂和制定者。如您所见,它们可能会导致管理关联的问题。以下是如何管理您的用户实体中的关联的示例。
namespace Whatever/Your/Namespace/Is;
use \Doctrine\Common\ArrayCollection;
class User
{
/**
* @Column (type="integer")
* @Id
* @var integer
*/
private $id;
/**
* @OneToMany(targetEntity="Country", mappedBy="user", cascade={"persist", "remove"})
* @var ArrayCollection
private $countries;
public function __construct()
{
$this->countries = new ArrayCollection();
}
public function getCountries()
{
return $this->countries;
}
public function setCountry(Country $country)
{
$this->country[] = $country;
$country->setUser($this);
return $this;
}
public function removeCountry(Country $country)
{
return $this->country->removeElement($country);
}
}
和国家
class Country
{
/**
* @Id
* @Column(type="integer")
* @var integer
*/
private $id;
/**
* @Column(length=100)
* @var string
*/
private $country_name;
/**
* @ManyToOne(targetEntity="User", inversedBy="countries")
* @var User
*/
private $user;
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
public function getUser()
{
return $this->user;
}
// ...
}