1

I wrote a sql script and in it I created a table ; Now I need to know ,how I can execute this script? (with which codes?) And I have another question : where? where I must write this codes?(which folder in zend project?) if it is possible for you please explain with an example.thanks

1 Answer 1

1

Creating tables in the database

Zend Framework is not supposed to be the one creating the tables, thus, my suggestion is to run those scripts in other environment. The fastest one is, probably, the very own SQL shell, but you can use another software such as MySQLWorkbench if you are using MySQL.

Once the tables are created, the access to the tables is made this way:

Introduction

When you are using Zend Framework, you are making use of the MVC pattern. I suggest you to read what is that: Wikipedia MVC

If you read the Wikipedia link, you probably know now that the acess to the database is going to be made by the model.

Thus, if you followed the recommended project structure that Zend provides you will have a models folder under your application folder. There, you are supposed to implement the classes that will make access to the DB.

But well... you now know where to locate those classes but you will ask me: how? It's easy if you know where to search. ZF provides an abstract class called Zend_Db_Table_Abstract that has all the methods that will make your life easier talking about interaction with your database's tables. This is the class that your classes should implement.

Example

Let's suppose you've got a page in your website in which you want to show to the user a list of products of your local store. You have a table in your database called "products" in which you have all the useful information such us name, price and availability.

You will have a controller with an action called indexAction() or listAction() this action is prepared to send the data to your view and will look like:

class Store_ProductsController extends Zend_Controller_Action {
    public function indexAction(){
        //TODO: Get data from the DataBase into $products variable
        $this->view->products = $products;
    }
}

And your view file will that that products variable and do sutff with it.

But now comes the magic, you will have a class that will access to the database as I've said, it'll be like:

class Model_Store_Products extends Zend_Db_Table_Abstract{

    protected $_name = 'products';

    public function getAllProducts(){
        $select = $this->$select()
                       ->from(array('P'=>$this->_name), 
                              array('id', 'name', 'price', availability));
        $productsArray = $this->fetchAll($select);
        return $productsArray;
    }
}

And ta-da, you have your array of products ready to be used by the controller:

class Store_ProductsController extends Zend_Controller_Action {
    public function indexAction(){
        $model = new Model_Store_Products();
        $products = $model->getAllProducts();
        $this->view->products = $products;
    }
}

It can be said that, since fetchAll is public function, and our select does basically nothing but set which columns do we want (it doesn't even have a where clause), in this case, it would be easier to call the fetchAll directly from the controller with no where and it will recover the whole table (all columns):

class Store_ProductsController extends Zend_Controller_Action {
    public function indexAction(){
        $model = new Model_Store_Products();
        $products = $model->fetchAll();
        $this->view->products = $products;
    }
}

Thus, our function in the model is not even needed.

This is the basic information of how to access to the database using Zend Framework. Further information of how to create the Zend_Db_Table_Select object can be found here.

I hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but my problem isn't really it ;my problem is this matter :I like write the codes for creating DB and Tables in independent script and then execute them so in your point's of view what is better ? i write an action for it and run it? or you suggest me another solution .
Zend is not thought to be used to creates tables in a Database. Thus, I really suggest you to run those scripts once in another software, maybe just MySQL shell? Once they are created, you can access to them in the way I said above. I'll edit the answer to add this information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.