Search code examples
zend-dbresultsetzend-framework2

ZF2 Mapping ResultSet of a Join-Statement into different Objects


when it comes down to increasing performance, reducing the amount of single SQL-Queries is one part of that.

Now let's assume a very basic example: i have a blog-table and a user-table. Each blog is referencing to a user by a given primary key.

A statement could be like

SELECT blog.title, blog.text, user.name FROM blog, user INNER JOIN on blog.user_id = user.id

Now my Blog-object i would like to have a $user-property which is a User-object

My Question: Are there inbuilt features within ZF2 to handle such a case? Or would i be needed to either manually map each field of the result into my objects?

Thanks in advance


Solution

  • No, there are no inbuilt features with ZF2 to do this - you should consider Doctrine 2 or Propel if you want that.

    With Zend\Db however, you could write such SQL statements within your mapper class and then use an ArraySerializable hydrator to populate the blog entity. The blog entity's populate() could then choose to create a user object with the user data that is passed to it.