Silverstripe SQL Logic

Joh*_*Doe 0 php mysql silverstripe

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

$return = MyCustomPage::get()->where(
    " MyCustomPage.ID IN(" . implode(',', $MyCustomPageIds) . ")"
)->limit(2);
Run Code Online (Sandbox Code Playgroud)

这会返回一个错误,因为查询它试图从MyCustomPage_Live而不是MyCustomPage获取数据.

这个逻辑就像改变一样,有时它从一个表有时从另一个表中获取,我需要在查询中指定表名(例如MyCustomPage.ID或MyCustomPage_Live.ID)

对于这个或任何解决方案有更好的方法吗?

bum*_*ack 5

您应该尽可能地避免原始SQL查询并改为使用SilverStripe ORM.

所以要实现与where语句相同的功能,但是使用ORM你会写:

$return = MyCustomPage::get()->byIDs($MyCustomPageIds)->limit(2);
Run Code Online (Sandbox Code Playgroud)

这也将自动从当前阶段读取.如果你需要强制一个阶段来阅读,你可以使用类似的东西:

// replace 'Live' with 'Stage' to read from stage. 
Versioned::set_stage('Live'); 
Run Code Online (Sandbox Code Playgroud)

在这些情况下,最好先存储当前阶段并在之后恢复.

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

// Do your thing…

Versioned::set_stage($currentStage);
Run Code Online (Sandbox Code Playgroud)