CakePHP:使用HABTM与同一模型的多个关系

时间:2011-04-25 21:25:39

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

我有3个表:users,estruturas和estruturas_users。 带有以下字段的Estruturas:id,name,user_id和hasAndBelongsToMany Users。

用户模型:

class User extends AppModel {
    var $name = 'User';
    var $hasAndBelongsToMany = array(
        'Estrutura' => array(
            'className' => 'Estrutura',
            'joinTable' => 'estruturas_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'estrutura_id',
            'unique' => true
        )
    );
}

Estruturas模型:

class Estrutura extends AppModel {
    var $name = 'Estrutura';

    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );


    var $hasAndBelongsToMany = array(
        'User' => array(
            'className' => 'User',
            'joinTable' => 'estruturas_users',
            'foreignKey' => 'estrutura_id',
            'associationForeignKey' => 'user_id',
            'unique' => true
        )
    );
}

Estruturas控制器:

function edit($id = null) {
    $this->layout = 'admin';
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid estrutura', true));
        $this->redirect(array('action' => 'admin'));
    }
    if (!empty($this->data)) {
        if ($this->Estrutura->save($this->data)) {
            $this->Session->setFlash(__('The estrutura has been saved', true));
            $this->redirect(array('action' => 'admin'));
        } else {
            $this->Session->setFlash(__('The estrutura could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->data = $this->Estrutura->read(null, $id);
    }
    $users = $this->Estrutura->User->find('list');
    $anos = $this->Estrutura->Ano->find('list');
    $estruturatipos = $this->Estrutura->Estruturatipo->find('list');
    $ciclos = $this->Estrutura->Ciclo->find('list');
    $users = $this->Estrutura->User->find('list');
    $this->set(compact('users', 'anos', 'estruturatipos', 'ciclos', 'users'));
}

在我的编辑视图中,我遇到此错误: 注意(8):未初始化的字符串偏移量:0 [CORE / cake / libs / view / helper.php,第863行]:

<?php
    echo $this->Form->input('id');
    echo $this->Form->input('nome');
    echo $this->Form->input('nome_completo');
    echo $this->Form->input('user_id');
    echo $this->Form->input('ano_id');
    echo $this->Form->input('estruturatipo_id');
    echo $this->Form->input('ciclo_id');
    echo $this->Form->input('User'); //the error is here
?>

我错了什么? 感谢

2 个答案:

答案 0 :(得分:0)

我没有在您的数据库中看到用户字段。也许您正在寻找的是删除echo $this->Form->input('User');并将其移动到表单的顶部,如下所示:

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

答案 1 :(得分:0)

问题出在var $ belongsTo中。我将User更改为User1并验证错误是否在模型中。在我的user.php中,我有虚拟字段,问题在于它。我必须改为:

function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->virtualFields['nomeUser'] = sprintf('CONCAT(%s.nome, " ", %s.apelido)', $this->alias, $this->alias);
}

var $displayField = 'nomeUser';

使用$ this-&gt;别名,问题就解决了。

谢谢!