如何在CakePHP中的两个表之间设置多个HABTM关系

时间:2015-06-10 21:48:29

标签: php cakephp has-and-belongs-to-many

我正在尝试在CAKEPHP 2.6中的这两个表之间建立两个HABTM关系,但是当我尝试创建记录时,它会给我一个数据库错误。我确定必须有一些模型设置或其他东西,但我无法解决它。 :(

这是一个简单的数据库结构: http://i57.tinypic.com/k4wkyh.jpg

模型

class Solicitude extends AppModel {
public $hasAndBelongsToMany = array(
    'Citante' => array(
        'className' => 'Cliente',
        'joinTable' => 'citantes',
        'foreignKey' => 'solicitude_id',
        'associationForeignKey' => 'cliente_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    ),
    'Citado' => array(
        'className' => 'Cliente',
        'joinTable' => 'citados',
        'foreignKey' => 'solicitude_id',
        'associationForeignKey' => 'cliente_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);

}

控制器ADD方法

public function add() {
    if ($this->request->is('post')) {
        $this->Solicitude->create();
        if ($this->Solicitude->save($this->request->data)) {
            $this->Session->setFlash(__('The solicitude has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The solicitude could not be saved. Please, try again.'));
        }
    }
    $citantes = $this->Solicitude->Citante->find('list');
    $citados = $this->Solicitude->Citado->find('list');
    $this->set(compact('citantes', 'citados'));
}

添加视图

echo $this->Form->create('Solicitude'); 

echo __('Add Solicitude');

    echo $this->Form->input('radicado');
    echo $this->Form->input('fecha');
    echo $this->Form->input('ccsede_id');
    echo $this->Form->input('ccusuario_id');
    echo $this->Form->input('consulta_id');
    echo $this->Form->input('peticiones');
    echo $this->Form->input('area_id');
    echo $this->Form->input('tipo_clase_id');
    echo $this->Form->input('Citante');
    echo $this->Form->input('Citado');

echo $ this-> Form-> end(__('Submit'));

添加

时出错

错误:SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'Citante.cliente_id'

SQL查询:SELECT Citantecliente_id FROM conciliacionclientes AS Citante WHERE Citantesolicitude_id =' 1'

1 个答案:

答案 0 :(得分:0)

使用CakePHP 2,您可以在Solicitude模型中定义这样的关系:

var $hasAndBelongsToMany = array(

        'Citantes' => array(
            'className' => 'Client',
            'joinTable' => 'citantes',
            'foreignKey' => 'solicitude_id',
            'associationForeignKey' => 'cliente_id'
        ),
        'Citados' => array(
            'className' => 'Client',
            'joinTable' => 'citados',
            'foreignKey' => 'solicitude_id',
            'associationForeignKey' => 'cliente_id'
        )
    );

你唯一要做的就是设置两个不同的关系名称,CitantesCitados。它们就是用于获取记录的名称。

使用CakePHP 3,这将在SolicitudesTable表中完成:

$this->belongsToMany('Citantes', [
            'className' => 'Client',
            'joinTable' => 'citantes',
            'foreignKey' => 'solicitude_id',
            'targetForeignKey' => 'cliente_id'
        ]);

$this->belongsToMany('Citados', [
            'className' => 'Client',
            'joinTable' => 'citados',
            'foreignKey' => 'solicitude_id',
            'targetForeignKey' => 'cliente_id'
        ]);