简体   繁体   English

CakePHP在同一Controller的非对象上调用成员函数create()

[英]CakePHP Call to a member function create() on a non-object in same Controller

Okay this is really odd I have the following test_sign_up action: 好的,这真的很奇怪,我有以下test_sign_up动作:

  public function test_sign_up(){

    if($this->request->is('post')){
        $signup_result = $this->request->data;

        $userData = array('User' => array(
            'username' =>$signup_result['User']['username'],
            'password'=> $signup_result['User']['password'],
            'group_id' => 2,
            'client_id' => 9999));

        $this->User->create();
    if ($this->User->saveAll($userData)) {
        $this->Session->setFlash(__('The user has been saved'));
        $this->redirect(array('action' => 'index'));
    } else {
        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
    }

    }

}

Now when I try to save a user I get: 现在,当我尝试保存用户时,我得到:

 Call to a member function create() on a non-object 

Note that this function is within my UsersController 请注意,此功能在我的UsersController

update 更新

If I do: 如果我做:

$this->loadModel('User');

It works no problem, But should that be necessary when i'm already in the controller bound to the User model? 它没有问题,但是当我已经在绑定到User模型的控制器中时,这是否有必要?

If you have a $uses array in your controller, then the controller will load these as models upon instantiation. 如果控制器中有$uses数组,则控制器将在实例化时将它们作为模型加载。 For example: 例如:

<?php
class UsersController extends AppController {

    public $uses = array(
        'Client',
        'Website',
        'Category',
        'Type',
        'WebsiteIssue'
    );
}

In the above, the Users controller will only load the five models specified. 在上面,用户控制器将加载指定的五个模型。 If you want the User model to be instantiated too (as it would by default if $uses was not specified) then simply add 'User' to your $uses array as well. 如果您也希望实例化User模型(默认情况下,如果未指定$uses ,则将被实例化),只需将'User'也添加到$uses数组中即可。

Use $this->Form->create() instead of $form->create() . 使用$this->Form->create()代替$form->create()

You must use $this before helper object. 您必须在辅助对象之前使用$this

Your index.php code should looks like: 您的index.php代码应如下所示:

echo $this->Form->create("Contact");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在cakephp控制器中的非对象上调用成员函数find() - Error: Call to a member function find() on a non-object in cakephp controller CakePhp错误:在非对象上调用成员函数create() - CakePhp Error: Call to a member function create() on a non-object CakePHP致命错误:在非对象(控制器)上调用成员函数create() - CakePHP Fatal error: Call to a member function create() on a non-object (Controller) CakePHP致命错误调用非对象上的成员函数schema() - CakePHP Fatal Error Call to a member function schema() on a non-object 调用非对象上的成员函数find()-cakePHP - Call to a member function find() on a non-object - cakePHP cakephp3测试组件在非对象上调用成员函数on() - cakephp3 test component Call to a member function on() on a non-object CakePHP:在非对象上调用成员函数setFlash() - CakePHP: Call to a member function setFlash() on a non-object CakePHP错误:在非对象上调用成员函数find() - CakePHP Error: Call to a member function find() on a non-object cakePHP:零星出现“在非对象上调用成员函数allow()” - cakePHP: sporadic occurrence of “Call to a member function allow() on a non-object” CakePHP-在非对象错误上调用成员函数allow() - CakePHP - Call to a member function allow() on a non-object error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM