It may be a typo, but you're setting a variable $categories, but
referencing $category in the view.
$this->set('categories', $this->Job->Category->find('list'));
And create the checkbox inputs like this:
<?= $form->input('Category', array('type' => 'select',
'multiple' => 'checkbox', 'label'=>'Categories'), $categories); ?>
Then you should be able to just use save() in the controller.
On Mar 16, 1:35 pm, Ode <[email protected]> wrote:
> I am working to build a job board with an admin side for adding jobs
> and categories, and a front end for browsing and searching.
>
> On the admin side I have a form for editing/creating jobs.
>
> Each job needs to be able to be assigned to multiple categories. I am
> assuming the best way to do this is through an HABTM association of
> the jobs and categories tables. So I have created the categories_jobs
> table and placed the HABTM associations along with joinTable
> properties in each of the jobs and categories models respectively.
>
> The following information is collected from the form: Title,
> Description, Location
>
> That information needs to be stored in the jobs table.
>
> Also, I have multiple checkboxes of categories on the form, these come
> from the categories table through the HABTM relation.
>
> When I try to save using $this->Job-save($this->data) I get an SQL
> error about saving an array.
>
> I need to be able to fill out the job data and then select multiple
> categories for the job to be associated with.
>
> Here is my action in the jobs_controller code:
>
> function edit($id=null) {
> $this->set('category', $this->Job->Category->find('list'));
> $data = $this->editOrCreate($id);
> if(!empty($this->data)) {
> $this->data['Category']['id'] = (array('category_id'));
> $this->User['User']['admin'] = 1;
> if($this->Job->saveAll($data)) {
> $this->Session->setFlash('The job has been saved');
> $this->redirect(array('action'=>'index',null,null,true));
>
> } else {
> $this->Session->setFlash('The Job could not be saved.
> Please try again later.');
> }
> }
> $categories = $this->Job->Category->find('list');
> }
>
> and here is the view where the data is entered:
>
> <?php echo $form->create('Job');?>
>
> <div class="body">
>
> <? if($editing): ?>
> <div class="floatRight">
> <?php echo $html->link('Delete Job',
> array('action'=>'delete',
> $form->value('Job.id')), null, __('Are you sure you want to delete
> this job?', true).' '. $form->value('Job.name')); ?>
> </div>
>
> <h2>Edit Job - <?=$form->value('Job.name')?></h2>
> <?=$form->input('username',array('type'=>'hidden'));?>
> <? else: ?>
> <h2>Create New Job</h2>
>
> <? endif; ?>
>
> <?=$form->input('name', array('autocomplete'=>'off','label'=>'Title',
> 'style'=>'width: 175px;'));?>
> <?=$form->input('description',
> array('autocomplete'=>'off','label'=>'Description', 'style'=>'width:
> 175px;'));?>
> <?=$form->input('location', array('autocomplete'=>'off',
> 'label'=>'Location', 'style'=>'width: 175px;'));?>
> <?=$form->input('job_id', array('type'=>'hidden',
> 'value'=>'job_id'));?>
> <?=$form->input('category_id',array('options'=>
> $category,'multiple'=>'checkbox', 'label'=>'Categories'));?>
>
> </div>
>
> <?=$form->save('Job');?>
>
> <?php echo $form->end();?>
>
> Any help to overcome this problem would be GREATLY appreciated. I am
> pretty new at PHP and CakePHP, so if you could be verbose in your
> reply I would be grateful.
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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en