如何在 CakePHP 中加载关联模型并保存相关数据?
我正在设置一个用户/组系统,允许用户发送加入组的请求。
我似乎无法加载关联的模型并添加行。真的很难抗拒使用 $this->query() 并完成它的冲动......但我正在尝试学习 Cake 约定并以正确的方式做事。
在我的组模型用于处理组加入请求的函数中:
$this->loadModel('GroupRequest');
$this->data = array('GroupRequest' =>
array('user_id' => $uid, 'group_id' => $gid));
if($this->GroupRequest->save($this->data)){ echo 'save success'; }
else { echo 'save fail'; }
以下是运行此命令时出现的错误:
警告 (512):SQL 错误:1064:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“loadModel”附近使用的正确语法 [CORE/cake/libs/model/datasources/dbo_source.php,第 684 行] 查询:loadModel
注意(8):未定义的属性:Group::$GroupRequest [APP/models/group.php,第 124 行] 致命错误:在第 124 行 /home/wxworksmat/sspot3/app/models/group.php 中的非对象上调用成员函数 save()
我也尝试使用 App::import:
App::import('Model','GroupRequest');
我没有收到任何 SQL以这种方式导入模型时出错,但仍然不起作用。我在 save() 或 create() 调用时收到以下错误:
致命错误:在第 124 行 /home/wxworksmat/sspot3/app/models/group.php 中的非对象上调用成员函数 save()
I am setting up a user/group system that allows users to send requests to join a group.
I can't seem to load the associated model and add a row. It's really really really difficult to resist the urge to just use $this->query() and be done with it... but I'm trying to learn the Cake conventions and do things the right way.
In my group model's function for handling group join requests:
$this->loadModel('GroupRequest');
$this->data = array('GroupRequest' =>
array('user_id' => $uid, 'group_id' => $gid));
if($this->GroupRequest->save($this->data)){ echo 'save success'; }
else { echo 'save fail'; }
Here are the errors I get when I run this:
Warning (512): SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'loadModel' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 684]
Query: loadModelNotice (8): Undefined property: Group::$GroupRequest [APP/models/group.php, line 124]
Fatal error: Call to a member function save() on a non-object in /home/wxworksmat/sspot3/app/models/group.php on line 124
I also tried using App::import:
App::import('Model','GroupRequest');
I don't get any SQL errors importing the Model this way, but it still doesn't work. I get the following error on the save() or create() call:
Fatal error: Call to a member function save() on a non-object in /home/wxworksmat/sspot3/app/models/group.php on line 124
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
据我了解,您正在尝试从模型内部执行此操作?
class GroupRequest extends AppModel {
public function associate($user, $group) {
$data["GroupRequest"] = array("user_id" => $user, "group_id" => $group);
$this->save($data);
}
}
然后在你的控制器中(假设group_requests_controller)
$this->GroupRequest->associate($user, $group);
如果你从另一个控制器调用它,你会首先加载模型
$this->loadModel("GroupRequests");
$this->GroupRequest->associate($user, $group);
但是,如果你在GroupRequests控制器中执行所有这些,你应该能够直接保存,而不需要为其创建单独的方法
public function add() {
$this->GroupRequest->create();
$this->GroupRequest->save($this->data); #for < 2.0
}
你的观点应该是这样的
<?php
echo $this->Form->create("GroupRequest");
echo $this->Form->input("user_id");
echo $this->Form->input("group_id");
echo $this->Form->end("Submit");
?>
我遇到的问题是我的模型顶部没有正确的模型关联声明。
现在我有:
group.php
var $hasMany = 'GroupRequest';
group_request.php
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public function new_request($user, $group) {
$data["GroupRequest"] = array("user_id" => $user, "group_id" => $group, 'status' => 'pending');
if($this->save($data)){ return true;} else {return false;}
}
现在因为一切都设置正确...我可以在我的 group.php 模型中执行此操作:
$this->GroupRequest->new_request($uid,$gid)
作为一个额外的好处,因为关联正在正确填充,当我在我的组或用户模型中执行 $this->find 时,现在所有相关的 GroupRequest 条目都会显示。奖金数据 FTW。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您混淆了控制器和模型方法,
它是控制器方法,只能在那里使用。
你应该总是
在其他地方使用
You are confusing controller and model methods
is a controller method and can only be used there.
You should always use
everywhere else