dongqindan4406 2019-07-03 09:45
浏览 274
已采纳

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

I want to create two Input Fields. One will save an Int the other one a value like K-0001. After saving those Input Fields and reloading the Form, I want to see inside the Fields the last Values they saved to the Database.

The reason I want to do it like this, is because that way I only need to change the last digit and can save the Form again. The Problem is I don't know how I can do that.

Example for better Explaining:
Form:

Field 1: First Value 20000. I Input the number 20001.
Field 2: First Value K-0000. I Input the text K-0001.

After saving my Form and Reloading it I want that it looks like this.

Field 1: Shows the Value 20001. I change it to 20005.
Field 2: Shows the Value K-0001. I change it to K-0005.

And again:

Field 1: Shows the Value 20005. I change it to 20007.
Field 2: Shows the Value K-0005. I change it to K-0007.
etc.

I guess that I need to create a Function which gets the Values from my Database. After that I need to put those inside my Input Fields, atleast that's what I'm thinking.

Code:

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


I hope I explained my Goal well enough and didn't forgot something. I'm still learning so if I forogt something just let me know and I will edit my Question.

  • 写回答

1条回答 默认 最新

  • dongshi1102 2019-07-05 07:55
    关注

    Found my Solution!

    I created a function inside my Controller to get me the Values i need:

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

    after that I only needed to add my $kdnr and $debinr to my ctp.

    add.ctp

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

    That was all I needed to do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问