开发者

Datamapper (Overzealous Edition), Codeigniter and Has Many Clarification

Firstly, I would like to just it out there that I am an ORM noob. I've never used an ORM in my life and have reached a point in my day-to-day developing that I need to do some crazy advanced relationships that I believe Datamapper Overzealous Edition can help me with as I am using Codeigniter.

In my database I have the following tables;

users

projects

clients

tasks

Here is my desired relationships between the tables;

  • A user can belong to many projects.
  • A project can have multiple tasks, but can only have one client.
  • A client can have many users and can have many projects
  • A task can only have one project

I have attempted to set-up my models in the models directory as it says in the documentation the model name without the s on the end, so for users I have a user.php model and so on.

I know the documentation is great, but I just can't seem to understand it properly even though it is obviously very easy. I know you instantiate the model by going for example $u = new User(); inside of your controller, but my question is setting up the relationships inside of the models.

How do I set out my models to have the above relationships so for example when I fetch a task I ca开发者_运维技巧n see what project it belongs to and a whole heap of information from its associated database tables.

I noticed that in the documentation you use the following inside of the projects model which should tell it that it can have more than one task for a project; var $has_many = array('task')

Is that all there is to it? Is it as simple as defining the $has_many and $has_one variables and putting in the associated model name in the array?


I've never used this particular ORM but I do use Doctrine. If the one you are using works in much the same way then the simple answer to your question is - yes! With Doctrine you set up all relationships in the model classes. The ORM will then manage it all for you. So, for example, if you instantiate a new task object... $task = new Task(); Then you can access the relationship with the Project table by simply writing $task->Project;

I must stress that the above code is not written for datamapper so might not work as is, but I hope it clears things up for you. It sounds like you do understand the documentation but just don't believe it!!!!


That is all you have to do as long as your database structure fits the layout expected by Datamapper Overzealous.

I've been using this ORM in a project I've been building and for the most part, it's been extremely helpful and time saving.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜