模型关联和数据建模

发布于 2024-12-14 10:52:34 字数 535 浏览 0 评论 0原文

我正在为一家美术馆开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为人所爱 2024-12-21 10:52:34

可以使用 CakePHP 的 bindModel & unbindModel 来建立不依赖于 Cake 自动魔法的关系。

由于您有兴趣在 Product 模型上进行查找,因此我将列出一个可以从 Products 控制器调用的示例:

// unbind Artist
$this->Product->unbindModel(array(
  'belongsTo' => array('Artist')
));

// bind Artist & Person using explicit conditions
$this->Product->bindModel(array(
  'hasOne' => array(
    'Artist' => array(
      'foreignKey' => false,
      'conditions' => array('Artist.id = Product.artist_id')
    ),
    'Person' => array(
      'foreignKey' => false,
      'conditions' => array('Person.id = Artist.person_id')
    ),
  )
));

// return the person
$person = $this->Product->find('first', array(
  'conditions' => array(
    'Product.id' => 1 // or any other condition
  ),
  'fields' => array(
    'Person.first_name',
    'Person.last_name'
  )
));

这里发生了什么?

首先,我们解除Product模型与Artist模型的关系。

其次,我们通过显式定义关系并禁用 Cake 的自动 forigenKey 连接来绑定 ArtistPerson 模型。

最后,我们对 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:

// unbind Artist
$this->Product->unbindModel(array(
  'belongsTo' => array('Artist')
));

// bind Artist & Person using explicit conditions
$this->Product->bindModel(array(
  'hasOne' => array(
    'Artist' => array(
      'foreignKey' => false,
      'conditions' => array('Artist.id = Product.artist_id')
    ),
    'Person' => array(
      'foreignKey' => false,
      'conditions' => array('Person.id = Artist.person_id')
    ),
  )
));

// return the person
$person = $this->Product->find('first', array(
  'conditions' => array(
    'Product.id' => 1 // or any other condition
  ),
  'fields' => array(
    'Person.first_name',
    'Person.last_name'
  )
));

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.

黄昏下泛黄的笔记 2024-12-21 10:52:34

每个“产品”都属于一个“艺术家”,每个“艺术家”都有AndBelongsToMany“产品”
您还需要下表:

artists_products
****************
id
product_id
artist_id

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:

artists_products
****************
id
product_id
artist_id

http://book.cakephp.org/#!/view/1044/hasAndBelongsToMany-HABTM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文