php – 为什么我不能在Doctrine实体中使用公共属性?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 为什么我不能在Doctrine实体中使用公共属性?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2848字,纯文字阅读大概需要5分钟。
内容图文
![php – 为什么我不能在Doctrine实体中使用公共属性?](/upload/InfoBanner/zyjiaocheng/811/d156710bb0a3426e888c1b57a4989933.jpg)
假设我在PHP中有一个非常简单的CRUD系统来管理数据库.假设它包含产品列表.使用Doctrine ORM,我想查询数据库并查看/编辑/添加记录.根据Getting Started手册,
When creating entity classes, all of the fields should be protected or
private (not public), with getter and setter methods for each one
(except $id). The use of mutators allows Doctrine to hook into calls
which manipulate the entities in ways that it could not if you just
directly set the values with entity#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();
但是,这似乎过于复杂.假设我没有必要挂钩操纵实体的调用,如手册中所述.我可以使这段代码更短,并执行此操作:
// src/Product.php
class Product
{
/**
* @var int
*/
public $id;
/**
* @var string
*/
public $name;
}
这将允许这样的事情:
$product = $db->getRepository('Product')->find(1);
// Recording a new name
$product->name = "My new title";
$db->persist($product);
$db->flush();
// Echoing the title
echo $product->name;
优点是:
>始终使用完全相同的名称
>实体类中没有额外的setter和getter
使用它的缺点是什么?我在这里承担一定的风险吗?
解决方法:
Say I don’t have a need to hook into calls to manipulate entities, as explained in the manual.
你不需要挂钩这些调用,但是Doctrine确实如此. Doctrine内部会覆盖这些getter和setter,以尽可能透明地实现ORM.
是的,它过于复杂,但这是错误的PHP语言,而不是Doctrine.使用公共属性没有任何本质上的错误,只是PHP语言不允许像其他语言那样创建自定义getter / setter(例如Kotlin,C#).
Doctrine究竟做了什么?它生成所谓的Proxy对象以实现延迟加载.这些对象扩展您的类并覆盖某些方法.
对于你的代码:
// 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;
}
}
学说可能会产生:
class Product extends \YourNamespace\Product implements \Doctrine\ORM\Proxy\Proxy
{
// ... Lots of generated methods ...
public function getId(): int
{
if ($this->__isInitialized__ === false) {
return (int) parent::getId();
}
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', []);
return parent::getId();
}
public function getName(): string
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getName', []);
return parent::getName();
}
// ... More generated methods ...
}
(这究竟不是重点,重点是Doctrine必须能够覆盖getter和setter来实现ORM)
要自己查看,请检查Doctrine代理目录中生成的Proxy对象.有关配置代理目录的详细信息,请参阅Advanced Configuration部分.有关Proxy对象以及Doctrine如何使用它们的更多信息,请参阅Working with Objects.
内容总结
以上是互联网集市为您收集整理的php – 为什么我不能在Doctrine实体中使用公共属性?全部内容,希望文章能够帮你解决php – 为什么我不能在Doctrine实体中使用公共属性?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。