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

发布于 2024-11-03 10:26:17 字数 2921 浏览 0 评论 0原文

我有 3 个表:users、estruturas 和 esttruturas_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
?>

我有什么问题? 谢谢

I have 3 tables: users, estruturas and estruturas_users.
Estruturas with this fields: id, name, user_id and hasAndBelongsToMany Users.

User model:

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 model:

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 controller:

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'));
}

In my edit view I have this error:
Notice (8): Uninitialized string offset: 0 [CORE/cake/libs/view/helper.php, line 863] with:

<?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
?>

What I have wrong?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷︶言冷语的世界 2024-11-10 10:26:17

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

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

I don't see the User field in your database. Maybe what you are looking for is to remove echo $this->Form->input('User'); and move it to the top of your form like this:

echo $this->Form->create('User');
翻身的咸鱼 2024-11-10 10:26:17

问题出在 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->alias 问题就解决了。

谢谢!

The problem is in the var $ belongsTo. I changed User to User1 and verify that the error is in the model. In my user.php I have virtual fields and the problem is with it. I have to change to:

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';

With $this->alias the problem is solve.

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文