简体   繁体   English

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

[英]How to setup multiple HABTM relationships between two tables in CakePHP

I'm trying to make two HABTM relationships between these two tables in CAKEPHP 2.6, but it gives me a database error when I try to create a record. 我正在尝试在CAKEPHP 2.6中的两个表之间建立两个HABTM关系,但是当我尝试创建记录时,它给了我一个数据库错误。 I'm sure there must be some model settings or something, but I could not fix it. 我确定必须有一些模型设置或某些设置,但是我无法修复。 :( :(

Here's an img of DB structure: 这是数据库结构的img: http://i57.tinypic.com/k4wkyh.jpg

Model 模型

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' => '',
    )
);

} }

Controller ADD method 控制器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'));
}

Add view 新增检视

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

echo __('Add Solicitude'); 回声__('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')); 回声$ this-> Form-> end(__('Submit'));

Error obtained adding 添加时出错

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Citante.cliente_id' in 'field list' 错误:SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ Citante.cliente_id”

SQL Query: SELECT Citante . SQL查询:SELECT Citante cliente_id FROM conciliacion . cliente_id FROM conciliacion clientes AS Citante WHERE Citante . clientes Citante Citante solicitude_id = '1' solicitude_id ='1'

With CakePHP 2 you can define these relations like this in your Solicitude model: 使用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'
        )
    );

The only thing you have to two is to set two different relation names, here Citantes and Citados . 您唯一需要做的两件事就是设置两个不同的关系名称,在这里是CitantesCitados They are then the names that are used to fetch records. 它们就是用于获取记录的名称。

And with CakePHP 3, this would be done like this in your SolicitudesTable table: 使用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'
        ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM