Question

I am a CakePHP newb and have limited PHP experience/knowledge so please be gentle! :) I have searched and read and searched and read some more but I can't get my head around what my issue is... I have multiple issues, but they all relate to HABTM... FWIW these files were originally generated by cake bake, but I have mucked with them a bit since then (as it never worked OOTB). This is the first issue - not saving all data!

I have these tables:

activities
activitities_unitquals
types
unitquals
users

And this is my Activity Model:

class Activity extends AppModel {
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id'
        ),
        'Type' => array(
            'className' => 'Type',
            'foreignKey' => 'type_id'
        )
    );
    public $hasAndBelongsToMany = array(
        'Unitqual' => array(
            'className' => 'Unitqual',
            'joinTable' => 'activitities_unitquals',
            'foreignKey' => 'activity_id',
            'associationForeignKey' => 'unitqual_id',
            'unique' => false
        )
    );

}

And my ActivitiesController:

class ActivitiesController extends AppController {
    public function index() {
        $this->Activity->recursive = 0;
        $this->set('activities', $this->paginate());
    }
    public function view($id = null) {
        if (!$this->Activity->exists($id)) {
            throw new NotFoundException(__('Invalid activity'));
        }
        $options = array('conditions' => array('Activity.' . $this->Activity->primaryKey => $id));
        $this->set('activity', $this->Activity->find('first', $options));
    }
    public function add() {
        if ($this->request->is('post')) {
            $this->Activity->create();
            if ($this->Activity->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The activity has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The activity could not be saved. Please, try again.'));
            }
            debug($this->Activity->validationErrors);
        }
        $users = $this->Activity->User->find('list');
        $types = $this->Activity->Type->find('list');
        $unitquals = $this->Activity->Unitqual->find('list');
        $this->set(compact('users', 'types', 'unitquals'));
    }
}

My activities/index seems to work ok (although it doesn't show any of the 'unitquals' but I don't care about that). The activities/view/id isn't showing any of the Unitquals info either (which I will need to fix). But the biggest issue is that it isn't saving the Unitqual info when I use activities/add.

Here is the View/Activities/add.ctp:

<div class="activities form">
<?php echo $this->Form->create('Activity'); ?>
    <fieldset>
        <legend><?php echo __('Add Activity'); ?></legend>
    <?php
        echo $this->Form->input('user_id');
        echo $this->Form->input('type_id');
        echo $this->Form->input('other');
        echo $this->Form->input('date');
        echo $this->Form->input('name');
        echo $this->Form->input('detail');
        echo $this->Form->input('status', array(
            'checked' => true
            ));
        echo $this->Form->input('unitqual', array(
            'type' => 'select',
            'multiple' => 'checkbox',
            'options' => $unitquals,
            'label' => 'Units & Quals'
            ));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>

The form displays fine, but it just doesn't save anything to the activitities_unitquals table (everything else is saved correctly to the activities table).

Any ideas???

Was it helpful?

Solution

add model name before field..

    echo $this->Form->input('Unitqual.unitqual', array( // here, add the model name
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $unitquals,
        'label' => 'Units & Quals'
        ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top