【发布时间】:2011-08-10 20:23:21
【问题描述】:
好吧,我是 cakephp 的新手,我有一个包含多个字段的搜索表单,并且我有一些在搜索表单中是可选的字段。我要做的是在提交表单时检查这些字段是否不为 NULL 并将它们应用于我的 find('all') 查询。
这是我的 ctp 视图
<h2>IDX Listings</h2>
<?php echo $this->Form->create('Listing', array('action'=>'results')); ?>
<fieldset><legend>Location Criteria</legend>
<?php echo $this->Form->input('listing_countyid', array('type'=>'select', 'options'=>$counties, 'default'=>'Richmond')); ?>
<?php echo $this->Form->input('listing_area', array('type'=>'select', 'options'=>$areas)); ?>
<?php echo $this->Form->input('listing_neighborhood', array('type'=>'select', 'options'=>$hoods)); ?>
<?php echo $this->Form->input('listing_subdivision', array('type'=>'select')); ?>
</fieldset>
<fieldset><legend>Market</legend>
<?php echo $this->Form->input('min_price', array('type'=>'select', 'options'=>$minPrices)); ?>
<?php echo $this->Form->input('max_price', array('type'=>'select', 'options'=>$maxPrices)); ?>
<?php echo $this->Form->input('listing_yearbuilt', array('type'=>'select', 'options'=>$years, 'label'=>'Built After')); ?>
</fieldset>
<fieldset><legend>Size</legend>
<?php echo $this->Form->input('min_rooms', array('type'=>'select', 'options'=>$minRooms)); ?>
<?php echo $this->Form->input('max_rooms', array('type'=>'select', 'options'=>$maxRooms)); ?>
<?php echo $this->Form->input('min_stories', array('type'=>'select', 'options'=>$stories, 'label'=>'Min Stories')); ?>
<?php echo $this->Form->input('min_listing_sqfttotal', array('type'=>'select', 'options'=>$minSqft, 'label'=> 'Min Sq Ft'));
<?php echo $this->Form->input('max_listing_sqfttotal', array('type'=>'select', 'options'=>$maxSqft, 'label'=> 'Max Sq Ft'));
?>
<?php echo $this->Form->input('min_bathrooms', array('type'=>'select', 'options'=>$minBath, 'label'=>'Min Bathrooms')); ?>
<?php echo $this->Form->input('max_bathrooms', array('type'=>'select', 'options'=>$maxBath, 'label'=>'Max Bathrooms')); ?>
<?php echo $this->Form->input('min_bedrooms', array('type'=>'select', 'options'=>$minBed, 'label'=>'Min Bedrooms')); ?>
<?php echo $this->Form->input('max_bedrooms', array('type'=>'select', 'options'=>$maxBed, 'label'=>'Max Bedrooms')); ?>
</fieldset>
<fieldset><legend>Options</legend>
<?php echo $this->Form->input('heat_type', array('type'=>'select', 'options'=>$heat_type, 'label'=>'Heating System')); ?>
<?php echo $this->Form->input('cool_type', array('type'=>'select', 'options'=>$cool_type, 'label'=>'Cooling System')); ?>
<?php echo $this->Form->input('irrigation_system', array('type'=>'select', 'options'=>$irrigation_system, 'label'=>'Irrigation System')); ?>
<?php echo $this->Form->input('handicap', array('type'=>'select', 'options'=>$handicap, 'label'=>'Handicap Equipped')); ?>
<?php echo $this->Form->input('fireplace', array('type'=>'select', 'options'=>$fireplace, 'label'=>'Fireplace')); ?>
<?php echo $this->Form->input('fence', array('type'=>'select', 'options'=>$fence, 'label'=>'Fenced')); ?>
<?php echo $this->Form->input('level1_mstr', array('type'=>'select', 'options'=>$level1_mstr, 'default'=>'No', 'label'=>'1st Floor Master Bedroom')); ?>
<?php echo $this->Form->input('level1_laundry', array('type'=>'select', 'options'=>$level1_laundry, 'default'=>'No', 'label'=>'1st Floor Laundry Room')); ?>
</fieldset>
<?php echo $this->Form->submit('Search', array('target'=>"SearchResults", 'id'=>'submit')); ?>
标题为 Options 的字段集是可选字段 Optional 字段值设置为初始值为 NULL 的数组
这是我的控制器内的搜索功能
function results() {
if (!empty($this->data)) {
$listings = $this->Idx->find('all', array(
'conditions'=>array(
'listing_countyid' => $this->data['Listing']['listing_countyid'],
'listing_area' => $this->data['Listing']['listing_area'],
'listing_neighborhood' => $this->data['Listing']['listing_neighborhood'],
'listing_listprice >=' => $this->data['Listing']['min_price'],
'listing_listprice <=' => $this->data['Listing']['max_price'],
'listing_rooms >=' => $this->data['Listing']['min_rooms'],
'listing_rooms <=' => $this->data['Listing']['max_rooms'],
'listing_sqfttotal >=' => $this->data['Listing']['min_listing_sqfttotal'],
'listing_sqfttotal <=' => $this->data['Listing']['max_listing_sqfttotal'],
'listing_stories >=' => $this->data['Listing']['min_stories'],
'listing_yearbuilt >=' => $this->data['Listing']['listing_yearbuilt'],
'listing_bathstotal >=' => $this->data['Listing']['min_bathrooms'],
'listing_bathstotal <=' => $this->data['Listing']['max_bathrooms'],
'listing_bedrooms >=' => $this->data['Listing']['min_bedrooms'],
'listing_bedrooms <=' => $this->data['Listing']['max_bedrooms'],
'listing_roommasterbrlevel' => $this->data['Listing']['level1_mstr'],
'listing_roomlaundrylevel' => $this->data['Listing']['level1_laundry'],
'listing_heatsystem' => $this->data['Listing']['heat_type'],
'listing_coolsystem' => $this->data['Listing']['cool_type'],
'listing_irrigationsrc' => $this->data['Listing']['irrigation_system'],
'listing_fireplaces' => $this->data['Listing']['fireplace'],
'listing_handicap' => $this->data['Listing']['handicap'],
'listing_fence' => $this->data['Listing']['fence']
)
));
$this->set('listings', $listings);
}
}
如果我从我的 find() 查询中删除选项,我返回的结果是正确的,但是一旦我在视图中设置了一个值,我就不会返回正确的结果。我知道 find() 准备语句背后的 SQL 语法是 SELECT * FROM table WHERE column_name AND column_name 等。但是将 NULL 值传递给数据库将 (1) 不返回结果或 (2) 返回错误的结果。我在控制器中尝试了许多不同的方法,使用 OR 语句,但 OR 是为了概括并返回许多结果。
有没有办法通过 beforeFind() 函数在控制器或我的模型中修改 SQL 语句(beforeFind 超出了我的知识范围,并且很难找到它在使用中的可靠示例),或者我会最好在 Model __construct() 函数中检查值并修改 SQL 语句?
【问题讨论】:
-
so.. 你的选择框中有 NULL 值吗?生成的 SQL 查询是什么?
-
生成的查询是 SELECT * FROM mls_cvrmls WHERE column_name AND column_name AND column_name 等。是的,我的选择框中确实有 NULL 值。
-
输入总是在回发中发布(除非它们被禁用),并且你总是得到一个字符串。你永远不会得到 NULL 回来。 (注意:复选框仅在选中时发布,单选按钮在选择组中的一个按钮时发布,值显示选择了哪个单选按钮。)
标签: php mysql sql database cakephp