Monday, April 5, 2010

Re: Yet another HABTM multiple checkbox query, cakePHP 1.3 RC3

Hi Andy,

Thanks for replying....

> Why are you trying to save to the relationship table? Cake will do
> that for you automatically.

That's what I was lead to believe, but it doesn't seem to be working.

> What does your view look like (at least the part that creates the
> form)? Provided your HABTM is set up correctly in the models and view,
> calling $this->Model->save($this->data) in the controller should be
> sufficient.

Here is the view:

<?php
echo $this->Form->input('id');
echo $this->Form-
>input('KsfDimensionsPortfolioEntry.portfolio_entry_id',array('type'=>'hidden','value'=>
$this->data['PortfolioEntry']['id']));
//echo $this->Form->input('user_id');
echo $this->Form-
>input('when',array('dateFormat'=>'DMY','timeFormat'=>'none','separator'=>'
'));
echo $this->Form->input('title');
echo $this->Form->input('description');
echo $this->Form->input('benefit');
echo $this->Form->input('activity_id');
echo $this->Form-
>input('KsfDimensionsPortfolioEntry.ksf_dimension_id',array('type'=>'select','multiple'=>'checkbox','label'=>'KSF
dimensions','selected'=>$selectedDimensions));
?>

Just for good measure, the action in the controller is:

function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(sprintf(__('Invalid %s', true), 'portfolio
entry'));
$this->redirect(array('action' => 'index'));
}
$portid = $this->PortfolioEntry->id;
if (!empty($this->data)) {
if ($this->PortfolioEntry->saveAll($this->data)) {
$this->PortfolioEntry->KsfDimensionsPortfolioEntry->save($this-
>data);
$this->Session->setFlash(sprintf(__('The %s has been saved',
true), 'portfolio entry'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(sprintf(__('The %s could not be saved.
Please, try again.', true), 'portfolio entry'));
}
}
if (empty($this->data)) {
$this->data = $this->PortfolioEntry->read(null, $id);
}
//$users = $this->PortfolioEntry->User->find('list');
$activities = $this->PortfolioEntry->Activity->find('list');
$ksfDimensions = $this->PortfolioEntry->KsfDimension-
>find('list',array('order'=>'KsfDimension.id asc'));
//set $selectedDimensions to the Dimension in the join table - need
to just find the one relating to this portfolio though!
$selectedDimensions = $this->PortfolioEntry-
>KsfDimensionsPortfolioEntry->find('list',
array('fields'=>array('ksf_dimension_id'),'conditions'=>array('portfolio_entry_id='.
$portid)));
//debug($selectedDimensions);
//die();
$this-
>set(compact('activities','ksfDimensions','selectedDimensions'));
}

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

To unsubscribe, reply using "remove me" as the subject.

No comments: