我记得在Doctrine 2模型中读到它,我不应该公开属性/字段.你怎么会暴露这些领域?使用的沙箱get*()和set*()方法.这是最好的主意吗?它非常麻烦.使用魔术方法__get() __set()会使设置字段公开类似吗?
你的推荐是什么?
我是PHP的新手.出于某些原因,在其他类型的编程语言(如JAVA)中,我对使用每个变量的setter和getter没有任何问题,但是当我在PHP编程时可能因为它非常灵活,感觉有点浪费时间.在大多数时候将类属性设置为公共并且像这样操纵它们感觉更简单.问题在于,当我这样做的时候,我觉得我做错了什么并且反对OO原则.
不使用setter和getter真是错吗?为什么或者为什么不?大多数时候你们是如何做到的?
我使用zf2.1.3和doctrine 2.我试图在我的课上保持信息,并意识到它DoctrineModule\Stdlib\Hydrator\DoctrineObject不适用于有下划线的字段,比如cat_id.
这是一个例子:
/* namespace Application\Entity; */
class Foo
{
private $cat_id;
private $cat_name;
public function getCatId()
{
return $this->cat_id;
}
public function setCatName($name)
{
$this->cat_name = $name;
return $this;
}
public function getCatName()
{
return $this->cat_nome;
}
}
class Bar
{
private $id;
private $name;
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->nome;
}
}
/* namespace Application\Controller; */
use …Run Code Online (Sandbox Code Playgroud) 假设我在PHP中有一个非常简单的CRUD系统来管理数据库。说它拥有一个产品清单。使用Doctrine ORM,我想查询数据库并查看/编辑/添加记录。根据入门手册,
创建实体类时,所有字段都应受保护或为私有字段(非公共字段),每个字段都应具有getter和setter方法($ id除外)。mutator的使用使Doctrine可以挂接到以实体#field = foo直接设置值的方式无法操纵实体的调用。
这是提供的示例:
// src/Product.php
class Product
{
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
// Recording a new title
$product->setName("My new name");
$db->persist($product);
$db->flush();
// Echoing the title
echo $product->getName();
Run Code Online (Sandbox Code Playgroud)
但是,这似乎过于复杂。假设我没有必要像实体手册中所述去处理实体的调用。我可以使这段代码更短并执行以下操作:
// src/Product.php
class Product …Run Code Online (Sandbox Code Playgroud) 我有一个类如下:
/** @Entity **/
class orgGroup{
//id and stuff...
/**
* @Column(type="string")
**/
private $name;
/**
* @Column(type="string", nullable=true)
**/
private $description;
/**
* @ManyToOne(targetEntity="orgGroupType", inversedBy="_orgGroups")
* @JoinColumn(name="_orgGroupType")
**/
private $_orgGroupType;
//...
}
Run Code Online (Sandbox Code Playgroud)
但是当我通过我的数据库加载此对象时
$groups = $em->getRepository("orgGroup")->findAll();
Run Code Online (Sandbox Code Playgroud)
我只是正确地得到了名称而不是_orgGroupType ...我不知道为什么... OrgGroup是orgGroupType的所有者,它只是一个对象而不是数组.我的Web服务总是说:
{"error":[],"warning":[],"message":[],"data":[{"name":"AdministratorGroup","description":null,"_orgGroupType":{"__ isInitialized __":false}}]}
Run Code Online (Sandbox Code Playgroud)
结果是:
"name":"AdministratorGroup",
"description":null,
"_orgGroupType":{"__ isInitialized __":false}
Run Code Online (Sandbox Code Playgroud)
但应该是:
"name":"AdministratorGroup",
"description":"some description",
"_orgGroupType":{name:"test"}
Run Code Online (Sandbox Code Playgroud)
所以有2个错误......我不明白为什么.所有数据都在数据库中正确设置.
有任何想法吗?
编辑:这是我的orgGroupType -entity缺少的代码
/** @Entity **/
class orgGroupType {
/**
* @OneToMany(targetEntity="orgGroup", mappedBy="_orgGroupType")
**/
private $_orgGroups;
public function __construct()
{
$this->_orgGroups = new …Run Code Online (Sandbox Code Playgroud)