0

Cake PHP Version: 3.1.5

I have a problem with saving a hasOne association, which works fine on one table but not with a second.

Ticketsand Cashdrafts are related to Cashpositions in a belongsTo relations. Cashpositions holds two FK for their id. So when a new cashposition is auto-created it holds either a ticket_id or a cashdraft_id. The second FK will be null.

The thing is, that the Tickets-Cashpositions saving is working fine, so everytime a ticket is created a related cashposition is created. But it is not working with Cashdrafts-Cashpositions. I don't understand why, because the setup and relations are exactly the same.

Here is the setup:

class CashpositionsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsTo('Tickets', [
           'foreignKey' => 'ticket_id'
        ]);
        $this->belongsTo('Cashdrafts', [
           'foreignKey' => 'cashdraft_id'
        ]);
    }
}
class TicketsTable extends Table
{
    public function initialize(array $config)
    {
        $this->hasOne('Cashpositions', [
            'foreignKey' => 'ticket_id'
        ]);
    }
}
class CashdraftsTable extends Table
{
    public function initialize(array $config)
    {
        $this->hasOne('Cashpositions', [
            'foreignKey' => 'cashdraft_id'
        ]);
    }
}

And then in the controllers add() functions:

class TicketsController extends AppController
{
    public function add($memberId = null)
   {
      $ticket = $this->Tickets->newEntity();
      if ($this->request->is('post')) {

        $ticket = $this->Tickets->patchEntity($ticket, $this->request->data, [
           // working fine: creates new cashposition for this ticket
            'associated' => ['Cashpositions']
        ]);

        if ($this->Tickets->save($ticket)) {
            $this->Flash->success(__('ticket saved'));
            return $this->redirect(['action' => 'view', $ticket->$id]);
        } else {
            $this->Flash->error(__('ticket could not be saved'));
        }
    } 
class CashdraftsController extends AppController
{
    public function add()
    {
    $cashdraft = $this->Cashdrafts->newEntity();
    if ($this->request->is('post')) {

        $cashdraft = $this->Cashdrafts->patchEntity($cashdraft, $this->request->data,[
          // fail: no associated record created
         'associated' => ['Cashpositions']
      ]);

     if ($this->Cashdrafts->save($cashdraft)) {            
            $this->Flash->success(__('cashdraft saved.'));
            return $this->redirect(['action' => 'view', $cashdraft->id]);
     } else {
            $this->Flash->error(__('cashdraft could not be saved'));
        }
    }
}

I debugged the $ticket and $cashdraft. But I cannot say I understand the output because:

The array for the ticket will show every related data but no cashposition, although a new record for it was created successfully...

And the array for the new cashdraft where the related cashposition is NOT created will look like this and say "null" for it:

object(App\Model\Entity\Cashdraft) {

'id' => (int) 10,
'amount' => (float) -7,
'created' => object(Cake\I18n\Time) {

    'time' => '2015-12-13T20:03:54+0000',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'modified' => object(Cake\I18n\Time) {

    'time' => '2015-12-13T20:03:54+0000',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'cashposition' => null,  // this part not even showing up for a "ticket" in the debug
'[new]' => false,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Cashdrafts'

}

In the SQL in DebugKit I can see that for the ticket an INSERT into the related cashpositions table is done. But for cashdrafts there is no INSERT done into the related table. So obviously Cake does not even try to create the associated record.

I'm really out of ideas now! In the database itself both FKs are set up exactly the same, names are correct etc.

Does anybody have an idea what the problem could be or where I could further search for the cause of the second association not working? Thanks!

eve
  • 247
  • 6
  • 16

1 Answers1

2

Ok, so after searching for a million hours I finally realized, that the problem was not with the Model or Controller like I thought. It was (just) the view and the request data not being complete. Somehow I thought Cake would magically add the entity for the association if non exists even if there is no input for it ;)

In the tickets table for which the saving worked I had an empty input field for a column in Cashpositions that does not even exist anymore and I just hadn't deleted it yet, but it did the trick (don't ask me why).

To fix it now I just put in a hidden input field for the association cashposition.ticket_id and cashposition.cashdraft_id in the add.ctp view for both tables that stays empty. Now the request data contains the array for the association and auto creates a new cashposition with the matching FK every time a new ticket or cashdraft is added.

<!-- Cashdrafts/add.ctp -->

<?php echo $this->Form->input(
        'cashposition.cashdraft_id', [
                'label' => false,
                'type' => 'hidden'
        ]) ?>

Since I'm just a beginner with this I don't know if this is the best way to go, but it works (finally...)

eve
  • 247
  • 6
  • 16