Cakephp外键ID设置不正确

时间:2017-01-02 04:25:49

标签: php mysql cakephp

1..n或n..m数据库表中的引用ID未自动设置。 请参阅下面的示例。我认为子表中的client_id是自动设置的,但在我的情况下,在某些情况下它是0。

要成为concreate:Users.client_id设置为client.id,但Contacts.client_id始终为0

DB

enter image description here

CREATE TABLE `clients` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `service_paid` date DEFAULT NULL,      
  `service_contract_accepted` tinyint(1) NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `users` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `client_id` int(11) NOT NULL,
   `contact_id` int(11) NOT NULL,
   `email` varchar(255) NOT NULL,
   `password` varchar(255) NOT NULL,
   `role` char(50) DEFAULT 'user',
   `token` varchar(255) DEFAULT NULL,
   `token_expires` datetime DEFAULT NULL,
   `activation_date` datetime DEFAULT NULL,
   `active` tinyint(1) DEFAULT '0',
   `created` datetime DEFAULT NULL,
   `modified` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `contacts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `client_id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `contact_categories_id` int(11) DEFAULT NULL,
  `company_indicator` tinyint(1) NOT NULL DEFAULT '0',
  `company_name` varchar(100) DEFAULT NULL,
  `salutation` varchar(200) DEFAULT NULL,
  `name` varchar(50) NOT NULL,
  `surname` varchar(50) DEFAULT NULL,
  `street` varchar(50) NOT NULL,
  `street_number` varchar(5) NOT NULL,
  `postal_code` int(5) NOT NULL,
  `city` varchar(99) NOT NULL,
  `country` varchar(20) NOT NULL,
  `lat` float DEFAULT NULL,
  `lng` float DEFAULT NULL,
  `letter_salutation` text,
  `phonenumber_business` varchar(45) DEFAULT NULL,
  `mobilenumber_business` varchar(45) DEFAULT NULL,
  `phonenumber_private` varchar(45) DEFAULT NULL,
  `mobilenumber_private` varchar(45) DEFAULT NULL,
  `faxnumber_business` varchar(45) DEFAULT NULL,
  `faxnumber_private` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `website` varchar(200) DEFAULT NULL,
  `logo` varchar(100) DEFAULT NULL,
  `sepa_glid` varchar(45) DEFAULT NULL,
  `vat_number` varchar(45) DEFAULT NULL,
  `taxident_number` varchar(45) DEFAULT NULL,
  `tax_office` varchar(45) DEFAULT NULL,
  `important_day` date DEFAULT NULL,
  `important_day_text` varchar(45) DEFAULT NULL,
  `important_day_resubmissin` varchar(45) DEFAULT NULL,
  `important_day_reminder` tinyint(1) DEFAULT NULL,
  `notes` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

VIEW

在以下客户/注册VIEW中 - 我已创建此表单

<fieldset>
<?= $this->Form->create($client) ?>

<h2 id="content"><?= __('Firmendaten') ?></h2>
  <?= $this->Form->input('users.0.contact.company_indicator',   ['type' => 'hidden', 'value' => '1']); ?>
  <?= $this->Form->input('users.0.contact.company_name',        ['type' => 'text',     'label'=> false, 'placeholder' => __('Firmenname')]); ?>
  <?= $this->Form->input('users.0.contact.surname',             ['type' => 'text',     'label'=> false, 'placeholder' => __('Vorname')]); ?>
  <?= $this->Form->input('users.0.contact.name',                ['type' => 'text',     'label'=> false, 'placeholder' => __('Nachname')]); ?>

  <?= $this->Form->input('users.0.contact.street',              ['type' => 'text',     'label'=> false, 'placeholder' => __('Straße'),               'required' => true]); ?>
  <?= $this->Form->input('users.0.contact.street_number',       ['type' => 'text',     'label'=> false, 'placeholder' => __('Nr.'),                  'required' => true]); ?>
  <?= $this->Form->input('users.0.contact.postal_code',         ['type' => 'text',     'label'=> false, 'placeholder' => __('PLZ'),                  'required' => true]); ?>
  <?= $this->Form->input('users.0.contact.city',                ['type' => 'text',     'label'=> false, 'placeholder' => __('Stadt'),                'required' => true]); ?>
  <?= $this->Form->input('users.0.contact.country',             ['type' => 'text',     'label'=> false, 'placeholder' => __('Land'),                 'required' => true]); ?>

<h2 id="content"><?= __('Anwenderdaten') ?></h2>
  <?= $this->Form->input('users.0.email',                       ['type' => 'email',    'label'=> false, 'placeholder' => __('Email'),                'required' => true]); ?>
  <?= $this->Form->input('users.0.confirm_email',               ['type' => 'email',    'label'=> false, 'placeholder' => __('Email wiederholen'),    'required' => true]); ?>
  <?= $this->Form->input('users.0.password',                    ['type' => 'password', 'label'=> false, 'placeholder' => __('Passwort'),             'required' => true]); ?>
  <?= $this->Form->input('users.0.confirm_password',            ['type' => 'password', 'label'=> false, 'placeholder' => __('Passwort wiederholen'), 'required' => true]); ?>
  <?= $this->Form->input('service_contract_accepted',           ['type' => 'checkbox', 'label' => __('AGB'), 'default' => 0, 'hiddenField' => false, 'required' => true]); ?>
  <?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</fieldset>

CONTROLLER

这是在ClientsController.php

中处理的
public function register()
{
    $client = $this->Clients->newEntity();

    if ($this->request->is('post')) {
        $client = $this->Clients->patchEntity($client, $this->request->data, ['associated' => ['Users', 'Users.Contacts' ]]);

        if ($this->Clients->save($client)) {

            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['controller' => 'Contracts', 'action' => 'index']);
        } else {
            debug($client);

            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('client'));
    $this->set('_serialize', ['client']);
}

模型

我把它们都烤了......我们走了

class ClientsTable extends Table
{
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('clients');
    $this->displayField('name');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->hasMany('AccountAllocationsTodefine', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('AccountingPeriodDetails', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('AccountingPeriods', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Accounts', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('AccountsFrameworks', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('AllocationValuesTodefine', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('BankAccounts', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('ContactCategories', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Contacts', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Contracts', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Counter', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('CounterTypes', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('CounterValues', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('DataProperties', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('DataPropertyValues', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('DebitPosition', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Employees', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('EnergyCertificates', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Fees', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Files', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Floors', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('GlobalSettingsHeaders', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Heatings', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Objects', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Owners', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Postings', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('RentPrices', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('ServiceProviders', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('SettingsHeaders', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('SettingsValues', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('TemplateAccounts', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('TemplateAllocationFormulas', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Templates', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('TenantDeposits', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Tenants', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Units', [
        'foreignKey' => 'client_id'
    ]);
    $this->hasMany('Users', [
        'foreignKey' => 'client_id'
    ]);
}

public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->boolean('active')
        ->requirePresence('active', 'create')
        ->notEmpty('active');

    $validator
        ->date('service_paid')
        ->allowEmpty('service_paid');

    $validator
        ->boolean('service_contract_accepted')
        ->requirePresence('service_contract_accepted', 'create')
        ->notEmpty('service_contract_accepted');

    return $validator;
}
}


class UsersTable extends Table
{
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('users');
    $this->displayField('username');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsTo('Clients', [
        'foreignKey' => 'client_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Contacts', [
        'foreignKey' => 'contact_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Employees', [
        'foreignKey' => 'user_id'
    ]);
}

public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->email('email')
        ->requirePresence('email', 'create')
        ->notEmpty('email')
        ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

    $validator
        ->requirePresence('password', 'create')
        ->notEmpty('password');

    $validator
        ->allowEmpty('role');

    $validator
        ->allowEmpty('token');

    $validator
        ->dateTime('token_expires')
        ->allowEmpty('token_expires');

    $validator
        ->dateTime('activation_date')
        ->allowEmpty('activation_date');

    $validator
        ->boolean('active')
        ->allowEmpty('active');

    return $validator;
}

public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->isUnique(['email']));
    $rules->add($rules->existsIn(['client_id'], 'Clients'));
    $rules->add($rules->existsIn(['contact_id'], 'Contacts'));

    return $rules;
}
}



class ContactCategoriesTable extends Table
{
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('contact_categories');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsTo('Clients', [
        'foreignKey' => 'client_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('SettingsValues', [
        'foreignKey' => 'settings_value_id',
        'joinType' => 'INNER'
    ]);
}

public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->boolean('active')
        ->requirePresence('active', 'create')
        ->notEmpty('active');

    return $validator;
}

public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->existsIn(['client_id'], 'Clients'));
    $rules->add($rules->existsIn(['settings_value_id'], 'SettingsValues'));

    return $rules;
}
}

问题

一切正常,但DB中的Contacts.client_id始终设置为0。 我认为cakephp会自动处理参考ID并自动设置它们。

如何扩展我的代码以自动设置此client_id?

client_id确实在我的所有表格中,我需要自动设置它。

由于 比伦特

1 个答案:

答案 0 :(得分:0)

您缺少自动增量,只需在主键后添加AUTO_INCREMENT:

  CREATE TABLE clients
  (
     id int NOT NULL AUTO_INCREMENT,
     // other as you did
  )

并尝试更换此行:

$client = $this->Clients->patchEntity($client, 
$this->request->data, ['associated' => ['Users', 'Users.Contacts' ]]);

通过

$client = $this->Clients->patchEntity($client, 
$this->request->data, ['associated' => ['Users', 'Contacts' ]]); //