简体   繁体   English

Dataigner(Overzealous Edition),Codeigniter,并且有很多说明

[英]Datamapper (Overzealous Edition), Codeigniter and Has Many Clarification

Firstly, I would like to just it out there that I am an ORM noob. 首先,我想证明我是ORM新手。 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. 我一生中从未使用过ORM,并且在日常开发中已经达到了一个点,我需要做一些疯狂的高级关系,我相信Datamapper Overzealous Edition在使用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. 我已经尝试在models目录中设置我的模型,就像在文档中说的那样,模型名末尾没有s,因此对于用户来说,我有一个user.php模型,依此类推。

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(); 我知道您通过例如$ 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 can 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? 就像定义$ has_many和$ has_one变量并将关联的模型名称放入数组中一样简单吗?

I've never used this particular ORM but I do use Doctrine. 我从未使用过这种特定的ORM,但确实使用了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. 使用Doctrine可以在模型类中设置所有关系。 The ORM will then manage it all for you. 然后,ORM将为您管理所有内容。 So, for example, if you instantiate a new task object... $task = new Task(); 因此,例如,如果您实例化一个新的任务对象... $ task = new Task(); Then you can access the relationship with the Project table by simply writing $task->Project; 然后,您只需编写$ task-> Project即可访问与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. 我必须强调,上面的代码不是为datamapper编写的,因此可能无法正常工作,但我希望它可以为您解决问题。 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. 只要数据库结构适合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. 我在一个正在构建的项目中一直使用这个ORM,并且在大多数情况下,它非常有用且节省时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM