Zend How the framework data interacts with object-oriented

Source: Internet
Author: User
Tags zend framework
Zend Framework Object-oriented frameworks

PHP Web site, using the framework is the Zend Framework, when fetching data is generally written SQL statements, using the Fetchall () method to obtain data, such as:
$db->fetchall ($sql);
Get an array collection, and then do processing, displayed on the page, which is missing a step, is to fill the data into the object, by the way the object is assigned to the page, I have not found the relevant examples on the Internet, the Zend Framework user manual also found
So I would like to ask you to give me a practical example, pure object-oriented, my project has written in the model layer, but has not been used, online to find specific code examples


Reply to discussion (solution)

No one's interested in this, folks?

"Fill in the data into the object" This sentence I did not read ...

Kinda agree with the upstairs story.

Why do you want to assign the data to the object and then pass the object to the page??

I remember. Call the query ($sql) method after getting the database adapter returns a PDO object to the database
You can print out not only the data that is queried from the database in this object object.
Also contains many table information about what field width and so on
It's no use.

It's usually a call to this object again. Fetchall () method to group

If you really want to pass the object to the page
I think I can do this.

You write a class yourself.
And then take Fetchall () out of the array as parameters
The construction method passed to this class
Assigning a value to a member variable in a class by constructing a method

This might be "filling the data into the object."
And then you pass this object to the page.

But I think even if you want to output objects in view
Or do you want to work with the object?




and using the framework how can not use the model it.
Do you want to use the traditional PHP method to manipulate the database?

In this case, do not have so much effort, write a function, the array to the class can be, what must be found in the framework, as long as the framework allows,

The answer is very strong, but not the answer I want, the reason is that the framework is easy to maintain in the future, extension, and object-oriented is a main idea of programming, if only to implement the function, I will not ask this question, I need is in the Zend, whether there is such a library, I can convert the data I've found into a class object! Continue to solve ....

So many rookie Ah, zf2 in so many ways of operation, Zend\db\sql,zend\db\tablegateway,zend\db\rowgateway, so do not

Write in DB

Class db_test{  function Fetchdata () {    $db =new Db ();    $res = $db->fetchall ("SELECT * from table");    return $res;  }}

Write it in the controller.
Class TestController extends  zend_controller_action{public  function testaction () {    $dbclass =new db_test ();    $data = $dbclass->fetchdata ();    $this->view->viewdata= $data;  }}

Write in the template
foreach ($this->viewdata as $key = + $value) {   echo $value;}

So many rookie Ah, zf2 in so many ways of operation, Zend\db\sql,zend\db\tablegateway,zend\db\rowgateway, so do not
Can you give me a little more detail, or a piece of code????

An array of objects?
Why don't you just tell me why you're doing this?

You say again, the scenario of doing this

You can't be tired of classes.

In fact, I do the website, has done, I now give the question is completely technical discussion

The model layer is to put some business logic
You're supposed to put the array on top of an object's properties,
Then the controller passes in an object in.
Then in the view just loop this property to get the array.

I am not able to upload the array to the page, my so-called object, refers to the database inside the table, its properties is the table field, I can not do is to the query out of the data assigned to the table field up?

$db->fetchall ($sql); it should be a collection of objects, using the ToArray () method to go to the array, you said to put the data on the object assignment to the page this really does not understand AH.

  • Related Article

    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.