Caekphp相关的模型数据未保存到数据库

时间:2019-06-07 19:51:44

标签: html cakephp cakephp-3.0

我需要帮助弄清楚为什么我的模型数据不会保存到数据库的两个表中。当我单击提交时,数据仅正确地保存到被调用的表中,而不正确地保存到关联的表中。

我的控制器动作:

public function build() {
    $usery = $this->UserAuth->getUserId();

    $available = null;

    $available['pd'] = $this->Parishes->find('all',['conditions'=>['Parishes.user'=>$usery]])->contain(['ORG']);
    $available['rel'] = $this->Reledfields->find('all',['conditions'=>['Reledfields.user_id'=>$usery]])->contain(['ORG']);
    $available['tech'] = $this->Technologytabs->find('all',['conditions'=>['Technologytabs.user_id'=>$usery]])->contain(['ORG']);

    //dump($available);
    $this->set('available',$available);

    $reim = $this->Reimburstment->newEntity();
    if ($this->request->is(['post'])) {
        //$getData = $this->request->getData();

        //$parishes = $getData['parishes'];
        //$rels = $getData['rels'];
        //$techs = $getData['techs'];
        //dump($this->request->getData());

        $reim = $this->Reimburstment->patchEntity($reim, $this->request->getData(),['associated' => ['REC']]);

        $reim['user_id'] = $usery;
        $reim['organization_id'] = $this->General->getOrganizationForUserId($usery)['id'];

        //$errors = $reim->getErrors();
        dump($reim);
        if ($this->Reimburstment->save($reim, ['validate'=>false])) {
            $this->Flash->success(__('Reimburstment submitted.'));
            //var_dump($reim);

        } else {
            $this->Flash->error(__('Unable to submit reimburstment.'));
        }
    }
    $this->set(compact('reim',$reim));
}

我的模特报销:

class ReimburstmentTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Timestamp');

        $this->hasOne('ORG', [
            'className' => 'Organizations',
            'bindingKey' => 'organization_id',
            'foreignKey' => 'id',
            'propertyName' =>'organization'
        ]);

        $this->hasMany('REC', [
            'className' => 'Receipt',
            'bindingKey' => 'id',
            'foreignKey' => 'reimb_id',
            'propertyName' => 'receipt'
        ]);

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'propertyName' =>'user_info'
        ]);
        /*
        $this->hasOne('Files', [
            'foreignKey' => 'file_id'
        ]);
        */
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {

        return $validator;
    }

    public function buildRules(RulesChecker $rules)
    {
//        $rules->add($rules->existsIn(['user_id'], 'Users'));

        return $rules;
    }
}

我的模型收据:

class ReceiptTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Timestamp');

        $this->belongsTo('REIM', [
            'foreignKey' => 'reimb_id',
            'propertyName' =>'reimburstment'
        ]);

        $this->hasOne('PD', [
            'className' => 'Parishes',
            'bindingKey' => 'entry_id',
            'foreignKey' => 'id',
            'propertyName' => 'grant'
        ]);

        $this->hasOne('REL', [
            'className' => 'Reledfields',
            'bindingKey' => 'entry_id',
            'foreignKey' => 'id',
            'propertyName' => 'grant'
        ]);

        $this->hasOne('TECH', [
            'className' => 'Technologytabs',
            'bindingKey' => 'entry_id',
            'foreignKey' => 'id',
            'propertyName' => 'grant'
        ]);

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'propertyName' =>'user_info'
        ]);
        /*
        $this->hasOne('Files', [
            'foreignKey' => 'file_id'
        ]);
        */
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {

        return $validator;
    }

    public function buildRules(RulesChecker $rules)
    {
//        $rules->add($rules->existsIn(['user_id'], 'Users'));

        return $rules;
    }
}

我的观点:

<?= $this->Form->create($reim) ?>
<div class="form-group" style="position: static;">
    <label for="input-id-5">Reimburstment Name</label>
    <?php echo $this->Form->control('name', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'remname', 'placeholder'=>'Reimburstment Name']);?>
</div>

<div class="form-group" style="position: static;">
    <label for="input-id-5">Comment</label>
    <?php echo $this->Form->control('comment', ['type'=>'textaarea', 'label'=>false, 'class'=>'form-control', 'id' => 'reimcom', 'placeholder'=>'Enter Comment']);?>
</div>

<div class="form-group" style="position: static;">
     <label for="input-id-5">Reimburstment Total</label>
     <?php echo $this->Form->control('riem_amount', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'readonly'=>false, 'id' => 'reimamount']);?>
</div>

<input class="btn btn-info" type="submit" value="Add Reciept">
<div id="reciept_1">
    <div class="row">
        <div class="col-md-12" style="display: block;">
            <div class="form-group" style="position: static;">
                <label for="input-id-1">Vendor</label>
                <?php echo $this->Form->control('rec.vendor', ['type'=>'text', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-1', 'placeholder'=>'Enter Vendor Name']);?>
            </div>
            <div class="form-group" style="position: static;">
                <label for="input-id-2">Item Cost</label>
                <?php echo $this->Form->control('rec.item_cost', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-2', 'placeholder'=>'Enter Item Cost']);?>
            </div>
            <div class="form-group" style="position: static;">
                <label for="input-id-3">Shipping</label>
                <?php echo $this->Form->control('rec.shipping', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-3', 'placeholder'=>'Enter Shipping Amount']);?>
            </div>
            <div class="form-group" style="position: static;">
                <label for="input-id-4">Tax</label>
                <?php echo $this->Form->control('rec.tax', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-4', 'placeholder'=>'Enter Tex']);?>
            </div>
            <div class="form-group" style="position: static;">
                <label for="input-id-5">Total</label>
                <?php echo $this->Form->control('rec.total', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-5', 'readonly'=>true, 'placeholder'=>'Total']);?>
            </div>
            <h4>ALLOCATE</h4>
            <div class="form-group" style="position: static;">
                <label for="input-id-4">ENTRY 1</label>
                <?php echo $this->Form->control('rec.entry1_id', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-7', 'placeholder'=>'Entry ID']);?>
                <?php echo $this->Form->control('rec.amount_en1', ['type'=>'field', 'label'=>false, 'class'=>'form-control', 'id' => 'input-id-8', 'placeholder'=>'Enter Amount Divided']);?>
            </div>
        </div>
    </div>
</div>
    <?= $this->Form->button(__('Submit'), ['class' => 'btn btn-success float-right']) ?>
    <?= $this->Form->end() ?>

我已经研究了2天了,没有得到想要的结果。

0 个答案:

没有答案