I am new in CakePHP. I have a CakePHP application which has streets table (with columns street_name, house_no, district, city). I need to autocomplete these data in a form and save them in an array for further processing of the data. It should be like, the user will be typing a street name in the Street Name field of a form which has fields- Street Name, House No, District and City. After selecting a street name from auto suggestions, corresponding data (House No, District and City) will be populated in the other fields of the form.
What I have tried:
Now, over 25000 rows are loaded in the streets table which is the entire street database of a city. this table is not linked with other tables of the application through foreign keys. Street model in /Model/Street.php is following:
class Street extends AppModel {
public function getStreet ($term = null) {
if(!empty($term)) {
$streets = $this->find('list', array(
'conditions' => array(
'street_name LIKE' => trim($term) . '%'
)
));
return $streets;
}
return false;
}
}
In /Controller/StreetsController.php,
class StreetsController extends AppController {
public function autocomplete($term){
if ($this->request->is('get')) {
$this->autoRender = false;
$data = $this->Street->getStreet($term);
$this->set(compact('data'));
$this->set('_serialize', array('data'));
echo json_encode($data);
}
}
}
In /webroot/js/street.js file,
(function($) {
$('#autocomplete').autocomplete({
source: "/street.json"
});
})(jQuery);
The input forms are in different controller. In View/Users/add.ctp file,
<fieldset>
<legend><?php echo __('Address Data'); ?></legend>
echo $this->Form->input('street_name', array(
'class' => 'ui-autocomplete',
'id' => 'autocomplete'));
echo $this->Form->input('house_no');
echo $this->Form->input('district');
echo $this->Form->input('city');
</fieldset>
<?php echo $this->Form->end(__('Save')); ?>
Could anyone tell me please how to proceed further ? I found some tutorials where this autocomplete function is implemented using jQuery in CakePHP. But, it is implemented using same table, model, view and controller like cars, Car.php, /View/Cars/index.ctp , CarsController.php. which is not similar to my scenario.