Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    •  
      CommentAuthorriffed
    • CommentTimeFeb 19th 2008
     

    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
    • CommentTimeFeb 22nd 2008
     

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

    In the php code that generates the form, make the 'id' names of the field something like '$i."__{fieldname minus brackets}". Where $i is an incrementing number with each 'address' group of fields. Then You've got to be a bit tricky in the 'save' function. U have to make a loop to create a new 'address' for each address group including a save function. Confusing? Here's my loop around the save function

    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
    • CommentTimeFeb 25th 2008 edited
     

    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
    • CommentTimeFeb 26th 2008
     

    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
    • CommentTimeFeb 27th 2008
     

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

    • CommentAuthorVBharathi
    • CommentTimeMar 11th 2008 edited
     
    Hi,
    I want akelos framework ebook with fullfledged examples.
    • CommentAuthorsuthern
    • CommentTimeMar 11th 2008 edited
     

    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
    • CommentTimeJun 11th 2008 edited
     

    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.

Add your comments
    Username Password
  • Format comments as