表单CakePhp 1形成多个表

时间:2011-08-28 16:45:08

标签: forms cakephp cakephp-1.3

我有两个控制器合作伙伴和交易。

当我添加交易时,会有字段合作伙伴ID,而在cakephp中,这会生成所有合作伙伴的下拉列表。如果用户想要添加新合作伙伴,他们可以点击复选框并显示添加表单。尝试添加合作伙伴输入框,是的,这会创建新合作伙伴,但在交易表中,它会从下拉列表中选择合作伙伴ID,而不是新合作伙伴。

交易Veiw

<div class="deals form">
<h2>Add New Deal</h2>
<p>Use the form below to fill in the new deals details.</p>
<?php echo $form->create('Deal');?>
    <?php
        echo $cksource->create();
        echo $form->input('title');
        echo $form->input('price');
        echo $form->input('market_price');
        echo $form->input('discount');
        echo $form->input('buy_link');
        echo $form->input('image');

        $config['toolbar'] = array(
            array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike', '-','BulletedList' ),
            array( 'Image', 'Link', 'Unlink', 'Anchor' )
        );
        ?><label>Deal Highlights</label><?php
        echo $cksource->ckeditor('highlights', array('config'=>$config));
        ?><label>Deal Fine Print</label><?php
        echo $cksource->ckeditor('fine_print', array('config'=>$config));

        echo $form->input('description');
        ?><hr />
        <h3>Partners Details<?php
        echo $form->input('partner_id');
        echo $form->input('Partner.name');
        echo $form->input('Partner.address1');
        echo $form->input('Partner.city');
        echo $form->input('Partner.county');
        echo $form->input('Partner.postcode');
        echo $form->input('city_id');
        ?><hr />
        <h3>Schedule Deal<?php
        echo $form->input('start');
        echo $form->input('end');
    echo $cksource->end();      
    ?>

<?php echo $form->end(__('Submit', true));?>
</div>

交易控制器

function admin_add() {
        if (!empty($this->data['Partner']['name'])) {
            $this->data['Deal']['partner_id'] = "";
            if ($this->Deal->Partner->saveAll($this->data)) {
                $this->Session->setFlash(__('The deal has been saved', true));
                $this->redirect(array('controller'=>'deals', 'action' => 'add'));
            } else {
                $this->Session->setFlash(__('The deal could not be saved. Please, try again.', true));
            }
        } else {
            if ($this->Deal->saveAll($this->data)) {
                $this->Session->setFlash(__('The deal has been saved', true));
                $this->redirect(array('controller'=>'deals', 'action' => 'add'));
            }
        }
        $partners = $this->Deal->Partner->find('list');

        $cities = $this->Deal->City->find('list');
        $this->set(compact('partners', 'cities'));
    }

任何想法家伙?如果您知道更好的方法,那么很高兴听到它,

由于 戴夫

1 个答案:

答案 0 :(得分:0)

保存前在控制器中

,检查合作伙伴表格是否为空,然后清空$ this-&gt;数据['Deal'] ['partner_id'];