My question is mostly about OOP and ORM design. I'm creating models in Zend for Users and Groups, and using mappers for the db (and also using dependency injection). I have a relational table, user_groups
, for linking. I'm trying to figure out the best way to go about getting and setting a user's groups, or a group's users. I am currently looking at 3 options, let me know if I'm forgetting something:
- Take care of joining db tables in mapper so user's groups look like part of the User model
- Add a UserGroupsMapper: create functions like user->addToGroup($group)
- Add a UserGroupsMapper and UserGroup model: create functions like userGroup->add($user, $group)
Thoughts abo开发者_运维百科ut option 1:
Is there supposed to be a one-to-one relationship between models and db tables, or can one model represent multiple tables? If so, when would it not be ok? Another example: If I have a color
table and a color_type
table (with things like "warm" or "cool" as rows), does Color contain the colorType as an attribute/instance variable? Or does it reference ColorType as an object by means of color_type_id
, or does it have an actual ColorType object as an ivar?
Thoughts about option 2:
For a function like user->addToGroup($group), would it call the UserMapper method, which then calls the method in the UserGroupsMapper, or would it call the method in the UserGroupsMapper directly? From my understanding, the mapper is supposed to abstract the model from the data, but what does that mean as far as relationships between models? Are the models supposed to know how they are linked, or just the mappers?
Thoughts about option 3:
Adding a UserGroup model creates the possibility of accessing that model in a controller. Should the Group controller know the model relationships, like userGroup objects, or should it only be able to do things like tell a group to add a user?
I hope the question is clear enough. I know it is pretty involved, but I haven't been able to find a good answer yet.
Usually your models are just simple objects that represent data in a table and allow you to get and set values. The mapper is where you would have all of your database access and logic. Typically they are one to one relationships with your data, but they don't have to be. As long as you know how to manage what objects where.
You would pass the model to the mapper function and it would get its information from there.
So I would put your addUserToGroup in one of the mappers, not in the model itself.
Generally your model should only have getters and setters, and maybe a helper function or two for manipulating data in the model.
If you have a query that needs to join data from multiple tables and may comprise one or more models, or part of one model and part of another, you can just return an array rather than a model object, but this would still be done in the mapper. I have instances of both of these cases in some of my applications.
Also, I extend my models from a base class that gives me an easy way to take an array of data and set the properties of the model from the array. Also they contain a toArray method so I can easily turn a model into an array if need be.
(Quick answer)
I might do something like the following:
$oMyGroup->addUser( $oMyUser );
You're adding the user to the group, regardless of what strategy you would use for persisting/materializing the objects (datamapper, activerecord, etc) in some type of data "storage": database, serialized objects in a text file, in-memory storage, etc.
精彩评论