Model Cakephp saveAssociated和SaveAll Nothing正常工作
我正在使用cakephp 2.4.6开发我的应用程序。我知道stackoverflow中有很多帖子都与saveAssociated问题有关。我在数据库中实现了几个HABTM关系,现在由于它的意外行为,我很挣扎。 我有 我的桌名是Model Cakephp saveAssociated和SaveAll Nothing正常工作,model,save,cakephp-2.0,has-and-belongs-to-many,Model,Save,Cakephp 2.0,Has And Belongs To Many,我正在使用cakephp 2.4.6开发我的应用程序。我知道stackoverflow中有很多帖子都与saveAssociated问题有关。我在数据库中实现了几个HABTM关系,现在由于它的意外行为,我很挣扎。 我有 我的桌名是 employee_profiles , t_addresses,t_addresses_employee_profiles, t_user_groups,employee_profiles_t_user_groups 和我的$
employee_profiles , t_addresses,t_addresses_employee_profiles,
t_user_groups,employee_profiles_t_user_groups
和我的$this->request->data contain
array(
'EmployeeProfile' => array(
'first_name' => 'Emppppp',
'last_name' => 'kjljj',
't_login_id' => '222'
),
'TUserGroup' => array(
(int) 0 => '9',
(int) 1 => '13'
),
'TAddress' => array(
(int) 0 => array(
'number_street' => 'emmmm',
'area' => '545454',
'state' => '2',
'city' => '3',
),
(int) 1 => array(
'number_street' => 'empppppp',
'area' => 'nkjk',
'state' => '2',
'city' => '3',
)
)
)
现在当我尝试使用
$this->EmployeeProfile->saveAssociated($this->request->data)
it will save only EmployeeProfile and TUserGroup Details
$this->EmployeeProfile->saveALl($this->request->data)
it will save only EmployeeProfile Details..
当我尝试使用
$this->EmployeeProfile->saveAssociated($this->request->data)
it will save only EmployeeProfile and TUserGroup Details
$this->EmployeeProfile->saveALl($this->request->data)
it will save only EmployeeProfile Details..
我怎么了。。我到处都结巴。。。
因为每一次我都在使用这种相似的关系。如果我能解决这个问题,那么只有我能完成剩下的部分。请帮帮我。。。
我在这里发布了我的全部代码,因为我理解您的问题,您需要重新思考模型关联。因此,
TEmployeeProfile
可以接受许多数据,然后您可以将模型链接为-
TEmployeeProfile.php模型:
TEmployeeProfilesController.php控制器:
add.ctp视图
请帮我查一下TEmployeeProfile和EmployeeProfile之间的关系是什么?是$hasOne吗?@FazalRasel如果你知道答案,请回答可能有小错误…请用表格名称、型号、控制器和视图更新你的问题。。。或者你可以粘贴所有这些,并在这里提供一个链接。很可能这是一个模型命名问题。这个想法很好,谢谢。但是我告诉过你,地址表是一个通用表,它将使用所有人。好吧,不管怎样,我会实现这个。
CREATE TABLE t_employee_profile
(
id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
communication_taddress_id INT UNSIGNED NOT NULL, // this column is mandatory
home_taddress_id INT UNSIGNED NOT NULL, // this column is mandatory
//and your other columns
);
public function add(){
if($this->request->data){
if($this->TEmployeeProfile->saveAll($this->request->data)){
$this->Session->setFlash('data saved');
//redirect to another place
}else{
//what you wants to do if save fails
}
}
$TUserGroup = $this->TEmployeeProfile->TUserGroup->find('list');
$this->set('tUserGroups', $TUserGroup);
}
}
<?php
echo $this->Form->create();
// t_employee_profile
echo $this->Form->input('first_name');
//t_address as CommunicationTAddress
echo $this->Form->input('CommunicationTAddress.street');
echo $this->Form->input('CommunicationTAddress.city');
//t_address as HomeTAddress
echo $this->Form->input('HomeTAddress.street');
echo $this->Form->input('HomeTAddress.city');
//t_user_group
echo $this->Form->input('TUserGroup');
echo $this->Form->end('save');