Quantcast

Question regarding models

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Question regarding models

Koen-14
I've been looking at the new Quick Start in the ZF manual, particularly the model part. For a new project I'm trying to create my models in the same way (including a data mapper, etc).

Unfortunately I don't know how to handle the relationships. When requesting data from your model you often want to include some fields in related tables (using joins), but I don't see a way doing that using the pattern as described in the quick start guide. Can someone give me some tips regarding this?

Thanks in advance!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding models

padraicb
Hi Koen,

I just published a chapter on implementing the Model you may want to look into to see how it fits. The book is free to read ;).
http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors

I think the main point I think is that you are thinking in terms of the database. Why use a join? You can use two distinct SQL queries and then aggregate these as objects. In the book, I used a simple example of blog entries needing authors - so the entry object contains an author object.

Joins are great, no mistake, but they are also premature optimisation. You can offset the cost of two queries using caching and lazy loading to avoid database access in the first place.

Hope this helps!
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: Koen <[hidden email]>
To: [hidden email]
Sent: Friday, August 21, 2009 11:36:35 AM
Subject: [fw-general] Question regarding models

I've been looking at the new Quick Start in the ZF manual, particularly the model part. For a new project I'm trying to create my models in the same way (including a data mapper, etc).

Unfortunately I don't know how to handle the relationships. When requesting data from your model you often want to include some fields in related tables (using joins), but I don't see a way doing that using the pattern as described in the quick start guide. Can someone give me some tips regarding this?

Thanks in advance!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding models

Diego Potapczuk
Hi Pádraic Brady,

Thansk for improving the documentation about how to proper implement an abstraction layer between Data Acess and the Application, i myself was looking like crazy for something like this, because im starting to develop an application and was designing its architecture, normally in ZF you see just View > Controller > DBTable, but now the updated the QuickStart tutorial showing the use of a proper Model and a Mapper, but that do not really show how it is used with ZF, and what you lose when you implement yourself this abstraction and the constraints you will have with the Framework.

I hope soon, if it is a better design approach, like i think it is, the ZF will get this as it default architecture.


Cheers,

::: Diego Potapczuk



On Fri, Aug 21, 2009 at 8:40 AM, Pádraic Brady <[hidden email]> wrote:
Hi Koen,

I just published a chapter on implementing the Model you may want to look into to see how it fits. The book is free to read ;).
http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors

I think the main point I think is that you are thinking in terms of the database. Why use a join? You can use two distinct SQL queries and then aggregate these as objects. In the book, I used a simple example of blog entries needing authors - so the entry object contains an author object.

Joins are great, no mistake, but they are also premature optimisation. You can offset the cost of two queries using caching and lazy loading to avoid database access in the first place.

Hope this helps!
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: Koen <[hidden email]>
To: [hidden email]
Sent: Friday, August 21, 2009 11:36:35 AM
Subject: [fw-general] Question regarding models

I've been looking at the new Quick Start in the ZF manual, particularly the model part. For a new project I'm trying to create my models in the same way (including a data mapper, etc).

Unfortunately I don't know how to handle the relationships. When requesting data from your model you often want to include some fields in related tables (using joins), but I don't see a way doing that using the pattern as described in the quick start guide. Can someone give me some tips regarding this?

Thanks in advance!

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding models

Koen-14
In reply to this post by padraicb
Hi Pádraic,

Thanks for mentioning the URL, it's very interesting! Still I have some questions left.

As an example lets say I want to list 50 users. Every user has several linked fields in another table, like department, company, etc. If doing this the aggregated way, you will end up having it least 150 queries, while this can be done using 1 (!) query with a few joins.
Here's a quote from the URL you gave: "Database operations are expensive, often the most expensive operation"
So with this in mind, its hard for me to use the 150+ queries solution, although I see some big advantages. I know you can cache, but still...

Isn't there some 'in between' solution?

Regards,

Koen


2009/8/21 Pádraic Brady <[hidden email]>
Hi Koen,

I just published a chapter on implementing the Model you may want to look into to see how it fits. The book is free to read ;).
http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors

I think the main point I think is that you are thinking in terms of the database. Why use a join? You can use two distinct SQL queries and then aggregate these as objects. In the book, I used a simple example of blog entries needing authors - so the entry object contains an author object.

Joins are great, no mistake, but they are also premature optimisation. You can offset the cost of two queries using caching and lazy loading to avoid database access in the first place.

Hope this helps!
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: Koen <[hidden email]>
To: [hidden email]
Sent: Friday, August 21, 2009 11:36:35 AM
Subject: [fw-general] Question regarding models

I've been looking at the new Quick Start in the ZF manual, particularly the model part. For a new project I'm trying to create my models in the same way (including a data mapper, etc).

Unfortunately I don't know how to handle the relationships. When requesting data from your model you often want to include some fields in related tables (using joins), but I don't see a way doing that using the pattern as described in the quick start guide. Can someone give me some tips regarding this?

Thanks in advance!

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding models

mbneto
In reply to this post by Koen-14
Koen,

When you have to use join it is recommend to use an ORM such as doctrine that can handle this in a more complex way.

Matthew has posted something about it in http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects-1766001


On Fri, Aug 21, 2009 at 6:36 AM, Koen <[hidden email]> wrote:
I've been looking at the new Quick Start in the ZF manual, particularly the model part. For a new project I'm trying to create my models in the same way (including a data mapper, etc).

Unfortunately I don't know how to handle the relationships. When requesting data from your model you often want to include some fields in related tables (using joins), but I don't see a way doing that using the pattern as described in the quick start guide. Can someone give me some tips regarding this?

Thanks in advance!

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding models

Jurian Sluiman-4
Op Friday 21 August 2009 16:01:42 schreef mbneto:
> Koen,
>
> When you have to use join it is recommend to use an ORM such as doctrine
> that can handle this in a more complex way.
>
> Matthew has posted something about it in
> http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects-1766
>001
>
> On Fri, Aug 21, 2009 at 6:36 AM, Koen <[hidden email]> wrote:
> > I've been looking at the new Quick Start in the ZF manual, particularly
> > the model part. For a new project I'm trying to create my models in the
> > same way (including a data mapper, etc).
> >
> > Unfortunately I don't know how to handle the relationships. When
> > requesting data from your model you often want to include some fields in
> > related tables (using joins), but I don't see a way doing that using the
> > pattern as described in the quick start guide. Can someone give me some
> > tips regarding this?
> >
> > Thanks in advance!


It is still possible with solely Zend_ components. I have a model with a mapper and DbTable object. The mapper maps data to the model from the DbTable object, but is also capable of handling the relations with find findDependentRowset, findParentRow find ManyToManyRowset. More information at the doc website:
http://framework.zend.com/manual/en/zend.db.table.relationships.html#zend.db.table.relationships.fetching.parent


Regards, Jurian


--
Jurian Sluiman
Soflomo.com


signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question regarding models

padraicb
In reply to this post by Koen-14
Hi Koen,

Marry the query to a domain model method (i.e. add it to the Data Mapper), and filter it into domain objects before returning the collection. You'll need a Collection class, just like Zend_Db_*'s rowset classes for the root of the aggregate (User) but it needn't be more than a general Iterator implementation. You can also filter the query inputs (any primary keys) against a local Identity Map to prevent duplicates. This does have a slight disadvantage if the existing entities are not fully loaded as they can spark extra queries to load the reference entities through lazy loading.

The filter logic, is really just another form of mapping logic. I know I use a simplistic use case but the principles remain the same - you just need a more specific set of operations in a Mapper to make the JOIN translate across to the domain model. Ensuring the returned results are easy to pick apart by entity is the only requirement needed for the query.

So no, you do not need Doctrine to do this. ;).
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: Koen <[hidden email]>
To: Pádraic Brady <[hidden email]>
Cc: Zend Framework General <[hidden email]>
Sent: Friday, August 21, 2009 2:51:32 PM
Subject: Re: [fw-general] Question regarding models

Hi Pádraic,

Thanks for mentioning the URL, it's very interesting! Still I have some questions left.

As an example lets say I want to list 50 users. Every user has several linked fields in another table, like department, company, etc. If doing this the aggregated way, you will end up having it least 150 queries, while this can be done using 1 (!) query with a few joins.
Here's a quote from the URL you gave: "Database operations are expensive, often the most expensive operation"
So with this in mind, its hard for me to use the 150+ queries solution, although I see some big advantages. I know you can cache, but still...

Isn't there some 'in between' solution?

Regards,

Koen


2009/8/21 Pádraic Brady <[hidden email]>
Hi Koen,

I just published a chapter on implementing the Model you may want to look into to see how it fits. The book is free to read ;).
http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors

I think the main point I think is that you are thinking in terms of the database. Why use a join? You can use two distinct SQL queries and then aggregate these as objects. In the book, I used a simple example of blog entries needing authors - so the entry object contains an author object.

Joins are great, no mistake, but they are also premature optimisation. You can offset the cost of two queries using caching and lazy loading to avoid database access in the first place.

Hope this helps!
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: Koen <[hidden email]>
To: [hidden email]
Sent: Friday, August 21, 2009 11:36:35 AM
Subject: [fw-general] Question regarding models

I've been looking at the new Quick Start in the ZF manual, particularly the model part. For a new project I'm trying to create my models in the same way (including a data mapper, etc).

Unfortunately I don't know how to handle the relationships. When requesting data from your model you often want to include some fields in related tables (using joins), but I don't see a way doing that using the pattern as described in the quick start guide. Can someone give me some tips regarding this?

Thanks in advance!

Loading...