假设我有一个名为local_ads
的数据库表。
现在,当一个本地广告被创建,一个人必须能够看到它的预览,如果他满意,然后保存它。此外,如果一个人想要更新一个本地广告,那么他可能希望看到它的预览,然后他覆盖现场版本的记录。
因此,我为local_ads
表提供了一个名为parent_id
的外键。如果这是null,那么它就是一个预览(至少根据我最初的想法)。否则就是现场直播。当保存预览时,有两种情况:
案例1:还没有与预览链接的实时记录。在这种情况下,一个新的记录被插入到local_ads
表中,parent_id
指向预览。
案例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
(我不知道如何使用逻辑操作数之间的逻辑or
,使用fRecordSet
的build
方法)。而且,这个查询是二维的,速度很慢。另外,我想知道如何执行子查询。另外,我不知道如何使用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');
}
注意,在某些情况下,我也必须检查域。
发布于 2013-12-15 05:47:44
与此同时,我设法解决了这个问题。这就是我们如何定义过滤器集来解决问题中提到的问题:
$filters = array();
if ($User->getType() == 'local_admin') {
$filters['domain='] = $User->getDomain();
}
$filters['parent_id<:|parent_id>=:'] = array('id', 'id');
https://stackoverflow.com/questions/20591397
复制相似问题