zf2 - one/many-to-many relationships

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

zf2 - one/many-to-many relationships

tonystamp
What is the best way of performing one/many to many relationships using zf2?

For instance, i have a UserMapper that consumes a UserTableGateway to get the data from the user table. I also have a PostsMapper that consumes a PostTableGateway to get data from the posts table.

When i load a user, i would like the user object returned from the UserMapper to be populated with the posts made by that user. What is the best way to achieve this?

So i have two questions:
1) Assuming to create a Posts object involves the lookup of two (or more) database tables, how do i perform this from within the PostsMapper, using the PostTableGateway?
2) What is the preferred method to connect mappers together so they can request information from eachother? I was thinking of use the EventManager, so in the above example, the UserMapper would send an event requesting the posts of the particular user to be loaded by the PostsMapper. But then that would mean all my mappers would need to have an EventManager instance. Is that bad practice? Could the dependancies be handled with the ServiceManager?
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

tonystamp
Anyone got any pointers on the best way to achieve the above? Not many tutorials out there on the subject for zf2.
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

jeremiah
I like Doctrine 2 module.

Jeremiah

On May 11, 2013, at 7:04 PM, tonystamp <[hidden email]> wrote:

> Anyone got any pointers on the best way to achieve the above? Not many
> tutorials out there on the subject for zf2.
>
>
>
> --
> View this message in context: http://zend-framework-community.634137.n4.nabble.com/zf2-one-many-to-many-relationships-tp4659910p4659999.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

Marco Pivetta
@Jeremiah he is asking specifically for a Zend\Db solution...

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 12 May 2013 07:50, Jeremiah Small <[hidden email]> wrote:

> I like Doctrine 2 module.
>
> Jeremiah
>
> On May 11, 2013, at 7:04 PM, tonystamp <[hidden email]> wrote:
>
> > Anyone got any pointers on the best way to achieve the above? Not many
> > tutorials out there on the subject for zf2.
> >
> >
> >
> > --
> > View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/zf2-one-many-to-many-relationships-tp4659910p4659999.html
> > Sent from the Zend Framework mailing list archive at Nabble.com.
> >
> > --
> > List: [hidden email]
> > Info: http://framework.zend.com/archives
> > Unsubscribe: [hidden email]
> >
> >
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

graphicmist
This post has NOT been accepted by the mailing list yet.
In reply to this post by tonystamp
everyone must not be using doctrine. is there any solution for it guys?
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

tonystamp
yes, a non-doctrine, zf only solution
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

graphicmist
what i have tried till now is that... if you want a user object with post values populates, make a query on user table with join on post. you have to modify your user mapper to get the value for post. This works one way you can get the values but cant use it to insert the values. its a workaround i have used. i dont think its a good solution
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

graphicmist
In reply to this post by tonystamp
http://ralphschindler.com/2010/11

This might be a solution to your problem.
Or what i uderstood is you can create a new mapper and entity for this type of relationship and inserting value back in database means write a custom save method which will obviously involves two queries
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

tonystamp
Hi, i'm hesitant about having a mapper write a query to two seperate tables. Although i can see why it would be tempting to do so. For instance in a many-to-many relationship, the joining table is only likely to contain two columns (both being foreign keys, pointing to separate tables), and having a mapper to represent and manage that is a bit overkill.

Thanks for the link, although it is a zf1 solution, whereas this is a zf2 app.
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

tonystamp
In reply to this post by graphicmist
So is nobody developing applications like this anymore? If not i'd love to know the better way that everyone else is doing it.
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

latterd
In reply to this post by tonystamp
I just use mappers that contain simple methods like getProductCategories, on a Category mapper for example, this method will then just use the bridge table in its sql, I don't use a separate mapper. I do however make sure I implement the method in question in a mapper that matches the type I am returning. Take the method example I used earlier, you could argue this should go in Product mapper, but because I am returning Categories I place it in the Category mapper, the reverse could be true with a method like getCategoryProducts.

tonystamp <[hidden email]> wrote:

So is nobody developing applications like this anymore? If not i'd love to
know the better way that everyone else is doing it.



--
View this message in context: http://zend-framework-community.634137.n4.nabble.com/zf2-one-many-to-many-relationships-tp4659910p4660085.html
Sent from the Zend Framework mailing list archive at Nabble.com.

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

tonystamp
ok, so you have one mapper that communicates with two seperate tables (even if the second table just contains, for instance, 2 columns (foreign keys))? So i'm assuming that is two seperate TableGateway instances? Or you just have an adapter with custom sql queries?
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

Michael Gooden
Hey all,

Just to butt in here, this is how I do it:

*Hypothetical situation:*
*Entities:*
PhoneNumber
Company
Person
*Relations:*
Companies can have many PhoneNumbers
Persons can have many PhoneNumbers
The same PhoneNumber can belong to multiple Companies
The same PhoneNumber can belong to multiple Persons
The same PhoneNumber can belong to Company(s) and Person(s) at the same
time.
*Tables:*
phone_number (phone_id, ...)
company (company_id, ...)
person (person_id, ...)
company_phone (company_id, phone_id)
person_phone (person_id, phone_id)
*Mappers:
*
CompanyMapper
- find($id)
PersonMapper
- find($id)
PhoneMapper
- find($id)
- findByCompany($companyId) [does a join on phone_number and company_phone]
- findByPerson($personId) [does a join on phone_number and person_phone]

My notes:
I keep the findBy* in the PhoneMapper, because I do object hydration of the
Phone object, so it makes sense to do these queries here.

FYI, I use the AbstractDbMapper from ZfcBase. It works great for doing your
own object management.

Hope this helps.

Cheers,

Michael Gooden


On 22 May 2013 01:54, tonystamp <[hidden email]> wrote:

> ok, so you have one mapper that communicates with two seperate tables (even
> if the second table just contains, for instance, 2 columns (foreign keys))?
> So i'm assuming that is two seperate TableGateway instances? Or you just
> have an adapter with custom sql queries?
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/zf2-one-many-to-many-relationships-tp4659910p4660088.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

latterd
In reply to this post by tonystamp
Hi,

Yeah, I just use an adapter that I inject into mappers, I don't use Zend/Db
stuff. I find it much easier to maintain and debug this way.

From my point of view this is all that is needed. All my mappers do is
query the db and return the models, I use service classes to utilise the
mappers and perform model validation.



On 22 May 2013 00:54, tonystamp <[hidden email]> wrote:

> ok, so you have one mapper that communicates with two seperate tables (even
> if the second table just contains, for instance, 2 columns (foreign keys))?
> So i'm assuming that is two seperate TableGateway instances? Or you just
> have an adapter with custom sql queries?
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/zf2-one-many-to-many-relationships-tp4659910p4660088.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

latterd
FYI a code view of my approach. Drill down to see Mapper.php:
https://bitbucket.org/latterd/smodels/src/d86fb69051235b33ce4bfba3078b5c0336d86c03/vendor/Supa?at=master


On 22 May 2013 16:13, Daniel Latter <[hidden email]> wrote:

> Hi,
>
> Yeah, I just use an adapter that I inject into mappers, I don't use
> Zend/Db stuff. I find it much easier to maintain and debug this way.
>
> From my point of view this is all that is needed. All my mappers do is
> query the db and return the models, I use service classes to utilise the
> mappers and perform model validation.
>
>
>
> On 22 May 2013 00:54, tonystamp <[hidden email]> wrote:
>
>> ok, so you have one mapper that communicates with two seperate tables
>> (even
>> if the second table just contains, for instance, 2 columns (foreign
>> keys))?
>> So i'm assuming that is two seperate TableGateway instances? Or you just
>> have an adapter with custom sql queries?
>>
>>
>>
>> --
>> View this message in context:
>> http://zend-framework-community.634137.n4.nabble.com/zf2-one-many-to-many-relationships-tp4659910p4660088.html
>> Sent from the Zend Framework mailing list archive at Nabble.com.
>>
>> --
>> List: [hidden email]
>> Info: http://framework.zend.com/archives
>> Unsubscribe: [hidden email]
>>
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

hbgtuberose
This post has NOT been accepted by the mailing list yet.
Exactly what I was looking for, thanks for sharing.
Reply | Threaded
Open this post in threaded view
|

Re: zf2 - one/many-to-many relationships

Martin
In reply to this post by Marco Pivetta
buffer the first resultset and use a setter at the objects of the first
resultset to set the child result set





--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]