如何在 CakePHP 中加载关联模型并保存相关数据?

发布于 12-20 01:54 字数 1079 浏览 3 评论 0原文

我正在设置一个用户/组系统,允许用户发送加入组的请求。

我似乎无法加载关联的模型并添加行。真的很难抗拒使用 $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: loadModel

Notice (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 技术交流群。

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

发布评论

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

评论(4

月下客2024-12-27 01:54:58

您混淆了控制器和模型方法,

$this->loadModel()

它是控制器方法,只能在那里使用。
你应该总是

$this->ModelName = ClassRegistry::init('ModelName');

在其他地方使用

You are confusing controller and model methods

$this->loadModel()

is a controller method and can only be used there.
You should always use

$this->ModelName = ClassRegistry::init('ModelName');

everywhere else

清风挽心2024-12-27 01:54:58

我可能是错的,如果我错了,请原谅,但看来你不太了解框架的概念。如果不给你一个完整的教程,很难回答你的问题。

也就是说,一切都依赖于模型关联。如果做得正确,事情就会变得容易。您应该阅读:

关联:

将 模型链接在一起模型正确链接后,您将能够非常轻松地保存主要模型以及相关模型。

保存相关模型数据(hasOne、hasMany、belongsTo)

I might be wrong, and please excuse me if I'm wrong, but it looks like you don't understand the concept of the framework very well. It is difficult to answer your question without giving you a complete tutorial.

This said, everything relies on model associations. If it's done correctly, things are getting easy. You should read:

Associations: Linking Models Together

Once you have your models correctly linked, you will be able to save the primary model, as well as the related model, very easily.

Saving Related Model Data (hasOne, hasMany, belongsTo)

∞觅青森が2024-12-27 01:54:58

据我了解,您正在尝试从模型内部执行此操作?

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");
?>

As I understand, you are trying to do this from inside a model?

class GroupRequest extends AppModel {

    public function associate($user, $group) {
        $data["GroupRequest"] = array("user_id" => $user, "group_id" => $group);
        $this->save($data);
    }

}

Then in your Controller (assuming group_requests_controller)

$this->GroupRequest->associate($user, $group);

If you're calling this from another controller you would loadModel first

$this->loadModel("GroupRequests");
$this->GroupRequest->associate($user, $group);

However, if you're doing all of this from within GroupRequests controller you should be able to save directly, without making a separate method for it

public function add() {
    $this->GroupRequest->create();
    $this->GroupRequest->save($this->data); #for < 2.0
}

Your view should be something like

<?php
    echo $this->Form->create("GroupRequest");
    echo $this->Form->input("user_id");
    echo $this->Form->input("group_id");
    echo $this->Form->end("Submit");
?>
没︽人懂的悲伤2024-12-27 01:54:58

我遇到的问题是我的模型顶部没有正确的模型关联声明。

现在我有:

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。

The problem I had was that I didn't have the correct model association declarations at the top of my model.

Now I have:

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

Now because everything is set up CORRECTLY... I can do this in my group.php model:

$this->GroupRequest->new_request($uid,$gid)

As an added bonus, because the assocations are populating properly, when I do $this->find in my group or user model, now all the related GroupRequest entries show up. Bonus data FTW.

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