如何解释嵌套 VO 对象中的空值

发布于 2024-12-04 09:30:30 字数 468 浏览 1 评论 0原文

我继承了 vo/dao 设置(尽管只有一个模型类具有每个类的所有 DAO 函数),但递归变得很奇怪。

每个用户都有帖子,每个帖子可以有视频,但视频也有用户,有时帖子没有设置视频。

getPost 看起来(部分)像这样:

function getPostById($id) {
  $post->name = $row['name'];
  $post->video = $this->getVideoById($row['video_id']);
}

video 有一个类似的函数来获取其用户,问题是如果视频没有在帖子中设置(id=0),那么 getUser 函数在逻辑上会失败,并带有 mysql_fetch_array() 期望参数 1 是资源,给定布尔值。现在我可以在所有引用其他对象的命令上添加条件,但是有其他方法可以处理像这样链接 V0 对象时可能出现的空值吗?

I have inherited a vo/dao setup (there's just one model class that has all the DAO functions for every class, though), but it's getting odd with recursion.

Every user has posts, each post can have a video, but videos also have users, and sometimes a video hasn't been set for a post.

getPost looks (partially) like this:

function getPostById($id) {
  $post->name = $row['name'];
  $post->video = $this->getVideoById($row['video_id']);
}

video has a similar function to get its user, the problem is if a video is not set within the post (id=0), then the getUser function for it fails, logically, with mysql_fetch_array() expects parameter 1 to be resource, boolean given. Now I could put conditionals on all those commands that refer to other objects, but is there some other way to deal with potentially null values in linking V0 objects like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

起风了 2024-12-11 09:30:30

您应该从 getUser 的结果中获取计数,我会评估该属性并在值小于 1 时抛出异常。

然后处理异常,因为代码非常依赖于方法结果依赖性。

然而,这将需要一些重构,我可以看到你正在试图避免。

You should get a count from the results of getUser, I would evualate that property and throw an exception if value is less than 1.

Then handle the exception since the code leans hard on method result dependency.

However, this will take a bit of refactoring that I can see you're trying to avoid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文