Bonjour à tous.
Voilà en fait mon problème est simple, je n'arrive pas à définir des modèles (pour la DB) et les utiliser dans mes controllers.
Tout les tutos que je fais m'indique de créer mon modèle et simplement dans l'action de mon controller faire un $maVariable = new MonModèle(); pour l'instancier. Seulement pas de bol pour moi, j'ai un message d'erreur m'indiquant qu'il était impossible de charger le fichier du modèle.
Alors j'en viens à me dire que c'est un problème dans le bootstrap qui parmi l'ensemble des tuto vus ne ressemble absolument pas à l'un d'eux. Il faut savoir que j'ai suivi l'exemple de bootstrap indiqué dans le Quickstart de Zend.
Je vous le donne donc:
Et donc le fichier bootstrap.php qui lui est dans le répertoire /application
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 <?php error_reporting(E_ALL|E_STRICT); ini_set('display_errors', 1); date_default_timezone_set('Europe/Brussels'); // Step 1: APPLICATION_PATH is a constant pointing to our // application/subdirectory. We use this to add our "library" directory // to the include_path, so that PHP can find our Zend Framework classes. define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/')); set_include_path( APPLICATION_PATH . '/../library' . PATH_SEPARATOR . get_include_path() ); // Step 2: AUTOLOADER - Set up autoloading. // This is a nifty trick that allows ZF to load classes automatically so // that you don't have to litter your code with 'include' or 'require' // statements. require_once "Zend/Loader.php"; Zend_Loader::registerAutoload(); // Step 3: REQUIRE APPLICATION BOOTSTRAP: Perform application-specific setup // This allows you to setup the MVC environment to utilize. Later you // can re-use this file for testing your applications. // The try-catch block below demonstrates how to handle bootstrap // exceptions. In this application, if defined a different // APPLICATION_ENVIRONMENT other than 'production', we will output the // exception and stack trace to the screen to aid in fixing the issue try { require '../application/bootstrap.php'; } catch (Exception $exception) { echo '<html><body><center>' . 'An exception occured while bootstrapping the application.'; if (defined('APPLICATION_ENVIRONMENT') && APPLICATION_ENVIRONMENT != 'production' ) { echo '<br /><br />' . $exception->getMessage() . '<br />' . '<div align="left">Stack Trace:' . '<pre>' . $exception->getTraceAsString() . '</pre></div>'; } echo '</center></body></html>'; exit(1); } // Step 4: DISPATCH: Dispatch the request using the front controller. // The front controller is a singleton, and should be setup by now. We // will grab an instance and call dispatch() on it, which dispatches the // current request. Zend_Controller_Front::getInstance()->dispatch();
Tout le reste respecte la manière d'organiser les fichiers, les vues, les controller, les modèles... bref je ne comprend pas mon erreur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 <?php // Step 1: APPLICATION CONSTANTS - Set the constants to use in this application. // These constants are accessible throughout the application, even in ini // files. We optionally set APPLICATION_PATH here in case our entry point // isn't index.php (e.g., if required from our test suite or a script). defined('APPLICATION_PATH') or define('APPLICATION_PATH', dirname(__FILE__)); defined('APPLICATION_ENVIRONMENT') or define('APPLICATION_ENVIRONMENT', 'development'); // Chargement de la configuration DB $config = new Zend_Config_Ini('../application/configuration.ini', 'developpement'); $registry = Zend_Registry::getInstance(); $registry->set('config', $config); // Mise en place de la BDD $db = Zend_Db::factory($config->db); Zend_Db_Table::setDefaultAdapter($db); // Step 2: FRONT CONTROLLER - Get the front controller. // The Zend_Front_Controller class implements the Singleton pattern, which is a // design pattern used to ensure there is only one instance of // Zend_Front_Controller created on each request. $frontController = Zend_Controller_Front::getInstance(); // Step 3: CONTROLLER DIRECTORY SETUP - Point the front controller to your action // controller directory. $frontController->setControllerDirectory(APPLICATION_PATH . '/controllers'); // Step 4: APPLICATION ENVIRONMENT - Set the current environment. // Set a variable in the front controller indicating the current environment -- // commonly one of development, staging, testing, production, but wholly // dependent on your organization's and/or site's needs. $frontController->setParam('env', APPLICATION_ENVIRONMENT); // Step 5: CLEANUP - Remove items from global scope. // This will clear all our local boostrap variables from the global scope of // this script (and any scripts that called bootstrap). This will enforce // object retrieval through the applications's registry. unset($frontController); ?>
Je vous donne également le modèle incriminé et le controller qui l'appelle.
Model: /application/models/Employee
Controllers: application/controllers/IndexController.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <?php class Employee extends Zen_Db_Table { protected $_name = 'employee'; } ?>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <?php class IndexController extends Zend_Controller_Action { public function indexAction() { $this->view->title = "Identification"; $employee = new Employee(); $this->view->employees = $employee->fetchAll(); } } ?>
Partager