问题描述
这是一个沉重的的MySQL
和 PHP
背景我真的不能让我的头在这个即将到来。
我分支的集合
有多对一关系餐厅
(所以有很多分支机构连接到同一家餐厅)。
这种关系由场 restaurantId
,存储该对象的id确定。我的分支
收集如下所示:
{
_id:uH4KbNYxw8cnCEqvk,
地址:1高街
LOC:{
坐标:[
0.0,
0.0
]
类型:点
},
restaurantId:dvxZ2NiA3gbevffsk
}
当然的餐饮
集
{
_id:dvxZ2NiA3gbevffsk,
名:餐厅的名字
}
现在,我通过查询 $附近
以接近下令所有的树枝,我找不到一个干净的方式来获得父母的餐馆,而名循环满枝头。
下面是我做的 - 我的意思是,它的工作原理,因为它是,但我认为性能方面它有很多有待改进
//控制器
angular.module(MyApp的)。控制器(SearchRestaurantsCtrl,
功能($范围,$ stateParams,$流星){ $ meteor.subscribe('分支');
$ meteor.subscribe('餐厅'); $ scope.branches = $ meteor.collection(函数(){
返回Branches.find({
LOC:{
近$ {
$几何:{
类型:点,
坐标:[$ stateParams.lng,$ stateParams.lat]
},
$ maxDistance:300000
}
}
});
}); $ scope.getRestaurant =功能(restaurantId){
返回Restaurants.findOne({
_id:restaurantId
});
};
}
);//视图
< DIV NG重复=分行分行>
< H3> {{getRestaurant(branch.restaurantId)。名称}}< / H3 GT&;
<地址> {{branch.address}}< /地址>
< / DIV>
我最终使用经过采用了棱角分明的收集和过滤两种:
控制器
angular.module(sushisushi24)。控制器(SearchRestaurantsCtrl
功能($范围,$ stateParams,$流星){ $ scope.branches = $ meteor.collection(分行).subscribe('branchesAndRestaurants');
$ scope.restaurants = $ meteor.collection(餐厅);
}
);
流星发布
Meteor.publish('branchesAndRestaurants',功能(选){ 分支= Branches.find();
restaurantIds = branches.map(功能(分公司){返回branch.restaurantId}); 返回[
分支机构,
Restaurants.find({_ ID:在{$:restaurantIds}})
];
});
查看
< DIV NG重复=分行分行>
< DIV NG重复=在饭店餐厅|过滤器:{_id:branch.restaurantId}>
< H3> {{restaurant.name}}< / H3 GT&;
< / DIV>
<地址> {{branch.address}}< /地址>
< / DIV>
coming from a heavy MySQL
and PHP
background I can't really get my head over this.
I have a collection of branches
with a ManyToOne relation to Restaurants
(so there are many branches attached to the same restaurant).
Such relation is defined by the field restaurantId
, which stores the id of the object. My branches
collection appears as follows:
{
"_id" : "uH4KbNYxw8cnCEqvk",
"address" : "1 High Street",
"loc" : {
"coordinates" : [
0.0,
0.0
],
"type" : "Point"
},
"restaurantId" : "dvxZ2NiA3gbevffsk"
}
And of course the restaurants
collection
{
"_id" : "dvxZ2NiA3gbevffsk",
"name" : "Restaurant name"
}
Now, I'm querying all the branches ordered by proximity using $near
, and I can't find a clean way to get the name of the parent restaurants while looping over the branches.
Here is what I did - I mean, it works as it is, but I think performance-wise it has a lot to be improved
// controller
angular.module("MyApp").controller("SearchRestaurantsCtrl",
function($scope, $stateParams, $meteor){
$meteor.subscribe('branches');
$meteor.subscribe('restaurants');
$scope.branches = $meteor.collection(function() {
return Branches.find({
loc: {
$near: {
$geometry: {
type: "Point",
coordinates: [ $stateParams.lng, $stateParams.lat]
},
$maxDistance: 300000
}
}
});
});
$scope.getRestaurant = function(restaurantId) {
return Restaurants.findOne({
_id: restaurantId
});
};
}
);
// view
<div ng-repeat="branch in branches">
<h3>{{getRestaurant(branch.restaurantId).name}}</h3>
<address>{{branch.address}}</address>
</div>
I ended up using passing both the collection and filtering using angular:
Controller
angular.module("sushisushi24").controller("SearchRestaurantsCtrl",
function($scope, $stateParams, $meteor){
$scope.branches = $meteor.collection(Branches).subscribe('branchesAndRestaurants');
$scope.restaurants = $meteor.collection(Restaurants);
}
);
Meteor Publish
Meteor.publish('branchesAndRestaurants', function(opts) {
branches = Branches.find();
restaurantIds = branches.map(function(branch) { return branch.restaurantId });
return [
branches,
Restaurants.find({_id: {$in: restaurantIds}})
];
});
View
<div ng-repeat="branch in branches">
<div ng-repeat="restaurant in restaurants | filter: {_id:branch.restaurantId}">
<h3>{{restaurant.name}}</h3>
</div>
<address>{{branch.address}}</address>
</div>
这篇关于在处理人际关系流星角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!