Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    •  
      CommentAuthorriffed
     

    I'm new to Ak and started a contact application to help learn the framework.

    I have a companies table with a has_many addresses table. I'd like to use a single form to add a company with multiple address. What is the best way to handle adding new addresses? How do I represent multiple addresses in the form?

    • CommentAuthorsuthern
     

    I can tell you how I've done something simliar, but it's kinda messy.

    IF($this->Request->isPost()) { FOR($i=1;array_key_exists($i.'__source_spot_id',$this->params['spots_transfers']);$i++) { $_s_t = array('source_spot_id' => $this->params['spots_transfers'][$i."__source_spot_id"], 'dest_spot_id' => $this->params['spots_transfers'][$i."__dest_spot_id"], 'unit_qty' => $this->params['spots_transfers'][$i."__unit_qty"], 'unit_id' => $this->params['spots_transfers'][$i."__unit_id"], 'user_id' => $this->USER_ID ); $this->parent[$i] = $this->SpotsTransfers->cloneRecord(); $this->parent[$i]->setAttributes($_s_t, true); IF($this->parent[$i]->save()) { $this->f .= $this->params['spots_transfers'][$i."__description"].", "; }; } // Here I have cut out some logic on where to go next, for sake of clarity } Like I said, not pretty, but it does work. I too would like to know a way to create multiple entries all at once without resorting to silly $i__ names.

    •  
      CommentAuthorriffed
     

    Here is my view solution, which provides an array with fields like company[addresses][0][id], etc.

    AK doesn't save the address(es) automatically though, which I thought it would. Do I have to create and save an address object in the company update action or is there away to get AK to validate and save the child records?

        {?Company.addresses}
          {loop Company.addresses}
             <?php
                $content_columns = array_keys($address->getContentColumns());
                $af = $form_helper->fields_for('company[addresses][' . intval($address_loop_counter - 1) . ']', $address);
                echo $af->hidden_field('id');
                foreach($content_columns as $content_column) :    
            ?>
            <label for="company[addresses][<?= $address_loop_counter-1 ?>]_<?= $content_column ?>"><?= ucfirst($content_column) ?></label><br />
            <?php 
                echo $af->text_field($content_column) ;
                endforeach;
             ?>                
          {end}
        {end}
    
    •  
      CommentAuthorriffed
     

    Here is my controller action. It doesn't handle deleted addresses or validations yet but I'll post a complete example on the wiki in a few days.

        function edit()
        {
            if (empty($this->params['id']))
            {
                $this->redirectToAction('listing');
            }
    
            /* finder_options isn't working in the current svn so you still have add the options to find() */
            $this->company = $this->Company->find($this->params['id'], $this->finder_options['Company']);
    
    
            if(!empty($this->params['company']) && !empty($this->params['id']))
            {
                $this->company->setAttributes($this->params['company']);
    
    
                $address_count= count($this->params['company']['addresses']);
                /* This loop will be changed to a foreach */        
                for($address_counter=0; $address_counter < $address_count; $address_counter++)
                {                    
    
                    if (empty($this->params['company']['addresses'][$address_counter]['id']))
                    {
                        /* add new address */
                        $this->company->address->add(
                            $this->company->address->build($this->params['company']['addresses'][$address_counter])
                        );
                    }
                    else
                    {
                        /* update current address */
                        $this->company->addresses[$address_counter]->setAttributes(
                            $this->params['company']['addresses'][$address_counter]
                        );
                    }
                }
    
                if($this->Request->isPost() && $this->company->save())
                {
                    $this->flash['notice'] = $this->t('Company was successfully updated.');
                    $this->redirectTo(array('action' => 'show', 'id' => $this->company->getId()));
                }
            }
        }
    
    • CommentAuthorsuthern
     

    Hey Riffed, your's is much cleaner. Thank you for sharing!

    • CommentAuthorVBharathi
     
    Hi,
    I want akelos framework ebook with fullfledged examples.
    • CommentAuthorsuthern
     

    The book framework tutorial could be extended a little bit to cover slightly more advanced cases. Perhaps after the wiki has been filled out a bit more then we can update the tutorial (using good akelos practices from the wiki).

    • CommentAuthorinsanet
     

    i have 1 question, this is my code:

            if(!empty($this->params['product'])){
    
            $this->Product->setAttributes($this->params['product']);
            $this->product_detail->setAttributes($this->params['product_detail']);
    
            $this->product->product_detail->build($this->params['product_detail']);
    
            if ($this->Request->isPost() && $this->Product->save()){
                $this->flash['notice'] = $this->t('Product was successfully created.');
            }
    

    now the form shows the validation errors of product, but how i can show the validation errors of product_detail too? (product and product_detail has a relation of 1:1). from what i understood, the 2 validate method of each class are called, but i can only show the product errors, dont know how to access the other ones, my form look like this:

    <?php  echo $active_record_helper->error_messages_for('product');?>
     // this doesnt show anything <?php  echo $active_record_helper->error_messages_for('product_detail');?>
    
        <?php echo $active_record_helper->input('product_detail', 'title')?>
        <?php echo $active_record_helper->input('product_detail', 'description')?>
    
        <?php echo $active_record_helper->input('product', 'license')?>
        <?php echo $active_record_helper->input('product', 'avail_from_on')?>
    

    i think there should be a way of adding the validation errors of product_detail to product errors, well i think that could be the way to go.

    • CommentAuthorenser
     
    Maybe this will help for somebody. Form with two different records for same model.

    In the view form (_form.tppl):
    <p>
    <label for="phone_phone_number">_{Phone number (Mobile)}</label><br />
    <?php echo $form_helper->text_field('phone_mobile', 'phone_number')?>
    </p>
    <p>
    <label for="phone_phone_number">_{Phone number (Land)}</label><br />
    <?php echo $form_helper->text_field('phone_land', 'phone_number')?>
    </p>

    In the add action in Controller (my case user_controller function add() ):
    $phone_mobile = new Phone();
    $phone_land = new Phone();
    $phone_mobile->setAttributes($this->params['phone_mobile']);
    $phone_mobile->set('phone_type_id','1');
    $phone_land->setAttributes($this->params['phone_land']);
    $phone_land->set('phone_type_id','2');
    $phone_mobile->save();
    $phone_land->save();

    If you need more then two records you can integrate it in some loop. But that is the idea.
    • CommentAuthormasylum
     
    I'm working on a form-helper-plugin that makes those forms tasks really easy to implement.
    It handles multi models forms, labels, error messages and even additional field information.

    I will try to release it next week.