CakePHP 2.3 - Newb HABTM问题

时间:2014-04-25 18:45:40

标签: cakephp-2.3

我是CakePHP新手,并且PHP经验/知识有限,所以请保持温和! :) 我已经搜索过,阅读和搜索并阅读了一些内容,但我无法理解我的问题...我有多个问题,但它们都与HABTM有关... FWIW这些文件最初是由蛋糕烘烤,但从那时起我已经与它们混淆了一些(因为它从来没有工作OOTB)。这是第一个问题 - 不保存所有数据!

我有这些表格:

activities
activitities_unitquals
types
unitquals
users

这是我的活动模型:

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
        )
    );

}

我的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'));
    }
}

我的活动/索引似乎运作正常(虽然它没有显示任何' unitquals'但我并不关心它)。活动/视图/ ID不显示任何Unitquals信息(我将需要修复)。但最大的问题是,当我使用活动/添加时,它不会保存Unitqual信息。

以下是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>

表单显示正常,但它不会将任何内容保存到activitities_unitquals表(其他所有内容都正确保存到活动表中)。

任何想法???

1 个答案:

答案 0 :(得分:0)

在字段前添加模型名称..

    echo $this->Form->input('Unitqual.unitqual', array( // here, add the model name
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $unitquals,
        'label' => 'Units & Quals'
        ));