Cakephp saveAssociated 和 SaveAll Nothing 正常工作
Posted
技术标签:
【中文标题】Cakephp saveAssociated 和 SaveAll Nothing 正常工作【英文标题】:Cakephp saveAssociated and SaveAll Nothing Working Properlly 【发布时间】:2014-04-30 10:55:02 【问题描述】:我正在使用 cakephp 2.4.6 开发我的应用程序。我知道***中有很多与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->data 包含
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
【问题讨论】:
请帮我解决很多问题 TEmployeeProfile 和EmployeeProfile 有什么关系?是 $hasOne 吗? @FazalRasel 如果你知道答案请回复 可能有小错误...请用表名、模型、控制器和视图更新您的问题...或者您可以将所有这些粘贴到此处并在此处提供链接.. 很可能是模型命名问题.. 【参考方案1】:据我了解,您需要重新考虑模型关联的问题。因此,TEmployeeProfile
可以占用多个 TAddress,然后您可以将模型链接为-
TEmployeeProfile.php 模型:
<?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');
TEmployeeProfile 的表架构:
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
);
TEmployeeProfilesController.php 控制器:
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);
添加.ctp 视图
<?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');
【讨论】:
这个主意不错,谢谢。但是我告诉过你,地址表是一个通用表,它会用到每个人。好的,无论如何我会实现这个。以上是关于Cakephp saveAssociated 和 SaveAll Nothing 正常工作的主要内容,如果未能解决你的问题,请参考以下文章
cakephp 模型 saveAssociated 错误 - 不能将字符串偏移量用作数组
Cakephp:使用`saveAssociated()`时如何将`parent_id`保存到基于树的模型中?
CakePHP如何在一个saveAll中不知道外键的情况下创建hasMany数据?