假设我有一个名为local_ads
的数据库表。
现在,当创建本地广告时,必须能够查看其预览,如果他满意,则保存它。此外,如果他想更新本地广告,则他可能希望在覆盖记录的实时版本之前查看其预览。
所以,我有一个local_ads
表的外键,叫做parent_id
。如果这是空的,那么这是一个预览(至少根据我最初的想法)。否则它就是活的。保存预览时,有两种情况:
案例1:还没有链接到预览的现场记录。在这种情况下,在parent_id
指向预览的情况下,将新记录插入到local_ads
表中。
案例2:有一个实时记录链接到预览。在这种情况下,实时记录会被更新。
一切看起来和工作都很好,但我在网格中显示结果时遇到了问题。如果不存在记录的实时版本,我想显示预览,如果存在,只显示实时版本。我想展示的精神
select col1, col2, col3, col4
from local_ads glob
where (not (parent_id is null))
or ((select id from local_ads temp where temp.parent_id = glob.id limit 0, 1) is null)
但我有几个问题。我们有一个逻辑or
(我想知道如何使用floushlib的fRecordSet
的build
方法在逻辑操作数之间使用逻辑or
)。此外,这个查询是二维的,速度很慢。此外,我想知道如何执行子查询。此外,我不知道如何使用is
运算符,就像在is null
中一样。
所以,我不得不重新思考我的想法,我想出了以下办法:
select col1, col2, col3, col4
from local_ads
where parent_id < id or parent_id >= id
想法很简单:如果预览没有实时版本,那么parent_id与id匹配,否则预览的parent_id为null。我知道这是一个丑陋的破解,但这是我能想出的解决问题、降低内存和性能复杂性的最好主意。
因此,剩下的唯一问题是检查where子句中由逻辑or
分隔的两个逻辑值。
从文件中我看到了:
* 'column<:' => 'other_column' // column < other_column
这个:
* 'column>=:' => 'other_column' // column >= other_column
所以我知道如何将这些添加到过滤器中,但我应该如何"或"它们?
到目前为止,我已经尝试过这种方式:
public static function localAd() {
$User = Globe::load('CurrentUser');
$Smarty = Globe::load('Smarty');
//handle default options
$options = array(
'recordsPerPage' => 20,
'pageLinks' => 10,
);
$page = 0;
if (isset($_GET['p'])) {
$page = $_GET['p'];
}
//get the data
$startIndex = (isset($page)) ? $page * $options['recordsPerPage'] : 0;
$filters = array();
if ($User->getType() == 'local_admin') {
$filters['domain='] = $User->getDomain();
}
$records = fRecordSet::build('LocalAd', $filters, array('created' => 'desc'), $options['recordsPerPage'], $page + 1);
//create result object for pagination
$Result = array(
"recordsReturned" => $records->count(),
"totalRecords" => $records->count(true),
"startIndex" => intval($startIndex),
"records" => $records->export(),
'recordsPerPage' => $options['recordsPerPage'],
'pageLinks' => $options['pageLinks'],
'currentPage' => $page,
//'options' => $options
);
$Result['totalPages'] = ceil($Result['totalRecords'] / $Result['recordsPerPage']);
$Smarty->assign('Result', $Result);
$Smarty->assign('ManagerURL', '?a=localAd');
AdminView::display('Admin/LocalAd/main.tpl');
}
注意,在某些情况下,我还必须检查域。
与此同时,我设法解决了这个问题。这就是我们如何定义过滤器集来解决问题中提到的问题:
$filters = array();
if ($User->getType() == 'local_admin') {
$filters['domain='] = $User->getDomain();
}
$filters['parent_id<:|parent_id>=:'] = array('id', 'id');