模型关联和数据建模
我正在为一家美术馆开发一个 Web 应用程序,我想检查我是否已正确设置数据模型。
我有一个 people
表和一个 artists
表。有些人
是艺术家
,有些则不是。
我还有一个 products
表,每个 product
都属于一位 artist
。
下面是表格的简化版本:
products
********
id
title
artist_id
artists
*******
id
profile
rating
person_id
people
******
id
first_name
last_name
在 Product
模型上执行 find()
方法时,我需要能够检索艺术家的姓名。
我是否正确设置了数据模型?如果是,检索艺术家姓名而不获取大量不需要的数据的最佳方法是什么?
I am developing a web app for an art gallery, and I want to check I have set up the data model correctly.
I have a people
table and an artists
table. Some of the people
are artists
and some are not.
I also have a products
table and each product
belongs to one artist
.
Below is a simplified version of the tables:
products
********
id
title
artist_id
artists
*******
id
profile
rating
person_id
people
******
id
first_name
last_name
I need to be able to retrieve the name of the artist when performing the find()
method on the Product
model.
Have I set up the data model correctly and if so what is the best way to retrieve the artist's name without getting lots of unwanted data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用 CakePHP 的 bindModel & unbindModel 来建立不依赖于 Cake 自动魔法的关系。
由于您有兴趣在 Product 模型上进行查找,因此我将列出一个可以从 Products 控制器调用的示例:
这里发生了什么?
首先,我们解除Product模型与Artist模型的关系。
其次,我们通过显式定义关系并禁用 Cake 的自动 forigenKey 连接来绑定 Artist 和 Person 模型。
最后,我们对 Product 模型进行“first”查找,并且仅请求“first_name”和“first_name”。 人的“姓氏”。
It's possible to use CakePHP's bindModel & unbindModel to build up relations that don't rely on Cake's automagic goodness.
Since you were interested in doing the find on the Product model, I'll set out an example that can be called from the Products controller:
What's happening here?
Firstly, we unbind the Product model's relationship with the Artist model.
Secondly, we bind the Artist and Person models by explicitly defining the relationships and disabling Cake's automatic forigenKey connections.
Lastly, we do a 'first' find on the Product model and only request the 'first_name' & 'last_name' of the Person.
每个“产品”都属于一个“艺术家”,每个“艺术家”都有AndBelongsToMany“产品”
您还需要下表:
http://book.cakephp.org/ #!/view/1044/hasAndBelongsToMany-HABTM
Each 'Product' belongs to an 'Artist' and each 'Artist' hasAndBelongsToMany 'Product'
You will also need the following table:
http://book.cakephp.org/#!/view/1044/hasAndBelongsToMany-HABTM