CakePHP模型中的多对多关系问题

时间:2014-06-25 13:58:16

标签: cakephp cakephp-2.1 cakephp-2.3 cakephp-appmodel

我在从关系中获取所有数据时遇到问题。这是我的表格:

**Regions**    
id(pk)
regions_name

**Areas**    
id(pk)
areas_name
regions_id (fk)

**carDealer**    
id(pk)
dealer_name
areas_id(fk)

我认为通过这种设计,我应该可以像这样查询区域:

    $this->Region->recursive = 5;
    $getRegions = $this->Region->find('all');

获取所有Regions的数组,这些Region将有一个子数组,其中包含一个carDealers的子数组。

我的代码只会带回区域和区域数组,但不会带回carDealers。以下是我的模型的一些相关代码:

地区表

<?php
App::uses('AppModel', 'Model');    
class Region extends AppModel {    
    public $validate = array(    
    );

    public $belongsTo = array(    
    );
    public $hasMany = array(
        'Areas' => array(
            'className' => 'Areas',
            'foreignKey' => 'regions_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),   
    );
}

地区表:

<?php
App::uses('AppModel', 'Model');
class Area extends AppModel {
    public $validate = array(
    );    
    public $belongsTo = array(
        'Regions' => array(
            'className' => 'Regions',
            'foreignKey' => 'regions_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );
    public $hasMany = array(
        'carDealer' => array(
            'className' => 'carDealer',
            'foreignKey' => 'areas_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );
}

汽车经销商表:

<?php
App::uses('AppModel', 'Model');

class carDealer extends AppModel {
    public $validate = array(
    );

    public $belongsTo = array(
        'Areas' => array(
            'className' => 'Areas',
            'foreignKey' => 'areas_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );
    public $hasMany = array(
    );
}

我在模特关系中遗漏了什么?

感谢 杰森

1 个答案:

答案 0 :(得分:0)

默认情况下,hasMany,belongsTo或hasOne关系中的外键被识别为相关表的(单数)名称,后跟_id。

浏览http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions

所以你的外键应该是area_id,region_id

需要更改递归级别

 $this->Region->recursive = 1;
 $getRegions = $this->Region->find('all');

需要修改模型关系的className

它应该是&#39; Area&#39;,&#39; Region&#39;,&#39; CarDealer&#39;。

需要删除不必要的变量或数组选项

地区模型

<?php
 App::uses('AppModel', 'Model');    
class Region extends AppModel {    
   public $hasMany = array(
    'Area' => array(
        'className' => 'Area',
        'foreignKey' => 'region_id'
       ),   
);
}

区域模型:

<?php
  App::uses('AppModel', 'Model');
 class Area extends AppModel {

public $belongsTo = array(
    'Region' => array(
        'className' => 'Region',
        'foreignKey' => 'region_id'
    ),
);
public $hasMany = array(
    'CarDealer' => array(
        'className' => 'CarDealer',
        'foreignKey' => 'area_id'
    ),
);
 }

CarDealer型号:

<?php
 App::uses('AppModel', 'Model');

class CarDealer extends AppModel {
   public $belongsTo = array(
    'Area' => array(
        'className' => 'Area',
        'foreignKey' => 'area_id'
       ),
);
  }