[英]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.