【问题标题】:CakePHP Aro->save() not working when used inside afterSave() in User modelCakePHP Aro->save() 在用户模型的 afterSave() 中使用时不起作用
【发布时间】:2011-11-21 06:39:30
【问题描述】:

到目前为止,我已经尝试了大约一个小时,我目前使用的是 cakephp 2,除了这段代码之外,一切都运行良好

下面是我在用户模型中的内容

var $actsAs = array('Acl' => array('type' => 'requester'));

public function bindNode($user)
{
    # ativando o simplified node mode
    # trabalharemos apenas com permissões referentes ao grupo e não a cada usuário
    #
    return array('model' => 'Grupo', 'foreign_key' => $user['User']['grupo_id']);
}

public function parentNode()    # utilizado para determinar um relacionmamento Pai -> Filho
{
    if (!$this->id && empty($this->data))
        return null;

    if (isset($this->data['User']['grupo_id']))
        $groupId = $this->data['User']['grupo_id'];
    else
        $groupId = $this->field('grupo_id');

    if (!$groupId)
        return null;
    else
        return array('Grupo' => array('id' => $groupId));
}

public function afterSave( $created )   # depois que as infos deste usuario sao salvas
{
    # Funcao para permitir atualizar os grupos dos usuarios
    if (!$created)
    {
        $parent = $this->parentNode();
        $parent = $this->node($parent);
        $node = $this->node();
        $aro = $node[0];
        $aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
        $this->Aro->save($aro);
    }
}

基本上他做的一切都很好,用正确的 group_id 更新表 User 并且如果我使用 debug($aro) 我可以看到它正在尝试用正确的信息进行更新。

但是 $this->Aro->save($aro) 似乎不起作用,Cake 也没有显示任何错误消息,尝试了几种调试方式但没有显示错误,它根本没有更新 aro表。

有人知道为什么会这样吗?

PS.: Grupo 代替 Group 和 grupo_id 代替 group_id 在表格上也是正确的,它只是在这两种情况下从英语到葡萄牙语的翻译。

【问题讨论】:

    标签: php cakephp models behavior


    【解决方案1】:

    尝试打印(使用 pr($aro))来打印 $aro 数组的内容。如果它包含任何无效的字段名称(不在数据库表中的属性名称),则保存操作将失败。

    【讨论】:

      猜你喜欢
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多