从域实体方法内检索依赖/父级

发布于 2024-08-04 11:31:02 字数 1009 浏览 3 评论 0原文

如何从实体中检索依赖/父对象。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-08-11 11:31:02

查看 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.

飘逸的'云 2024-08-11 11:31:02

首先我觉得这个问题的标题不是很清楚,其次我也不是很清楚
完全确定我明白所问的内容......在我看来,标题和帖子确实应该进行编辑以使其更加干净。

这篇文章似乎包含 5 个问题(如果我可以解释的话):

  1. 如何从实体中检索依赖/父对象?
  2. $person->getAddress() 是检索 Address 对象的好方法吗?
    数据库中的人?
  3. 如果这是一个好方法,那么代码会是什么样子?
  4. 可以在 getAddress 方法中使用 $domain 对象吗?
  5. 可以在 Person 类中保存关系信息吗?

就答案而言:

  1. 在这种情况下 $person->getAddress() 似乎是一个不错的选择,你知道,很好且具有描述性。 ;-)
  2. 我没有看到问题:Person HAS A 地址,要获取您向 person 对象询问的地址,请使用 is。
  3. 给定的代码示例似乎回答了这个问题,尽管它有点混乱。我只是将地址设置为成员对象,但除此之外它基本上是相同的
  4. & 5. 我想说主要是品味、优先级和设计依赖性的问题。对此确实没有一个明确的答案。另外,我们在这里争论的是语义还是现实生活场景?

一些与答案 Nr.3 一起使用的代码:(我无法让它在列表中正常工作)

class Person {
    protected $_domain = null; // domain is assigned when instantiated
    protected $_address;        // object

    public function getAddress() {
        if (!isset($this->_address) ) {
            $this->_address = $this->_domain->getAddressForPerson($this); // assuming $this contains some sort of ID
        }
        return $this->_address;
    }
}

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):

  1. How would one go about retrieving a dependent/parent object from an entity?
  2. Is $person->getAddress() a good way to retrieve the Address object for a
    person from the database?
  3. If this a good way to do it then what would the code look like?
  4. Is it okay to use the $domain object inside the getAddress method?
  5. Is it okay to keep relation information in the Person class?

As far as answers go:

  1. In this case $person->getAddress() seems a good choice, you know, nice and descriptive. ;-)
  2. I don't see a problem: Person HAS A Address, to get that adress you ask the person object for is.
  3. The given code example seems to answer this, although it is a bit cluttered. I would've just made address a member object, but other than that it's mostly the same
  4. & 5. I'd say are mostly a matter of taste, priority and design-dependency. There really isn't a clean cut answer to this. Also, are we arguing semantics here or a real life scenario?

Some code to go along with answer Nr.3: (I couldn't get it to work properly within the list)

class Person {
    protected $_domain = null; // domain is assigned when instantiated
    protected $_address;        // object

    public function getAddress() {
        if (!isset($this->_address) ) {
            $this->_address = $this->_domain->getAddressForPerson($this); // assuming $this contains some sort of ID
        }
        return $this->_address;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文