TYPO3 Extbase - 基本 CRUD/从数据库中选择值并在 v8 中呈现
TYPO3 Extbase - Basic CRUD / Selecting values from database and render in v8
我试图从 tt_content 获取一些数据进行测试,就像这里的 'SELECT a single row' 示例 (https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/BasicCrud/Index.html#select-a-single-row):
在Controller.php中:
...
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
...
public function listAction() {
$uid = 4;
$tester = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tt_content')
->select(
['uid', 'pid', 'bodytext'],
'tt_content',
[ 'uid' => (int)$uid ]
)
->fetch() // 1. attempt
->execute(); // 2. attempt
$this->view->assign('inet', $tester);
}
List.html:
<f:debug>{inet}</f:debug>
当我使用 ->execute() 时的调试输出是:
Extbase Variable Dump
TRUE
我使用 ->fetch() 时的调试输出是:
Extbase Variable Dump
FALSE
我看到了你的尝试,但你试过了吗:
->execute()->fetch()
?这应该可以解决问题。
所以有效的是例如这个
$uid = 10;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$queryBuilder->getRestrictions()->removeAll();
$statement = $queryBuilder
->select('uid', 'pid', 'header')
->from('tt_content')
->where(
$queryBuilder->expr()->eq('uid', $uid)
)
->execute();
while ($row = $statement->fetchAll()) {
$this->view->assign('inet', $row);
}
我试图从 tt_content 获取一些数据进行测试,就像这里的 'SELECT a single row' 示例 (https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/BasicCrud/Index.html#select-a-single-row):
在Controller.php中:
...
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
...
public function listAction() {
$uid = 4;
$tester = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tt_content')
->select(
['uid', 'pid', 'bodytext'],
'tt_content',
[ 'uid' => (int)$uid ]
)
->fetch() // 1. attempt
->execute(); // 2. attempt
$this->view->assign('inet', $tester);
}
List.html:
<f:debug>{inet}</f:debug>
当我使用 ->execute() 时的调试输出是:
Extbase Variable Dump
TRUE
我使用 ->fetch() 时的调试输出是:
Extbase Variable Dump
FALSE
我看到了你的尝试,但你试过了吗:
->execute()->fetch()
?这应该可以解决问题。
所以有效的是例如这个
$uid = 10;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$queryBuilder->getRestrictions()->removeAll();
$statement = $queryBuilder
->select('uid', 'pid', 'header')
->from('tt_content')
->where(
$queryBuilder->expr()->eq('uid', $uid)
)
->execute();
while ($row = $statement->fetchAll()) {
$this->view->assign('inet', $row);
}