无法获得简单的cakephp搜索表单

时间:2012-02-13 15:35:25

标签: mysql forms cakephp-2.0

我不断收到我的视图中未定义变量的错误。这实际上是我的第一个搜索表单(不仅仅是蛋糕),我敢肯定我一定做错了。这是我的控制器代码: UPDATE 以下是控制器操作和html表单的新代码。

<?php
$options = array('house' => 'House', 'condo' => 'Condo', 'hotel'=>'Hotel');
$attributes = array('legend' => 'Property:<span style="font-style:italic">(Please select one)</span>');
echo $this->Form->radio('Unit.type', $options, $attributes);
?>
<?php echo $this->Form->end('Search'); ?>

给了我这个输出:

<form action="/lodgings/search" id="UnitIndexForm" method="get" accept-charset="utf-8">
<fieldset><legend>Property:<span style="font-style:italic">(Please select one)</span></legend>
<input type="hidden" name="type" id="UnitType_" value=""/><input type="radio" name="type" id="UnitTypeHouse"  value="house" />
<label for="UnitTypeHouse">House</label>
<input type="radio" name="type" id="UnitTypeCondo"  value="condo" />
<label for="UnitTypeCondo">Condo</label>
<input type="radio" name="type" id="UnitTypeHotel"  value="hotel" />
<label for="UnitTypeHotel">Hotel</label>
</fieldset>
<div class="submit">
<input  type="submit" value="Search"/>
</div>
</form>

现在我的控制器逻辑:

function search() {

$result = array();
if (isset($this->params['url'] ))
{
$type = $this->params['url'];
$conditions = array("Unit.type = " => $type, 'Unit.active'=>1);
$result = $this->Unit->find('all', array('conditions'=> $conditions));
$this->log(print_r($result, true));
$this->set('type', $result);

}

}

2 个答案:

答案 0 :(得分:1)

<input type="radio" value="house" name="data[Lodgings][type]">House

如果模型是单位,则不应该

<input type="radio" value="house" name="data[Unit][type]">House

答案 1 :(得分:1)

您正在使用Lodgings Controller并想使用Unit模型,您是否提到要使用的单位模型?

public $uses = array('Unit','Lodging');

编辑:

视图

<?php
echo $this->Form->create(false, array('id'=>'search', 'url'=>array('controller' => 'lodgings', 'action'=>'search')));             
$options = array('house' => 'House', 'condo' => 'Condo', 'hotel'=>'Hotel');
$attributes = array('legend' => 'Property:<span style="font-style:italic">(Please select one)</span>');
echo $this->Form->radio('type', $options, $attributes);
?>
<?php echo $this->Form->end('Search'); ?>

控制器:

function searchr(){
      $result = array();

      if (isset($this->data['type']))
      {
          $type = $this->data['type'];
          $conditions = array("Unit.type = " => $type, 'Unit.active'=>1);
          $result = $this->student_info->find('all', array('conditions'=> $conditions));
          $this->log(print_r($result, true));
          $this->set('type', $result);

      }
   }