我正在使用cakephp 2.4.6开发我的应用程序。我知道stackoverflow中有很多与saveAssociated相关的帖子。我已经在我的数据库中实现了几个HABTM关系,现在我因为意外的行为而苦苦挣扎。 我有
Model Details
TEmployeeProfile HABTM TAddress
TEmPloyeeProfile HABTM TUserGroup
我的表名是
employee_profiles , t_addresses,t_addresses_employee_profiles,
t_user_groups,employee_profiles_t_user_groups
我的$ this-> request->数据包含
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
Ans当我尝试使用
保存时 $this->EmployeeProfile->saveALl($this->request->data)
it will save only EmployeeProfile Details..
与我错在一起..我到处都是... 因为我正在使用这种关系。如果我能解决这个问题,那么只有我可以做剩下的部分。请帮我... 我在这里发布了我的整个代码COMPLETE CODE
答案 0 :(得分:1)
据我所知,您需要重新考虑模型关联。因此,TEmployeeProfile
可以使用许多TAddress,然后您可以将模型链接为 -
<?php
class TEmployeeProfile extends AppModel{
public $useTable = 't_employee_profile';
public $belongsTo = array(
'CommunicationTAddress' => array(
'className' => 'TAddress',
'foreignKey' => 'communication_taddress_id'
),
'HomeTAddress' => array(
'className' => 'TAddress',
'foreignKey' => 'home_taddress_id'
)
);
public $hasAndBelongsToMany = array('TUserGroup');
}
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');