A
A
alesto2011-07-19 12:07:57
Zend Framework
alesto, 2011-07-19 12:07:57

Getting objects with seters and getters from mysql query in zend framework?

When executing a Sql query in Zend_Db, the result is returned as an array of associative arrays. I want to make the result returned as an array of objects with seters and geters (Like in Magento, for example, I know that there is a collection implementation, a simple array of objects would be enough for me).
Only the following comes to mind:
1) We create the loadFromArray method on the object
2) We execute our request
3) We create objects in the loop, load them from the array and add them to the new array.
4) Return the resulting array.
But I'm not sure if this is correct. I'd love to know how to do it right.
I'll ask one more question here, so as not to produce topics. If I need only 1 parameter of an object, then in order not to load the entire object, do I need to create a helper or add a method that returns this value without loading the entire object?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2011-07-19
@alesto

Zend_Db has Zend_Db::FETCH_CLASS That is
, you can use pdo
$stmt->setFetchMode(Zend_Db::FETCH_CLASS|Zend_Db::FETCH_PROPS_LATE, 'ClassName');
And objects of class ClassName will be returned

A
Anatoly, 2011-07-19
@taliban

I did this:
1. create the desired object (it will be a collection), inherit from arrayIterator (ideally, I used all the interfaces manually, so it turns out to be more flexible, but this option is easier), and override the getItem and setItem methods (it seems that in the manual they yes)
2. instead of your 3 points, we give the entire array to our collection
3. $collection[x] returns an object
As a result, we get rid of the cycle, and we get a good collection. I also made it more universal, gave the name of the class that I want to get in the end along with the array.
If it is not clear, ask =) I will explain in more detail with examples.

S
SeriousDron, 2011-07-19
@SeriousDron

I think you need Zend_Db_Table, you will get Zend_db_Table_Rowset (which is SeekableIterator, Countable, ArrayAccess, so almost an array) of Zend_Db_Table_Row objects.
If you need some special objects, make your successor from Zend_Db_Table_Row. By default, there will be no explicit getters and setters, but everything will work through __get and __set, which is basically the same. Nobody bothers you to make explicit ones either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question