我有一个CakePHP模型“Person”,它从mysql视图加载数据(public $useTable = 'people_rel';
,people_rel是视图)。
视图将一些数据连接到表'people'。
视图prefix_people_rel:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `xxxxxxx`@`%`
SQL SECURITY DEFINER
VIEW `prefix_people_rel` AS
select
`prefix_people`.`id` AS `id`,
`prefix_people`.`username` AS `username`,
`prefix_people`.`first_name` AS `first_name`,
`prefix_people`.`last_name` AS `last_name`,
`prefix_people`.`email` AS `email`,
`prefix_people`.`password` AS `password`,
`prefix_people`.`status` AS `status`,
`prefix_people`.`outpost_id` AS `outpost_id`,
`prefix_outposts`.`region_id` AS `region_id`,
`prefix_regions`.`district_id` AS `district_id`,
`prefix_districts`.`country_id` AS `country_id`
from
(((`prefix_people`
left join `prefix_outposts` ON ((`prefix_people`.`outpost_id` = `prefix_outposts`.`id`)))
left join `prefix_regions` ON ((`prefix_outposts`.`region_id` = `prefix_regions`.`id`)))
left join `prefix_districts` ON ((`prefix_regions`.`district_id` = `prefix_districts`.`id`)))
表prefix_people:
在此模型中,region_id,district_id和country_id列是只读的,它们在模型Outpost,Region和District中进行编辑。
问题是我无法保存模型,因为它是从视图加载的
我想从视图people_rel加载数据并将它们保存到表人员中。我怎样才能做到这一点? (我使用CakePHP 2.5.3并希望在稳定版本可用时升级到3)
非常感谢提前!
修改
这没有帮助:
模范人物:
public function beforeSave($options = array())
{
// ... Password hashing ...
$this->useTable = 'people';
return true;
}
public function afterSave($created, $options = array()) {
$this->useTable = 'people_rel';
}
编辑2:
一个非常类似的解决方案。请参阅下面的答案。
答案 0 :(得分:1)
根据Nunser的想法,我找到了解决方案 在模型中,我添加了 beforeSave()和afterSave():
public function beforeSave($options = array())
{
//$this->useTable = 'people'; <= Does not work
$this->setSource('people'); // Works
return true;
}
public function afterSave($created, $options = array()) {
//$this->useTable = 'people_rel'; <= Does not work
$this->setSource('people_rel'); // Works
}
删除对象所必需的:
public function beforeDelete($cascade = true) {
$this->setSource('people');
return true;
}
public function afterDelete() {
$this->setSource('people_rel');
}