Reputation: 87
Here is my project structure: http://img823.imageshack.us/img823/7047/68818300.png
I connected to database by using php.ini:
[production]
phpSetting.display_status_errors = 1
phpSetting.display_errors = 1
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "front"
resources.frontController.baseUrl = "http://localhost:8080/zendfirst/"
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "estore"
autoloadernamespaces.extendlib = "ExtendLib_"
resources.layout.layout = "index"
resources.layout.layoutPath = APPLICATION_PATH "/templates/front/default"
[developer : production]
phpSetting.display_status_errors = 0
phpSetting.display_errors = 0
I searched on google but there were many ways to do but I don't know how. I'm trying this way but it doesn't work. Here is my model:
<?php
class Model_User {
protected $db;
public function __construct() {
$this->db = Zend_Registry::get('db');
}
public function listAll() {
$sql = $this->db->query("SELECT * FROM SanPham ORDER BY Id DESC");
return $sql->fetchAll();
}
}
And this is my controller:
<?php
class IndexController extends Zend_Controller_Action {
public function indexAction() {
$muser = new Model_User();//error here
$data = $muser->listAll();
echo "<pre>";
print_r($data);
echo "</pre>";
}
public function viewAction() {
}
public function preDispatch() {
}
}
The controller doesn't know what is user model. How should I call model in controller? Here is my project: https://www.box.com/s/idw5twyyo41yn8gq1kfe
Upvotes: 0
Views: 1365
Reputation: 4898
Put in your Bootstrap.php
protected function _initAutoLoad(){
$resource_loader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
),
),
)
);
return $resource_loader;
}
Or put in your ini file:
appnamespace = "Application"
than name your model
class Application_Model_User{ ... }
Upvotes: 1
Reputation: 24645
The problem is the autoloader doesn't know where Model_Users is.
You will need to config in a resource autoloader.
If for some reason this is library code (ie under Model directory right off of one of your include paths) then just add autoloadernamespaces.models = "Model_"
to your ini file
Upvotes: 0