Growing Together with Zend framework (III)

Source: Internet
Author: User
Tags mysql command line zend framework

On: Growing Together with Zend Framework (2) the common HTML code is very fast, obviously there are many identical HTML code in our view. We extract them to the header. phtml and footer. phtml files, which are placed in the script directory. They are used to store the "common" HTML extracted from the view template. The new file is zf-tutorial/application/views/scripts/header. again, we need to modify the phtml view: zf-tutorial/application/views/scripts/index. phtml zf-tutorial/application/views/scripts/index/add. phtml zf-tutorial/application/views/scripts/index/edit. phtml zf-tutorial/application/views/scripts/index/delete. although this is just a tutorial, we need CSS files to make our programs look printable. Because the URL does not point to the correct root directory, we do not know how to locate the CSS file, which leads to a small problem. To solve this problem, we use the getBaseURL () function, which is part of the request and passes it to the view. This provides a URL that we don't know. Modifying IndexController: init () looks like this: zf-tutorial/application/controllers/IndexController. in php, we need to add the CSS file to the header. in the phtml section: zf-tutorial/application/views/scripts/header. finally, we need some CSS styles: This shoshould make it look slightly prettier! Since the database separates program control from the display view, it is time to take a look at the model section in the program. Remember, the model is the core intent of the processing program (the so-called "business rules"). Therefore, it is a database for us. We will use the Zend_Db_Table class in Zend Framework to operate the insert, update, and delete records of tables in the database. Configuration in order to use Zend_Db_Table, we need to tell it which database (along with the user and password) will be used. Because we do not want hard-code in the program, we use a configuration file to save this information. Zend Framework provides a Zend_Config to provide flexible object-oriented access to the configuration file. At this moment, the configuration file can be a PHP array, an INI file, or an XML file. We will use the INI file: zf-tutorial/application/config. ini QUOTE: [general] db. adapter = PDO_MYSQL db. config. host = localhost db. config. username = rob db. config. password = 123456 Page 11 of 18 db. config. dbname = zftest obviously, you should use your own user name, password and Database Name, instead of mine! It is very easy to use Zend_Config: QUOTE: $ config = new Zend_Config_Ini ('config. ini ', 'section'); note that in this example, Zend_Config_Ini loads a section from the INI file, instead of each section (even if you want, all sections can be loaded ). It supports symbols in section names and allows loading of additional sections. Zend_Config_Ini also considers the "point" level Separator in the parameter, which allows a group of related parameters. In our config. ini, the host, user name, password, and database name parameters are all under the group $ config-> db-> config. We will load the configuration file (index. php) Relevant part of zf-tutorial/index. php In the Startup File. We load the class we will use (Zend_Config_Ini and Zend_Registry) and load the 'general 'section in application/config. ini to the $ config object. Finally, we allocate the $ config object to the registry so that it can be retrieved and used anywhere in the program. Note: In this tutorial, we do not actually need to store $ config in the registry, but in the "real" project, you may have a lot of configuration information in the INI file, it is not just a database. Similarly, you need to know that if you are not careful, the Registry is a little bit like a global variable and leads to dependency between objects, but they do not depend on each other. Create Zend_Db_Table to use Zend_Db_Table, we need to tell it the configuration information of the database we just loaded. We need to create a Zend_Db instance and use the static function Zend_Db_Table: setdefaadapter adapter () to register it. Emphasize that we can complete it in the Startup File (in black): Relevant part of zf-tutorial/index. I plan to use MySQL for TABLE creation in php, so the SQL statement for TABLE creation is as follows: QUOTE: CREATE TABLE album (id int (11) NOT NULL auto_increment, artist varchar (100) NOT NULL, title varchar (100) not null, primary key (id) runs this statement on the MySQL client, such as phpMyAdmin or standard MySQL command line client. Insert test Albums we also need to insert some records into the table to test the retrieval function on the home page. I plan to use Amazon. co. the first two items in the "Hot100" CD in uk: QUOTE: insert into album (artist, title) VALUES ('James mornel', 'undiscovered'), ('snow patrol ', 'Eyes open'); the Model Zend_Db_Table is an abstract class, so we derive a class dedicated to album management. It doesn't matter how to name a class, but it makes sense to use the same name for the class name and table name. In this way, the class name is Album because the table name is album. To tell Zend_Db_Table the name of the table to be managed, we must set the protection attribute $ _ name as the table name. In addition, Zend_Db_Table assumes that the table has a primary key named id, which can automatically increase. This field can be changed if needed. We will save our Album table to the model Directory: zf-tutorial/application/models/Album. php. Isn't it very complicated ?! We are fortunate to have very simple requirements and Zend_Db_Table provides all the functions we need. However, if you need special functions to manage your models, you can put them in this class. In general, the attached function will be an additional "find" type method, which makes the set of precise data you are looking for valid. You can also tell Zend_Db_Table about the relevant table and it can also obtain relevant data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.