1 to 10 of 10
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?
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.
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}
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()));
}
}
}
Hey Riffed, your's is much cleaner. Thank you for sharing!
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).
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.
1 to 10 of 10