On Feb 18, 2:24 pm, rb <[email protected]> wrote:
> However the problem lies in that that name input field is *not*
> required. So if I submit a form with zip = 00001 and name = [empty
> string]
>
> the SQL equivalent is obviously,
> SELECT *
> FROM schools
> WHERE zip_mail = 00001
> AND name = ''
>
> This is obviously wrong because I only want the last AND condition if
> name is not empty. So i'm not sure where to go from here...
Just check to see if the name is empty. If it isn't empty, include it
in the find conditions. One possible solution:
$formSchools = $this->data['schools'];
$conditions = array(
'School.zip_mail' => $formSchools['zip_mail']
);
if (!empty($formSchools['name']))
$conditions['School.name'] = $formSchools['name'];
$this->set('querySearchSchools', $this->School->find('all', array
('conditions' => $conditions)));
Note that you should use the newer form of the find() method. In other
words, specify as the first parameter to find(): 'all' if you have the
possibility of finding multiple records, 'first' if you want the first
record found, or any of the other valid find methods defined in the
online manual. The second parameter should be an array containing
arrays for each of the query options you wish to have. Additional
examples:
$fields = array('School.city', 'School.start_date');
$this->set('querySearchSchools', $this->School->find('all', array
('conditions' => $conditions, 'fields' => $fields, 'recursive' =>
-1)));
In the example above, the city and date the school came into existance
would be retrieved for any school that met the specified conditions.
Also, the 'recursive' => -1 part tells Cake to do no joins with other
models/tables and to just retrieve data strictly from the School model/
table.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---