在 Silverstripe 模型中查询远程表
我试图从另一种页面类型的页面类型中获取字段“成本”的总和。 (ProjectCategory),就像这样:
class ProjectCategory extends Page {
static $belongs_many_many = array(
'RelateToProject' => 'Project'
);
function totalCost(){
$sqlQuery = new SQLQuery(
"SUM(Project.cost)", // Select
"Project", // From
"what to do here?" // Where (optional)
);
$totalVisits = $sqlQuery->execute()->value();
return $totalVisits;
}
我该为 where 位做什么?我怎样才能获得该类别的总成本? (如果我将 where
留空,它将返回每个类别的所有项目成本的总和 - 这不好)。
I'm trying to get a sum of a field "cost" from a page type from another page type. (ProjectCategory), like this:
class ProjectCategory extends Page {
static $belongs_many_many = array(
'RelateToProject' => 'Project'
);
function totalCost(){
$sqlQuery = new SQLQuery(
"SUM(Project.cost)", // Select
"Project", // From
"what to do here?" // Where (optional)
);
$totalVisits = $sqlQuery->execute()->value();
return $totalVisits;
}
What do I do for the where bit? How can I get the sum of cost for just this Category? (If I leave the where
blank it returns the sum of all project costs for every category - which is no good).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“where”部分应该是:
哦,等等,上面的内容仅适用于 $has_one,但您使用的是 Many_many 关系。
以下应该可以解决您的 Many_many 关系:
构建相关项目的 ID 数组:
然后在“where”语句中使用它们,如下所示:
the "where" part should be:
oh, wait, the above will only work für $has_one, but you're using a many_many relationship.
the following should do the trick for your many_many relationship:
build an array of IDs of related projects:
then use them in your 'where' statement like so: