Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Silverstripe SQL逻辑_Php_Mysql_Silverstripe - Fatal编程技术网

Php Silverstripe SQL逻辑

Php Silverstripe SQL逻辑,php,mysql,silverstripe,Php,Mysql,Silverstripe,我正在尝试运行查询,但发生了一些奇怪的情况: $return = MyCustomPage::get()->where( " MyCustomPage.ID IN(" . implode(',', $MyCustomPageIds) . ")" )->limit(2); 这将返回一个错误,因为它试图从MyCustomPage\u Live而不是MyCustomPage获取数据的查询 这种逻辑一直在变化,有时从一个表中获取,有时从另一个表中获取,我需要在查询中指定表名(例如M

我正在尝试运行查询,但发生了一些奇怪的情况:

$return = MyCustomPage::get()->where(
    " MyCustomPage.ID IN(" . implode(',', $MyCustomPageIds) . ")"
)->limit(2);
这将返回一个错误,因为它试图从MyCustomPage\u Live而不是MyCustomPage获取数据的查询

这种逻辑一直在变化,有时从一个表中获取,有时从另一个表中获取,我需要在查询中指定表名(例如MyCustomPage.ID或MyCustomPage\u Live.ID)


是否有更好的方法来解决此问题或任何解决方案?

您应该尽可能避免原始SQL查询,并利用原始SQL查询

因此,为了实现与where语句相同的效果,但通过使用ORM,您可以编写:

$return = MyCustomPage::get()->byIDs($MyCustomPageIds)->limit(2);
这也将自动从当前阶段读取。如果需要强制某个阶段从中读取,可以使用以下方法:

// replace 'Live' with 'Stage' to read from stage. 
Versioned::set_stage('Live'); 
在这些情况下,最好先存储当前阶段,然后再恢复

$currentStage = Versioned::get_stage(); 
Versioned::set_stage('Live');

// Do your thing…

Versioned::set_stage($currentStage);

您应该尽可能避免原始SQL查询,而是利用原始SQL查询

因此,为了实现与where语句相同的效果,但通过使用ORM,您可以编写:

$return = MyCustomPage::get()->byIDs($MyCustomPageIds)->limit(2);
这也将自动从当前阶段读取。如果需要强制某个阶段从中读取,可以使用以下方法:

// replace 'Live' with 'Stage' to read from stage. 
Versioned::set_stage('Live'); 
在这些情况下,最好先存储当前阶段,然后再恢复

$currentStage = Versioned::get_stage(); 
Versioned::set_stage('Live');

// Do your thing…

Versioned::set_stage($currentStage);

您不需要使用
where
语句来执行此操作,您可以按照
CustomPage::get()->filter(['ID'=>$CustomPageArray])->limit(2)
进行操作。SilverStripe中作为第二个参数的数组在类型的查询中被视为
。使用
过滤器
也会自动解决您遇到的版本控制问题

如果您真的需要在其中使用
,类似这样的功能会有所帮助:

$extra = '';
if (Versioned::current_stage() == 'Live') {
     $extra = '_live'
}
Page::get()->where('MyFilter' . $extra . '.ID' IN implode(',', $CustomIds));
在这里输入代码


(请原谅任何语法错误;)

您不需要使用
where
语句来执行此操作,您可以按照
CustomPage::get()->filter(['ID'=>$CustomPageArray])->limit(2)
的思路执行操作。SilverStripe中作为第二个参数的数组在
类型的查询中被视为
。使用
过滤器
也会自动解决您遇到的版本控制问题

如果您真的需要在其中使用
,类似这样的功能会有所帮助:

$extra = '';
if (Versioned::current_stage() == 'Live') {
     $extra = '_live'
}
Page::get()->where('MyFilter' . $extra . '.ID' IN implode(',', $CustomIds));
在这里输入代码

(请原谅任何语法错误;))