Closed delaneyj closed 10 years ago
While querying DB Objectory do not follow all links automatically. Otherwise retrieving one object from db you potentially may get all db (by transitive links). Instead you get object with pointers to linked object.
To populate these pointers with real objects you may use fetchLinks
method of original persistent object.
See console blog example:
Future printArticle(article) {
var completer = new Completer();
article.fetchLinks().then((__) {
print("${article.author.name}:${article.title}:${article.body}");
for (var comment in article.comments) {
print(" ${comment.date}:${comment.user.name}: ${comment.body}");
}
completer.complete(true);
});
return completer.future;
}
Or you may set queryBuilder to fetch links for you on query. In your example that would be:
objectory[PurchaseModel].findOne(where.fetchLinks()).then((PurchaseModel pm){
Is there a more proper way for me to do this? In my example I'm just trying to grab the user's name. I'd think something like pm.buyer.fetch().then((UserModel u){});
would be the efficient way to do this.
When I tried that it looks like persistent_object.dart Line188 is returning true
instead of the actual PersistentObject
. Am I understanding this incorrectly?
Both
objectory[PurchaseModel].findOne(where.fetchLinks()).then((PurchaseModel pm){
print("If I don't drop the tables and initCollections again it appears that the same query fails.");
print(pm.buyer.name);
print("Why is this null and what am I doing wrong?");
objectory.close();
});
And
objectory[PurchaseModel].findOne().then((PurchaseModel pm){
return pm.fetchLinks();
}).then((pm) {
print("If I don't drop the tables and initCollections again it appears thathe same query fails.");
print(pm.buyer.name);
print("Why is this null and what am I doing wrong?");
objectory.close();
});
work in your example. That are the proper ways.
Now that you've explained how it works fetchLinks()
brings up concerns in my situation. So I need to avoid setting up models where deep linking is occurring? Is there any way to selectively fetchLinks?
Sorry for the confusion, now I think its starting to make sense. In my case another query of objectory[UserModel].findOne(where.id(pm.buyer.id)).then((UserModel u){/*doStuff*/});
will work as I want. Thanks for your time and patience. This conversation could probably be used as the basis for filling in the quick tour on the subject of fetchLinks()
Thank you for kindly words. I definitely should refresh and extend quick tour. Hope I'll manage to do it soon.
Took a project I'm working on and convert the core to a single file console program to show the issue. Basically if I follow the examples and drop the tables and fill with fresh data I am able to query properly. However if I run the same query on a db after that with a new connection I'm getting null values.