How to account for empty values in nested VO objects
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?
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.
精彩评论