Search code examples
model-view-controllercakephp-2.0formhelperviewrendering

File /View/Elements/lookup.ctp won't render inside /View/Layouts/default.ctp (error displayed)? How can I do it?


I want to embed an HTML input form (search bar) in /View/Layouts/default.ctp (which is Homepage presentation layout of the site). I've created /View/Elements/lookup.ctp with the following code (I wrote Element because I wanted to include that search bar on every page of the site):

<?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();
?>

The search bar itself should look like this:

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

The generated code in /View/Layouts/default.ctp must be like this:

<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>

I included line:

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

in /View/Layouts/default.ctp (which embeds the Element), but when I go to Homepage, it won't render this element and the error appears:

( @Wylie: Yes, you're right. It wasn't the whole error. Here's the complete error )

#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

Missing table?! What missing table, it just need to add a couple of HTML lines inside a /View/Layouts/default.ctp. I don't understand what's going on. Please help. Thank you.


Solution

  • Cake expects a Model to have a corresponding database table. Based on the incomplete error output Cake looks for a searches table in the database, but can't find it. Because you include an element which contains a form belonging to the Search Model Cake queries the database for a description of the columns to correctly display the form.

    Are you certain you want to have a separate Controller and Model for searching? search could also be an action of the relevant Controller (the PostsController, for example).

    Of course, if you need a global Search model which can query several models (Posts, Pages, Users, for example) modelising might make sense. You can tell Cake that a Model doesn't have a database table:

    public $useTable = false;