CakePHP:使用 HABTM 与同一模型的多个关系
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在您的数据库中没有看到“用户”字段。也许您正在寻找的是删除 echo
$this->Form->input('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:问题出在 var$belongsTo 上。我将 User 更改为 User1 并验证错误是否存在于模型中。在我的 user.php 中,我有虚拟字段,问题就出在它身上。我必须更改为:
使用 $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:
With $this->alias the problem is solve.
Thanks!