CakePHP:保存从MySQL视图加载的模型

CakePHP:保存从MySQL视图加载的模型,php,mysql,cakephp,Php,Mysql,Cakephp,我有一个CakePHP模型“Person”,它从mysql视图加载数据(public$useTable='people\u rel';,people\u rel是视图)。 该视图将一些数据连接到表“people” 视图前缀\u people\u rel: CREATE ALGORITHM = UNDEFINED DEFINER = `xxxxxxx`@`%` SQL SECURITY DEFINER VIEW `prefix_people_rel` AS s

我有一个CakePHP模型“Person”,它从mysql视图加载数据(
public$useTable='people\u rel';
,people\u rel是视图)。
该视图将一些数据连接到表“people”

视图前缀\u people\u 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`)))
表格前缀\u人:

此模型中的region\u id、district\u id和country\u id列是只读的,它们在模型前哨站、region和district中编辑。
问题是我无法保存模型,因为它是从视图加载的。
我想从视图people\u rel加载数据并将其保存到people表中。我该怎么做?(我使用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';
}
public function beforeDelete($cascade = true) {
    $this->setSource('people');
    return true;
}

public function afterDelete() {
    $this->setSource('people_rel');
}
编辑2:

一个非常类似的解决方案也能起作用。请参阅下面的答案。

基于Nunser的想法,我找到了一个解决方案。
在模型中,我添加了beforeSave()和afterSave():


不清楚您是否使用同一型号的
People
进行读取和保存。但是,如果是,您是否尝试过在保存前更改
开头的表别名(和数据库表)并在保存后将其更改回
人名?我是否误解了您的问题?我正在从“people_rel”视图中读取数据,并希望将更改写入表“people”,但这一切都是从模型“Person”中完成的。我尝试了你的想法,使用了before-and-afterSave(参见我的编辑),但没有成功。基于你的想法,我找到了一个解决方案。我将在下面添加它。谢谢太好了!如果我必须做类似的事情,我会记住这一点。
public function beforeDelete($cascade = true) {
    $this->setSource('people');
    return true;
}

public function afterDelete() {
    $this->setSource('people_rel');
}