如何从数据库中获取值,并在输入字段中使用和显示它?

时间:2019-07-03 09:45:18

标签: php html cakephp

我想创建两个输入字段。一个将保存一个Int,另一个将保存一个K-0001之类的值。保存这些输入字段并重新加载表单后,我想在字段中查看它们保存到数据库中的最后一个值。

之所以要这样做,是因为这样,我只能需要更改最后一位数字,然后可以再次保存表格。问题是我不知道该怎么做。

更好的解释示例:
形成:

字段1:第一个值20000。我输入数字20001。
栏位2:第一个值K-0000。我输入文字K-0001。

保存并重新加载表单后,我希望它看起来像这样。

字段1:显示值20001。我将其更改为20005。
栏位2:显示值K-0001。我将其更改为K-0005。

再次:

字段1:显示值20005。我将其更改为20007。
栏位2:显示值K-0005。我将其更改为K-0007。
等等

我想我需要创建一个从数据库中获取值的函数。之后,我需要将它们放入“输入字段”中,至少这是我的想法。

代码:

add.ctp

div class="customers form large-9 medium-8 columns content">
    <?= $this->Form->create($customer) ?>
    <fieldset>
        <legend><?= __('Neuen Kunden erstellen') ?></legend>
        <?php
            echo $this->Form->control('tour_id', ['options' => $tours, 'empty' => true]);
/*          echo $this->Form->control('order', array('label' => __('Bestellung', true))); */
            echo $this->Form->control('kdnr', array('label' => __('Kundennummer', true)));
            echo $this->Form->control('debinr', array('label' => __('Debitorennummer', true)));
            echo $this->Form->control('anrede', array('label' => __('Anrede', true)));
            echo $this->Form->control('name', array('label' => __('Name', true)));
            echo $this->Form->control('strasse', array('label' => __('Straße', true)));
            echo $this->Form->control('plz', array('label' => __('PLZ', true)));
            echo $this->Form->control('ort', array('label' => __('Ort', true)));
            echo $this->Form->control('tel', array('label' => __('Telefon', true)));
            echo $this->Form->control('kontonummer', array('label' => __('Kontonummer', true)));
            echo $this->Form->control('bankleitzahl', array('label' => __('Bankleitzahl', true)));
            echo $this->Form->control('lastschrift', array('label' => __('Lastschrift', true)));            
            echo $this->Form->control('detail', array('label' => __('Weitere Details', true)));
            echo $this->Form->control('betreuer_anrede', array('label' => __('Betreuer Anrede', true)));
            echo $this->Form->control('betreuer_name', array('label' => __('Betreuer Name', true)));
            echo $this->Form->control('betreuer_strasse', array('label' => __('Betreuer Straße', true)));
            echo $this->Form->control('betreuer_plz', array('label' => __('Betreuer PLZ', true)));
            echo $this->Form->control('betreuer_ort', array('label' => __('Betreuer Ort', true)));
            echo $this->Form->control('betreuer_on_bill', array('label' => __('Betreuer soll auf der Rechnung stehen', true)));
        ?>
    </fieldset>
    <?= $this->Form->button(__('Bestätigen')) ?>
    <?= $this->Form->end() ?>
</div>


CustomersTable.php

class CustomersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('customers');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->belongsTo('Tours', [
            'foreignKey' => 'tour_id'
        ]);
        $this->hasMany('Bills', [
            'foreignKey' => 'customer_id'
        ]);
        $this->hasMany('Orders', [
            'foreignKey' => 'customer_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', 'create');

        $validator
            ->integer('order')
            ->allowEmptyString('order');

        $validator
            ->scalar('kdnr')
            ->maxLength('kdnr', 45)
            ->allowEmptyString('kdnr');

        $validator
            ->scalar('debinr')
            ->maxLength('debinr', 31)
            ->allowEmptyString('debinr');

        $validator
            ->scalar('anrede')
            ->maxLength('anrede', 45)
            ->allowEmptyString('anrede');

        $validator
            ->scalar('name')
            ->maxLength('name', 45)
            ->allowEmptyString('name');

        $validator
            ->scalar('strasse')
            ->maxLength('strasse', 45)
            ->allowEmptyString('strasse');

        $validator
            ->integer('plz')
            ->allowEmptyString('plz');

        $validator
            ->scalar('ort')
            ->maxLength('ort', 45)
            ->allowEmptyString('ort');

        $validator
            ->scalar('tel')
            ->maxLength('tel', 45)
            ->allowEmptyString('tel');

        $validator
            ->boolean('lastschrift')
            ->allowEmptyString('lastschrift');

        $validator
            ->scalar('kontonummer')
            ->maxLength('kontonummer', 32)
            ->allowEmptyString('kontonummer');

        $validator
            ->integer('bankleitzahl')
            ->maxLength('bankleitzahl', 32)
            ->allowEmptyString('bankleitzahl');

        $validator
            ->scalar('detail')
            ->allowEmptyString('detail');

        $validator
            ->scalar('betreuer_anrede')
            ->maxLength('betreuer_anrede', 45)
            ->allowEmptyString('betreuer_anrede');

        $validator
            ->scalar('betreuer_name')
            ->maxLength('betreuer_name', 45)
            ->allowEmptyString('betreuer_name');

        $validator
            ->scalar('betreuer_strasse')
            ->maxLength('betreuer_strasse', 45)
            ->allowEmptyString('betreuer_strasse');

        $validator
            ->integer('betreuer_plz')
            ->allowEmptyString('betreuer_plz');

        $validator
            ->scalar('betreuer_ort')
            ->maxLength('betreuer_ort', 45)
            ->allowEmptyString('betreuer_ort');

        $validator
            ->boolean('betreuer_on_bill')
            ->allowEmptyString('betreuer_on_bill');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['tour_id'], 'Tours'));

        return $rules;
    }
}


我希望我对目标的解释足够好,并且没有忘记什么。我仍在学习,因此,如果我放弃某些内容,请告诉我,然后我将编辑我的问题。

1 个答案:

答案 0 :(得分:0)

找到我的解决方案!

我在Controller内创建了一个函数,以获取所需的值:

CustomersController.php

$query = $this->Customers->find('list', [
            'order' => ['Customers.kdnr' => 'DESC'],
            'valueField' => 'kdnr',
            'limit' => 1]);

        $kdnr = $query->first();
        $kdnr++;

        $query = $this->Customers->find('list', [
            'order' => ['Customers.debinr' => 'DESC'],
            'valueField' => 'debinr',
            'limit' => 1]);

        $debinr = $query->first();
        $debinr++;

        $this->set(compact('customer', 'tours', 'kdnr', 'debinr'));

之后,我只需要将$ kdnr和$ debinr添加到ctp中即可。

add.ctp

echo $this->Form->control('kdnr',['value' => $kdnr]);
echo $this->Form->control('debinr',['value' => $debinr]);

那是我要做的。