从域实体方法内检索依赖/父级
如何从实体中检索依赖/父对象。
$person->getAddress();
这应该从数据库中检索该人的地址对象并将其作为对象返回。这是一个好方法吗?如果可以的话,代码应该是什么样子。
这样做意味着对象本身应该知道地址是与其相关的实体/值对象。 为什么我想要这种语法是因为它在表示层中看起来很干净。
person 类看起来像这样:
class Person {
protected $_domain = null; // domain is assigned when instantiated
protected $_data = array('name', 'address');
protected $_relations = array(
'address'=>array(
'class'=>'Address'
)
);
protected $_retrievedRelations = array();
public function getAddress() {
if (array_key_exists('address', $this->_relations) ) {
if (!array_key_exists('address', $this->_retrievedRelations) ) {
$this->_retrievedRelations['address'] = $this->_domain->getAddress($this->_data['address']);
}
return $this->_retrievedRelations['address'];
}
return $this->_data['address'];
}
}
那么可以在 getAddress 方法中使用 $domain 对象并将关系信息保留在 Person 类中吗?
请回答,因为我一直在寻找答案。
How would one go about retrieving a dependent/parent object from an entity.
$person->getAddress();
This should retrieve the Address object for that person from the database and return it as an object. Is this a good way to do it and how should the code look like if this is ok to do.
Doing this would mean that the object itself should be aware that address is an entity/value object that it is related.
Why i want this kind of syntax is because it will look clean in the presentation layer.
The person class would look like this:
class Person {
protected $_domain = null; // domain is assigned when instantiated
protected $_data = array('name', 'address');
protected $_relations = array(
'address'=>array(
'class'=>'Address'
)
);
protected $_retrievedRelations = array();
public function getAddress() {
if (array_key_exists('address', $this->_relations) ) {
if (!array_key_exists('address', $this->_retrievedRelations) ) {
$this->_retrievedRelations['address'] = $this->_domain->getAddress($this->_data['address']);
}
return $this->_retrievedRelations['address'];
}
return $this->_data['address'];
}
}
So is it ok to use the $domain object inside the getAddress method and to keep relation information in the Person class?
Please answer because i've been looking all over for an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Zend_Db_Table 关系。他们允许这样做,甚至更多。您现在就可以使用
Zend_Db_Table
,而不是尝试提出自己的实现,或者您可以学习 Zend Framework 的 代码,了解他们针对您的问题的设计决策。这是一个相当权威的来源。Take a look at Zend_Db_Table Relationships. They allow doing exactly that, and much more. You can just use
Zend_Db_Table
today, instead of trying to come up with your own implementation, or you can study Zend Framework's code for their design decisions wrt to your questions. It's quite an authoritative source.首先我觉得这个问题的标题不是很清楚,其次我也不是很清楚
完全确定我明白所问的内容......在我看来,标题和帖子确实应该进行编辑以使其更加干净。
这篇文章似乎包含 5 个问题(如果我可以解释的话):
$person->getAddress()
是检索 Address 对象的好方法吗?数据库中的人?
就答案而言:
$person->getAddress()
似乎是一个不错的选择,你知道,很好且具有描述性。 ;-)一些与答案 Nr.3 一起使用的代码:(我无法让它在列表中正常工作)
First I think the title of the question isn't very clear, second I'm not
entirely sure I understand what is being asked... In my opinion both the title and the post really should be edited to clean it up a bit.
The post seems to contain 5 questions (if I might paraphrase):
$person->getAddress()
a good way to retrieve the Address object for aperson from the database?
As far as answers go:
$person->getAddress()
seems a good choice, you know, nice and descriptive. ;-)Some code to go along with answer Nr.3: (I couldn't get it to work properly within the list)