简体   繁体   中英

How to import table in CakePHP Helper?

Hi i am writing the following code in the ZipcodeClassHelper.php

public function get_zip_point($table,$zip) {

  App::import('Model','Driver_location');
  $MyModel = new Driver_location();

  $qry = $MyModel->find("all",array('conditions'=>array('zip'=>$zip)));
  pr($qry); exit;
}

I got the error message:

Error: Class 'Driver_location' not found
File: E:\xampp\htdocs\2014\cab-zone\cabs\app\View\Helper\ZipcodeClassHelper.php
Line: 25

That's violating the MVC pattern, you don't fetch data in the view. Your model is not well named either, it doesn't follow the convention, it should be DriverLocation.

Set your data from the controller to the view:

$this->set('whatever', $this->Model->find('...'));

Do the blog tutorial to get an idea of how Cake work with it's conventions and MVC.

Loading a Model in CakePHP when not inside a Controller can be done like this:

$ModelName = ClassRegistry::init('ModelName');

Then query the model:

$result = $ModelName->find('all');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM