0

I have this database table:

CREATE TABLE IF NOT EXISTS `businesses` (
`id` int(10) unsigned NOT NULL,
  `record_id` int(10) unsigned DEFAULT NULL,
  `businesstype_id` int(10) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=1 ;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `businesses`
--
ALTER TABLE `businesses`
 ADD PRIMARY KEY (`id`), ADD KEY `FK_businesses_records` (`record_id`), ADD KEY `FK_businesses_businesstypes` (`businesstype_id`);

It has innodb engine and uses foreign keys.

The controller code at src/Controller/BusinessesController add

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use App\Model\Entity\Record;

/**
 * Businesses Controller
 *
 * @property App\Model\Table\BusinessesTable $Businesses
 */
class BusinessesController extends AppController {

/**
 * Index method
 *
 * @return void
 */
    public function index() {
        $this->paginate = [
            'contain' => ['Records', 'Businesstypes']
        ];
        $this->set('businesses', $this->paginate($this->Businesses));
    }

/**
 * View method
 *
 * @param string $id
 * @return void
 * @throws \Cake\Network\Exception\NotFoundException
 */
    public function view($id = null) {
        $business = $this->Businesses->get($id, [
            'contain' => ['Records', 'Businesstypes']
        ]);
        $this->set('business', $business);
    }

/**
 * Add method
 *
 * @return void
 */
    public function add() {
        //$business = $this->Businesses->newEntity($this->request->data);
        $records = TableRegistry::get('Records');
        $entity = $records->newEntity($this->request->data(),[
            'associated' => ['Businesses.Businesstypes', 'Emails','Telephones.Telephonetypes','Addresses']
        ]);
        if ($this->request->is('post')) {
                    if ($records->save($entity)) {
                        $this->Flash->success('The b usiness has been saved.');
                        return $this->redirect(['action' => 'index']);
                    } else {
                        $this->Flash->error('The business could not be saved. Please, try again.');
                    }
            // if ($this->Businesses->save($business)) {
            //     $this->Flash->success('The business has been saved.');
            //     return $this->redirect(['action' => 'index']);
            // } else {
            //     $this->Flash->error('The business could not be saved. Please, try again.');
            // }
        }
        $telephonetypes = $this->Businesses->Records->Telephones->Telephonetypes->find('list');
        $records = $this->Businesses->Records->find('list');
        $businesstypes = $this->Businesses->Businesstypes->find('list');
        $this->set(compact('telephonetypes','entity', 'records', 'businesstypes'));
    }

And finally my form at /src/Template/Businesses/add.ctp

<?=$this->element('business_actions')?>
<div class="businesses form large-10 medium-9 columns">
<?= $this->Form->create($entity) ?>
    <fieldset>
        <legend><?= __('Add Business') ?></legend>
    <?php
        echo $this->Form->input('Records.name');
        echo $this->Form->input('Records.url');
        echo $this->Form->input('Records.description');
        echo $this->Form->input('Business.businesstype_id', ['options' => $businesstypes]);
        echo $this->Form->input('Emails.email');
        echo $this->Form->input('Telephones.telephonetype_id', ['options' => $telephonetypes]);
        echo $this->Form->input('Telephones.telephone');
        echo $this->Form->input('Addresses.streetAddress');
        echo $this->Form->input('Addresses.addressLocality');
        echo $this->Form->input('Addresses.postalCode');
        echo $this->Form->input('Addresses.addressCountry');
        echo $this->Form->input('Addresses.description',['label'=>__('Address Description')]);
    ?>
    </fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

However when I try to save, I get "The business could not be saved. Please, try again." error. When I check sql log, I can't see anything than "BEGIN, ROLLBACK"

Any help will be appreciated.

UPDATE: Saving related data requires form like this:

<?=$this->element('business_actions')?>
<div class="businesses form large-10 medium-9 columns">
<?= $this->Form->create($entity) ?>
    <fieldset>
        <legend><?= __('Add Business') ?></legend>
    <?php
        echo $this->Form->input('name');
        echo $this->Form->input('url');
        echo $this->Form->input('description');
        echo $this->Form->input('businesses.0.businesstype_id', ['options' => $businesstypes]);
        echo $this->Form->input('emails.0.email');
        echo $this->Form->input('telephones.0.telephonetype_id', ['options' => $telephonetypes]);
        echo $this->Form->input('telephones.0.telephone');
        echo $this->Form->input('addresses.0.streetAddress');
        echo $this->Form->input('addresses.0.addressLocality');
        echo $this->Form->input('addresses.0.postalCode');
        echo $this->Form->input('addresses.0.addressCountry');
        echo $this->Form->input('addresses.0.description',['label'=>__('Address Description')]);
    ?>
    </fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
4

0 回答 0