Php 模型关联和数据建模

Php 模型关联和数据建模,php,model-view-controller,database-design,cakephp,data-modeling,Php,Model View Controller,Database Design,Cakephp,Data Modeling,我正在为一家画廊开发一个web应用程序,我想检查我是否正确设置了数据模型 我有一张people表和一张artists表。有些人是艺术家,有些人不是 我还有一个产品表,每个产品都属于一个艺术家 以下是表格的简化版本: products ******** id title artist_id artists ******* id profile rating person_id people ****** id first_name last_name 在产品模型上执行find()方法时,我需

我正在为一家画廊开发一个web应用程序,我想检查我是否正确设置了数据模型

我有一张
people
表和一张
artists
表。有些人是艺术家,有些人不是

我还有一个
产品
表,每个
产品
都属于一个
艺术家

以下是表格的简化版本:

products
********
id
title
artist_id

artists
*******
id
profile
rating
person_id

people
******
id
first_name
last_name
产品
模型上执行
find()
方法时,我需要能够检索艺术家的姓名


我是否正确设置了数据模型?如果正确,检索艺术家姓名而不获取大量不需要的数据的最佳方法是什么?

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

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

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

由于您对产品模型感兴趣,我将列出一个可以从产品控制器调用的示例:

// 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'
  )
));
这里发生了什么事

首先,我们解开了产品模型与艺术家模型的关系

其次,我们通过明确定义关系并禁用Cake的自动forigenKey连接来绑定艺术家人物模型


最后,我们在产品模型上进行“第一”查找,只请求

艺术家“hasMany”产品的“名字”和“姓氏”。不需要“HasandBelongtomany”关系,也不需要表格“people”表格的目的是什么?在你的应用程序中,非艺术家的角色是什么?