gpt4 book ai didi

php - 在 CakePHP 中将数据保存到数据库中的 2 个表中

转载 作者:行者123 更新时间:2023-11-29 23:59:29 28 4
gpt4 key购买 nike

我创建了一个简单的示例项目,用于测试如何从 CakePHP 中的一个 Controller 将数据添加到 2 个表,我有一个名为 StudentUsers 的表管理员用户组。我需要将学生添加到数据库表中,并将学生用户名和密码添加到用户表中。以下是表的 SQL。

CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_group_id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`username` varchar(40) DEFAULT NULL,
`password` varchar(40) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;


CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_group_id` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `user_groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

CREATE TABLE `admins` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_group_id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`post` varchar(255) DEFAULT NULL,
`username` varchar(40) DEFAULT NULL,
`password` varchar(40) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

我创建了以下函数来将数据添加到 Students 表和 Users 表中。

public function add() {
if ($this->request->is('post')) {

$userGrp = $this->request->data['Student']['user_group_id'];
$username = $this->request->data['Student']['username'];
$pass = $this->request->data['Student']['password'];

$this->Student->create();
if ($this->Student->save($this->request->data)) {
$this->request->data['User']['user_group_id'] = $userGrp;
$this->request->data['User']['username'] = $username;
$this->request->data['User']['password'] = $pass;
if ($this->Student->User->save($this->request->data)) {
$this->Session->setFlash(__('The student has been saved. Both in to Student and User'));
return $this->redirect(array('action' => 'index'));
}

} else {
$this->Session->setFlash(__('The student could not be saved. Please, try again.'));
}
}
$userGroups = $this->Student->UserGroup->find('list');
$this->set(compact('userGroups'));
}

学生的模型如下。

<?php
App::uses('AppModel', 'Model');

class Student extends AppModel {

public $displayField = 'name';

public $belongsTo = array(
'UserGroup' => array(
'className' => 'UserGroup',
'foreignKey' => 'user_group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

}

当我将学生详细信息添加到数据库中时,显示以下错误。数据发送到 Student 表,但不发送到 Users 表

Error: Call to a member function save() on a non-object
File: D:\wamp\www\MultiTables\app\Controller\StudentsController.php

我在这里做错了什么?请帮忙。

最佳答案

IMO,学生和管理员都应该位于一个名为“用户”的表中。您命名为“users”的表实际上是一个连接表。如果您遵循约定,那么这是一个不正确的连接表名称。

然后你就会有这些模型:

  • 用户(属于用户组)
  • 用户组(habtm 用户通过 UserGroupUsers)
  • UserGroupUsers(属于用户和属于用户组)

如果像这样链接,那么这些输入会将数据保存到所有三个表中:

$this->Form->input('User.username')

$this->Form->input('User.password')

$this->Form->input('UserGroup.id', array('value' => 15)

您应该查看 CakePHP 的“烘焙”功能。如果你给它一个正确构建的数据库,它将帮助你创建正确链接的模型。

看来您当前正在尝试做的是:

  • 学生(属于用户)
  • 用户组(habtm 学生通过用户,habtm 管理员通过用户)
  • 用户(属于学生、属于用户、属于管理员)

$this->Form->input('学生.用户名')

$this->Form->input('User.password')

$this->Form->input('UserGroup.id', array('value' => 15)

关于php - 在 CakePHP 中将数据保存到数据库中的 2 个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25145923/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com