Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthoromagro
     
    I have a model called organization and this has many producers, I want
    to pass the id of organization to the organization_id on producer when
    I'm adding a new producer to the current organization?
    How can I do this, please help (I'm a newbie of course)
    My add function look like this
    function add()
    {
    if(!empty($this->params['producer'])){
    $this->Producer->setAttributes($this->params['producer']);
    $this->Producer->organization_id = $this->organization-
    >get('id'); // this is obviously not working what can I do

    if ($this->Request->isPost() && $this->Producer->save()){
    $this->flash['notice'] = $this->t('Producer was
    successfully created.');
    $this->redirectTo(array('action' => 'show', 'id' =>
    $this->producer->getId()));
    }
    }
    }
    Do I need to add anything to the producer model???
    • CommentAuthorsuthern
     

    I assume that when you are creating the input forms, you have the organization ID availible. If so, then simply put it in a hidden field inside the form (the _form.tpl inside your views/producer/ folder), and fill it's 'value' with the 'organization_id'. Something like this:

     <?php echo $form_helper->hidden_field('producer','organization_id',array('value' => $organization->id )); ?>
    

    Everything in the form is transferred to the database when you do a SAVE (as long as the field name is correct).

    • CommentAuthorThijs
     

    Another way: you could make a 'add producer to organisation' action in the organisation controller.

    That way you could pass the ID of the organisation with a special route.

    $Map->connect('/organisation/:id/producer/', array(
    'controller' => 'organisation',
    'action' => 'add_producer'
    ));
    

    and in the organisation controller:

    function add_producer(){ 
    
       if(!empty($this->params['producer'])){
    
          $this->Producer->setAttributes($this->params['producer']); 
          $this->Producer->organization_id = $this->params['id'];
    
          if ($this->Request->isPost() && $this->Producer->save()){ 
             $this->flash['notice'] = $this->t('Producer was successfully created.'); 
             $this->redirectTo(array('action' => 'show', 'id' => $this->producer->getId())); 
          } 
       } 
    } 
    

    .. or something like that