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?
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
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
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.
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.
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?
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
phone_number (phone_id, ...)
company (company_id, ...)
person (person_id, ...)
company_phone (company_id, phone_id)
person_phone (person_id, phone_id)
- findByCompany($companyId) [does a join on phone_number and company_phone]
- findByPerson($personId) [does a join on phone_number and person_phone]
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.
> 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
>> if the second table just contains, for instance, 2 columns (foreign
>> 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] >>