Cakephp 如何将数据表单控制器发送到其他模型?
我为寄存器创建视图并将数据发送到控制器Cakephp 如何将数据表单控制器发送到其他模型?,cakephp,cakephp-1.3,cakephp-model,Cakephp,Cakephp 1.3,Cakephp Model,我为寄存器创建视图并将数据发送到控制器 //app::import('Model','Myprofile'); Class MembersController extends AppController { var $name = 'Members'; var $helpers = array('Form', 'Session'); var $uses = array('Myprofile'); // function register() {
//app::import('Model','Myprofile');
Class MembersController extends AppController {
var $name = 'Members';
var $helpers = array('Form', 'Session');
var $uses = array('Myprofile'); //
function register() {
//$myprofile = new Myprofile();
if (!empty($this - > data)) {
$this - > Member - > create();
if ($this - > Member - > save(($this - > data['Member']['username']), ($this - > data['Member']['password'])) && $this - > Myprofile - > save(($this - > data['Myprofile']['name']), ($this - > data['MyProfile']['address']), ($this - > data['Myprofile']['phonenumber']))) {
$this - > redirect('index');
}
} else {
$this - > Session - > setFlash('failed');
}
}
我想将数据表单memberscontroller发送到Myprofile模型
我试着用
$use = array('Myprofile');
我得到
Undefined property: MembersController::$Member
当我使用
//app::import('Model','Myprofile');
//$myprofile = new Myprofile()
我得到
我不知道该不该走哪条路
还有另一种方法可以解决我的问题
感谢您的建议您必须将这两种型号都添加到
$uses
阵列中:
var $uses = array('Myprofile', 'Member');
您必须将两种型号都添加到
$uses
阵列中:
var $uses = array('Myprofile', 'Member');
$uses数组只允许您访问在其中指定的模型 如果您已经注释掉$uses,那么默认情况下您仍然可以访问$this->Member模型,因为您在Members控制器中 将另一个模型添加到$uses数组后,必须记住还包括初始模型 我还发现,在某些情况下,确保在执行类似操作时,首先指定默认模型是非常有用的
否则,您可能会从$this->paginate()之类的操作中获得意外的结果。使用$uses数组将只允许您访问在其中指定的模型 如果您已经注释掉$uses,那么默认情况下您仍然可以访问$this->Member模型,因为您在Members控制器中 将另一个模型添加到$uses数组后,必须记住还包括初始模型 我还发现,在某些情况下,确保在执行类似操作时,首先指定默认模型是非常有用的
否则,您可能会从$this->paginate()之类的操作中得到意外的结果。尝试使用
$this->loadModel('Myprofile')代码>在您的操作中
您确定您的型号名称为“Myprofile”吗
您甚至可以调试它以查看它是否返回true或false。这将帮助您找到模型是否已正确实例化。尝试使用$this->loadModel('Myprofile')代码>在您的操作中
您确定您的型号名称为“Myprofile”吗
您甚至可以调试它以查看它是否返回true或false。这将帮助您找到模型是否已正确实例化。如果使用$uses
数组,请确保该数组中也包含当前模型。否则,如果定义特定的$uses
数组,则默认情况下不会包含当前模型
如果MyProfile
与Member
相关,您可以通过
$this->Member->MyProfile; //depends on associations
您还可以使用App:import
App:import('Model', array('Myprofile')); //loads the class
$myProfile = new MyProfile();
// OR
MyProfile::staticMethod();
如果使用$uses
数组,请确保该数组中也包含当前型号。否则,如果定义特定的$uses
数组,则默认情况下不会包含当前模型
如果MyProfile
与Member
相关,您可以通过
$this->Member->MyProfile; //depends on associations
您还可以使用App:import
App:import('Model', array('Myprofile')); //loads the class
$myProfile = new MyProfile();
// OR
MyProfile::staticMethod();
您可以以正常方式调用相关模型的任何公共方法。例如
在Profile.php中
function someMethod( $param = null ) {
// some definition
}
来自MembersController.php
function register ( ) {
$this->Member->Profile->someMethod( $my_data_to_pass ); // if related
/* if not related
$this->loadModel('Profile');
$this->Profile->someMethod($my_data_to_pass);
*/
}
顺便说一句,若你们为了使URL更合理而遇到这种情况,请查看路由器(http://book.cakephp.org/2.0/en/core-utility-libraries/router.html)您可以以正常方式调用相关模型的任何公共方法。例如
在Profile.php中
function someMethod( $param = null ) {
// some definition
}
来自MembersController.php
function register ( ) {
$this->Member->Profile->someMethod( $my_data_to_pass ); // if related
/* if not related
$this->loadModel('Profile');
$this->Profile->someMethod($my_data_to_pass);
*/
}
顺便说一句,若你们为了使URL更合理而遇到这种情况,请查看路由器(http://book.cakephp.org/2.0/en/core-utility-libraries/router.html)้ำั嘿,我按照你们的建议做了,但我得到了未定义的属性:MembersController::$Myprofile然后问题可能出在Myprofile模型的代码中,无论是类名还是文件名。้ำั嘿,我按照你们的建议做了,但我得到了未定义的属性:MembersController::$Myprofile然后问题可能出在Myprofile模型的代码中,无论是类名还是文件名。้ำั嘿,我遵循了你们的建议,但我得到了未定义的属性:MembersController::$Myprofile้ำั嘿,我遵循了你们的建议,但我得到了未定义的属性:MembersController::$Myprofile