File /View/Elements/lookup.ctp 不会在 /View/Layouts/default.ctp 中呈现(显示错误)?我该怎么做?



我想在/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:wampwwwlibCakeModelDatasourceDboSource.php(436): PDOStatement->execute(Array)
#1 C:wampwwwlibCakeModelDatasourceDatabaseMysql.php(307): DboSource->_execute('SHOW FULL COLUM...')
#2 C:wampwwwlibCakeModelModel.php(1226): Mysql->describe(Object(Search))
#3 C:wampwwwlibCakeViewHelperFormHelper.php(197): Model->schema()
#4 C:wampwwwlibCakeViewHelperFormHelper.php(450): FormHelper->_introspectModel('Search', 'fields')
#5 C:wampwwwazilViewElementslookup.ctp(2): FormHelper->create('Search', Array)
#6 C:wampwwwlibCakeViewView.php(595): include('C:wampwwwazi...')
#7 C:wampwwwlibCakeViewView.php(317): View->_render('C:wampwwwazi...', Array)
#8 C:wampwwwazilViewLayoutsdefault.ctp(91): View->element('lookup')
#9 C:wampwwwlibCakeViewView.php(595): include('C:wampwwwazi...')
#10 C:wampwwwlibCakeViewView.php(411): View->_render('C:wampwwwazi...')
#11 C:wampwwwlibCakeViewView.php(373): View->renderLayout('<h2>Database ta...', 'default')
#12 C:wampwwwlibCakeControllerController.php(900): View->render('error500', NULL)
#13 C:wampwwwlibCakeErrorExceptionRenderer.php(282): Controller->render('error500')
#14 C:wampwwwlibCakeErrorExceptionRenderer.php(191): ExceptionRenderer->_outputMessageSafe('error500')
#15 [internal function]: ExceptionRenderer->_cakeError(Object(MissingTableException))
#16 C:wampwwwlibCakeErrorExceptionRenderer.php(165): call_user_func_array(Array, Array)
#17 C:wampwwwlibCakeErrorErrorHandler.php(127): ExceptionRenderer->render()
#18 [internal function]: ErrorHandler::handleException(Object(MissingTableException))
#19 {main} [<b>CORECakeErrorErrorHandler.php

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

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

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

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

public $useTable = false;

最新更新