代码之家  ›  专栏  ›  技术社区  ›  user2133231

Cakephp 2.3.1 HABTM编辑表单显示下拉框而不是多选框

  •  2
  • user2133231  · 技术社区  · 12 年前

    我在舞者和舞者之间有一种HABTM的关系。除了在编辑我的舞蹈的edit.ctp上,CakePHP显示了一个下拉框,而不是我预期的多选框之外,一切似乎都如预期的那样。令我疯狂的是,多选框与编辑.ctp的工作方式相反,用于编辑Dancers!我不明白为什么它能以一种方式工作,但不能以另一种方式。

    这是我的舞蹈模型,定义了舞蹈和舞者之间的关系:

        public $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            )
        );
    
    /**
     * hasAndBelongsToMany associations
     *
     * @var array
     */
        public $hasAndBelongsToMany = array(
            'Dancer' => array(
                'className' => 'Dancer',
                'joinTable' => 'dancers_dances',
                'foreignKey' => 'dance_id',
                'associationForeignKey' => 'dancer_id',
                'unique' => 'keepExisting',
            )
        );
    

    这是我的舞蹈总监:

    public function edit($id = null) {
        if (!$this->Dance->exists($id)) {
            throw new NotFoundException(__('Invalid dance'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Dance->save($this->request->data)) {
                $this->Session->setFlash(__('The dance has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The dance could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Dance.' . $this->Dance->primaryKey => $id));
            $this->request->data = $this->Dance->find('first', $options);
        }
        $users = $this->Dance->User->find('list');
        $dancers = $this->Dance->Dancer->find('list');
        $this->set(compact('users', 'dancers'));
    }
    

    这是我为舞蹈视图编辑的.ctp

    <div class="dances form">
    <?php echo $this->Form->create('Dance'); ?>
        <fieldset>
            <legend><?php echo __('Edit Dance'); ?></legend>
        <?php
            echo $this->Form->input('id');
            echo $this->Form->input('dance_name');
            echo $this->Form->input('users_id');
            echo $this->Form->input('Dancers');
        ?>
        </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>
    </div>
    <div class="actions">
        <h3><?php echo __('Actions'); ?></h3>
        <ul>
    
            <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Dance.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('Dance.id'))); ?></li>
            <li><?php echo $this->Html->link(__('List Dances'), array('action' => 'index')); ?></li>
            <li><?php echo $this->Html->link(__('List Dancers'), array('controller' => 'dancers', 'action' => 'index')); ?> </li>
            <li><?php echo $this->Html->link(__('New Dancers'), array('controller' => 'dancers', 'action' => 'add')); ?> </li>
        </ul>
    </div>
    

    我认为相关表格如下: 舞者, 跳舞者跳舞, 舞蹈

    在舞蹈表上,我有以下几列: 身份证件, dancer_id中, 舞蹈id

    请告诉我更多信息是否有帮助。

    提前谢谢。

    1 回复  |  直到 12 年前
        1
  •  4
  •   thaJeztah    12 年前

    首先,你可以去掉HABTM定义中的大多数设置,因为你已经在使用Cake Conventions了;

    public $hasAndBelongsToMany = array(
        'Dancer' => array(
            'unique' => 'keepExisting',
        )
    );
    

    但问题出在表单中输入的名称上,应该是 单数的 并以与其相关的模型命名;

    echo $this->Form->input('Dancer');
    

    您可以在此处看到手册中的一些示例(请在本节中查找“标记”示例):

    http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm