0

我想在 /View/Layouts/default.ctp (这是网站的主页演示布局)中嵌入一个 HTML 输入表单(搜索栏)。我使用以下代码创建了 /View/Elements/lookup.ctp(我编写 Element 是因为我想在网站的每个页面上都包含该搜索栏):

<?php
echo $this->Form->create('Search', array('action' => 'lookup', 'accept-charset' => 'utf-8'));
echo $this->Form->input(array('type' => 'search', 'name' => 'search', 'placeholder' => 'enter search term'));
echo $this->Form->button('Search', array('type' => 'button', 'value' => 'submit')); 
echo $this->Form->end();
?>

搜索栏本身应如下所示:

___________________________     ______________
| enter search term       |    |  Search      | <--- button
|_________________________|    |______________|
    ↑
 search bar

/View/Layouts/default.ctp 中生成的代码必须是这样的:

<form id="searchForm" method="post" action="/searches/lookup" accept-charset="utf-8">
    <input type="search" name="search" placeholder="enter search term" />
    <button type="button" name="submit" value="submit">Search</button>
</form>

我包括行:

<?php echo $this->element('lookup'); ?>

在 /View/Layouts/default.ctp (嵌入元素)中,但是当我转到主页时,它不会呈现此元素并出现错误:

(@Wylie:是的,你是对的。这不是整个错误。这是完整的错误)

#0 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(436): PDOStatement->execute(Array)
#1 C:\wamp\www\lib\Cake\Model\Datasource\Database\Mysql.php(307): DboSource->_execute('SHOW FULL COLUM...')
#2 C:\wamp\www\lib\Cake\Model\Model.php(1226): Mysql->describe(Object(Search))
#3 C:\wamp\www\lib\Cake\View\Helper\FormHelper.php(197): Model->schema()
#4 C:\wamp\www\lib\Cake\View\Helper\FormHelper.php(450): FormHelper->_introspectModel('Search', 'fields')
#5 C:\wamp\www\azil\View\Elements\lookup.ctp(2): FormHelper->create('Search', Array)
#6 C:\wamp\www\lib\Cake\View\View.php(595): include('C:\wamp\www\azi...')
#7 C:\wamp\www\lib\Cake\View\View.php(317): View->_render('C:\wamp\www\azi...', Array)
#8 C:\wamp\www\azil\View\Layouts\default.ctp(91): View->element('lookup')
#9 C:\wamp\www\lib\Cake\View\View.php(595): include('C:\wamp\www\azi...')
#10 C:\wamp\www\lib\Cake\View\View.php(411): View->_render('C:\wamp\www\azi...')
#11 C:\wamp\www\lib\Cake\View\View.php(373): View->renderLayout('<h2>Database ta...', 'default')
#12 C:\wamp\www\lib\Cake\Controller\Controller.php(900): View->render('error500', NULL)
#13 C:\wamp\www\lib\Cake\Error\ExceptionRenderer.php(282): Controller->render('error500')
#14 C:\wamp\www\lib\Cake\Error\ExceptionRenderer.php(191): ExceptionRenderer->_outputMessageSafe('error500')
#15 [internal function]: ExceptionRenderer->_cakeError(Object(MissingTableException))
#16 C:\wamp\www\lib\Cake\Error\ExceptionRenderer.php(165): call_user_func_array(Array, Array)
#17 C:\wamp\www\lib\Cake\Error\ErrorHandler.php(127): ExceptionRenderer->render()
#18 [internal function]: ErrorHandler::handleException(Object(MissingTableException))
#19 {main} [<b>CORE\Cake\Error\ErrorHandler.php

桌子不见了?!缺少什么表,它只需要在 /View/Layouts/default.ctp 中添加几行 HTML 行。我不明白发生了什么事。请帮忙。谢谢你。

4

1 回答 1

0

Cake 期望一个模型有一个对应的数据库表。根据不完整的错误输出,Cakesearches在数据库中查找表,但找不到。因为您包含一个element包含属于搜索模型的表单,所以 Cake 会在数据库中查询列的描述以正确显示该表单。

您确定要使用单独的控制器和模型进行搜索吗?search也可以是相关控制者的动作(PostsController例如 )。

当然,如果您需要一个可以查询多个模型(例如帖子、页面、用户)的全局搜索模型,建模可能是有意义的。你可以告诉 Cake Model 没有数据库表:

public $useTable = false;
于 2012-02-02T12:29:49.880 回答