- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个面向对象的 PHP 应用程序。我有一个存储在 SQL 表中的简单层次结构(可以分配给章节的章节和作者)。
我编写了以下方法来在单个查询中获取章节和作者,然后遍历结果,找出哪些行属于同一章节,并创建 Chapter 对象和 Author 对象数组。
不过,我觉得这可以做得更整洁。有人可以帮忙吗?
function getChaptersWithAuthors($monographId, $rangeInfo = null) {
$result =& $this->retrieveRange(
'SELECT mc.chapter_id, mc.monograph_id, mc.chapter_seq, ma.author_id, ma.monograph_id, mca.primary_contact, mca.seq, ma.first_name, ma.middle_name, ma.last_name, ma.affiliation, ma.country, ma.email, ma.url
FROM monograph_chapters mc
LEFT JOIN monograph_chapter_authors mca ON mc.chapter_id = mca.chapter_id
LEFT JOIN monograph_authors ma ON ma.author_id = mca.author_id
WHERE mc.monograph_id = ?
ORDER BY mc.chapter_seq, mca.seq',
$monographId, $rangeInfo
);
$chapterAuthorDao =& DAORegistry::getDAO('ChapterAuthorDAO');
$chapters = array();
$authors = array();
while (!$result->EOF) {
$row = $result->GetRowAssoc(false);
// initialize $currentChapterId for the first row
if ( !isset($currentChapterId) ) $currentChapterId = $row['chapter_id'];
if ( $row['chapter_id'] != $currentChapterId) {
// we're on a new row. create a chapter from the previous one
$chapter =& $this->_returnFromRow($prevRow);
// set the authors with all the authors found so far
$chapter->setAuthors($authors);
// clear the authors array
unset($authors);
$authors = array();
// add the chapters to the returner
$chapters[$currentChapterId] =& $chapter;
// set the current id for this row
$currentChapterId = $row['chapter_id'];
}
// add every author to the authors array
if ( $row['author_id'] )
$authors[$row['author_id']] =& $chapterAuthorDao->_returnFromRow($row);
// keep a copy of the previous row for creating the chapter once we're on a new chapter row
$prevRow = $row;
$result->MoveNext();
if ( $result->EOF ) {
// The result set is at the end
$chapter =& $this->_returnFromRow($row);
// set the authors with all the authors found so far
$chapter->setAuthors($authors);
unset($authors);
// add the chapters to the returner
$chapters[$currentChapterId] =& $chapter;
}
}
$result->Close();
unset($result);
return $chapters;
}
PS:_returnFromRow 方法只是在给定 SQL 行的情况下构造一个 Chapter 或 Author 对象。如果需要,我可以在此处发布这些方法。
编辑:我不能一次将一位作者附加到章节中。由于 Chapter 对象的结构,我需要一次添加它们。
最佳答案
我想目标是让这个更简单、更容易理解。像这样的伪代码怎么样? :
execute query
chapters = array()
for each result:
chapter = result['chapter']
author = result['author']
if (!isset(chapters[chapter]))
chapters[chapter] = new Chapter
chapters[chapter].authors.append(author)
与其创建 authors 数组,填充它,存储它,然后重新开始每一章,我们可以使代码更具可读性和更短(尽管可能会慢一点)。简而言之,对于每个结果,它都会查看章节是否在章节数组中。如果没有,它会创建它。然后它将作者添加到章节中。
关于php - 算法优化: returning object and sub-objects from single SQL statement in PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2619793/
在下面的方法中,如何将第一个Single的结果传递给第二个Single? 如现在所写,当它返回时,somethingId 是空的。如果我将返回的 Single.just(somethingId) 中的
我发现很多帖子都在回答类似的问题(例如“如何用 / 替换 \”或“如何替换 \\” by \”。我理解所有这些,但没有一个能解决我的特殊问题。这里是: 我正在从注册表中读取路径字符串,其中包含“///
注意:事实证明,toCompletable() 并非错误,而是调用代码。调用代码使得更改此方法使其工作(或不工作)。 我有以下方法。它返回一个 Single。有用。执行内部代码,以便 remoteDa
react-native repo here 中的一个文件中有一段代码如下图: export type Operation = & {instanceID: DebugID} & (
当使用带有 Single() 的 LINQ 时,我的代码行总是带有绿色下划线,并带有建议“替换为对 single 的单一调用”。这是什么意思?下面是产生该建议的一行代码示例: var user = d
讨论来自 this answer让我好奇。哪个更快: someEnumerable.Single(predicate); 或 someEnumerable.Where(predicate).Singl
我正在使用 Keycloak 作为单点登录 (SSO) 平台的 OP。我已经将我的两个 Web 应用程序连接到 Keycloak,以便使用单点登录功能。 此外,我已经制作了一个应用程序,当注销时将被重
我的步骤是: 创建单个值 x - 可能会占用一些 CPU 资源 使用值x来执行IO操作。这已经返回 Completable 返回x 所以我想这样做: Single result =
我想知道是否有人可以阐明这个问题,什么时候使用 Single.fromCallable( ()-> myObject ) 代替 Single.just(myObject) 根据文档,Single.fr
我有两个 Singles 来源,我将它们组合成一个 Single of Pair。 假设我们对这些来源有两种方法: private Single single1() {} private Single
我想将单个 Intel CPU 内核的速度与单个 nVidia GPU 内核的速度(即:单个 CUDA 代码、单个线程)进行比较。我确实实现了以下简单的二维图像卷积算法: void convoluti
我在实现 Ping Federate 时遇到此问题 Error - Single Sign-On Single sign-on authentication was unsuccessful (ref
我有几个 api 调用(Rx singles),我想将它们组合成一个 Single。我正在使用 Single.merge 尝试合并这些调用的结果,但是当我订阅响应时,我得到一个空数组,因为订阅已经发生
早上好。我的代码有问题 bootsfaces 。我需要我的 DataTable 支持单行选择,但不支持多行选择。但是,我的表格始终只使用多项选择。 这是我的代码: 我没有进行简单的选择,因为我引用了
我怎样才能像下面的代码那样使用字符串。 $str = 'Is yo"ur name O'reil"ly?'; 上面的代码只是一个例子..我需要使用包含单引号和双引号的大 html 模板。我尝试了 Ad
我有一组地理空间+时间数据和一些附加属性,我将在 map 上显示这些数据。该集合目前有几百万份文件,并且会随着时间的推移而增加。 每个文档都有以下字段: 位置:[geojson 对象] 日期:[日期对
我目前在 .NET 2.0 下使用 SharpZipLib,通过它我需要将单个文件压缩为单个压缩存档。为此,我目前正在使用以下内容: string tempFilePath = @"C:\Users\
我有 table create table1( column1 number(10, column2 number(10), column3 number(10) ); column1是主
考虑下面这段代码,我正在尝试使用 Executors.newFixedThreadPool(1).asCoroutineDispatcher()创建单线程调度程序;我想要 launch(singleT
我面临着困惑,举个例子 4 Single: val s1 : Single = service1.execute().subscribeOn(io()) val s2 : Single = servi
我是一名优秀的程序员,十分优秀!