Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthoraronbirkir
     
    I have two models Area and Unit.
    Area has_many units and Unit belongs_to Area.
    Generated scaffolds and modified to see a list of areas when editing Units.
    When I create new units the area is saved without problems.
    But when updating with another Area it keeps to original area_id although the newly chosen area id is written to the model parameters.

    Here is the code from the edit action including logging calls printing the attributes of the model:
    $this->_Logger->message("setting attributes",$this->Unit->getAttributes());
    $this->Unit->setAttributes($this->params['Unit']);
    $this->_Logger->message("set attributes",$this->Unit->getAttributes());
    if($this->Request->isPost() && $this->Unit->save()){
    $this->_Logger->message("setting attributes",$this->Unit->getAttributes());
    $this->flash_options = array('seconds_to_close' => 10);
    $this->flash['notice'] = $this->t('Unit was successfully '.($is_add?'created':'updated').'.');
    $this->redirectTo(array('action' => 'show', 'id' => $this->Unit->getId()));
    }

    And here is the corresponding log:

    Mon, 02 Feb 2009 02:30:28 +0100 [MESSAGE] setting attributes
    PARAMS{
    - id: 5
    - area_id: 4
    - name: Hall
    - description:
    - updated_at: 2009-02-02 02:24:38
    - created_at: 2009-02-02 02:19:08
    }
    Mon, 02 Feb 2009 02:30:28 +0100 [MESSAGE] set attributes
    PARAMS{
    - id: 5
    - area_id: 3
    - name: Hall
    - description:
    - updated_at: 2009-02-02 02:24:38
    - created_at: 2009-02-02 02:19:08
    }
    Mon, 02 Feb 2009 02:30:29 +0100 [MESSAGE] Updating Unit: UPDATE units SET id=5, area_id=4, name='Hall', description='', updated_at='2009-02-02 02:30:29', created_at='2009-02-02 02:19:08' WHERE id=5
    Mon, 02 Feb 2009 02:30:29 +0100 [MESSAGE] saved attributes
    PARAMS{
    - id: 5
    - area_id: 4
    - name: Hall
    - description:
    - updated_at: 2009-02-02 02:30:29
    - created_at: 2009-02-02 02:19:08
    }

    After setting the attibutes from the request to the model, the area_id changes to 3.
    But the update SQL statement contains the old area_id = 4.

    How am I supposed to handle the update ?
    Do I need to update the new id through the association object before saving ?

    Please advise.
    •  
      CommentAuthorbermi
     

    Hi Aron,

    Use the association handler, and make sure there are no validation errors, otherwise the SQL queries will be rolled back.

    Good luck