如何加入 SilverStripe 中的两个表
How to join two tables in SilverStripe
我找遍了所有地方,但找不到解决方案。我想加入 SilverStripe 中的两个表。很简单:
class Module extends DataObject {
...
static $has_one = array(
'Website' => 'Website'
);
...
}
class Website extends DataObject {
...
static $has_many = array(
'Modules' => 'Module'
);
...
}
我想加入这两个,并得到一个DataList中的所有属性。 leftJoin()
函数不会做任何事情,在他们的网站上提到
Passing a $join statement to will filter results further by the JOINs performed against the foreign table. It will not return the additionally joined data.
我尝试使用原始查询
DB::query('SELECT * FROM "Module" LEFT JOIN "Website" ON "Website"."ID" = "Module"."WebsiteID"');
但我得到的只是这个
MySQLQuery Object ( [handle:protected] => mysqli_result Object ( [current_field] => 0 [field_count] => 19 [lengths] => [num_rows] => 5 [type] => 0 ) [currentRecord:protected] => [rowNum:protected] => -1 [queryHasBegun:protected] => )
有人知道怎么做吗?谢谢!
我找到了解决方法。它不是连接两个表的完美解决方案,尤其是当有很多属性时,但它确实给了我现在想要的东西。
$modules = Module::get();
$list = new ArrayList();
foreach($modules as $module) {
$website = Website::get()->filter(array(
'ID' => $module->WebsiteID
))->first();
$array = array("mName" => $module->Name,
"mDes" => $module->Description,
"wName" => $website->Name);
$list->push($array);
}
你确实无法通过默认的ORM获取连接数据。但是,如果您选择决定使用 DB::Query()
,您可以轻松地将它们作为数组获取。
一个例子:
$items = DB::Query("
SELECT
Module.Title,
Website.URL
FROM Module
LEFT JOIN Website ON Website.ID = Module.WebsiteID
");
if($items) {
$i = 0;
foreach($items as $item) {
$moduleTitle = $item['Title'];
$websiteURL = $item['URL'];
}
}
此选项比您建议的解决方法更快。如果您因为要使用模板中的数据而需要 ArrayList,请自己构建 ArrayList,或使用像 this.
这样的代码片段
我找遍了所有地方,但找不到解决方案。我想加入 SilverStripe 中的两个表。很简单:
class Module extends DataObject {
...
static $has_one = array(
'Website' => 'Website'
);
...
}
class Website extends DataObject {
...
static $has_many = array(
'Modules' => 'Module'
);
...
}
我想加入这两个,并得到一个DataList中的所有属性。 leftJoin()
函数不会做任何事情,在他们的网站上提到
Passing a $join statement to will filter results further by the JOINs performed against the foreign table. It will not return the additionally joined data.
我尝试使用原始查询
DB::query('SELECT * FROM "Module" LEFT JOIN "Website" ON "Website"."ID" = "Module"."WebsiteID"');
但我得到的只是这个
MySQLQuery Object ( [handle:protected] => mysqli_result Object ( [current_field] => 0 [field_count] => 19 [lengths] => [num_rows] => 5 [type] => 0 ) [currentRecord:protected] => [rowNum:protected] => -1 [queryHasBegun:protected] => )
有人知道怎么做吗?谢谢!
我找到了解决方法。它不是连接两个表的完美解决方案,尤其是当有很多属性时,但它确实给了我现在想要的东西。
$modules = Module::get();
$list = new ArrayList();
foreach($modules as $module) {
$website = Website::get()->filter(array(
'ID' => $module->WebsiteID
))->first();
$array = array("mName" => $module->Name,
"mDes" => $module->Description,
"wName" => $website->Name);
$list->push($array);
}
你确实无法通过默认的ORM获取连接数据。但是,如果您选择决定使用 DB::Query()
,您可以轻松地将它们作为数组获取。
一个例子:
$items = DB::Query("
SELECT
Module.Title,
Website.URL
FROM Module
LEFT JOIN Website ON Website.ID = Module.WebsiteID
");
if($items) {
$i = 0;
foreach($items as $item) {
$moduleTitle = $item['Title'];
$websiteURL = $item['URL'];
}
}
此选项比您建议的解决方法更快。如果您因为要使用模板中的数据而需要 ArrayList,请自己构建 ArrayList,或使用像 this.
这样的代码片段