Skip to content

Instantly share code, notes, and snippets.

@holisticnetworking
Last active November 2, 2016 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holisticnetworking/dcad57efc4cbdd262a9fe0e9d32e66ff to your computer and use it in GitHub Desktop.
Save holisticnetworking/dcad57efc4cbdd262a9fe0e9d32e66ff to your computer and use it in GitHub Desktop.
Ok. Working with HABTM table. Payment Transactions belong to many Invoices. But trying to assign the association between a single payment and multiple invoices using the link() function doesn't seem to be working. Here is my code:
/*
* Apply Payments to Invoices
*/
public function applyPayments() {
$this->loadModel('PaymentMethods');
$invoices = $this->Invoices
->find( 'summary' )
->where([ 'Invoices.id IN' => $this->request->data( 'invoices' ) ]);
// Add totals:
$totals = $this->InvoiceTotals->doTotals( $invoices );
extract( $totals );
$payment_methods = $this->PaymentMethods->find('list', [
'keyField' => 'shortname',
'valueField' => 'name'
]);
if ( $this->request->data( 'apply_payment' ) ) {
$payment = $this->Invoices->PaymentTransactions->newEntity();
$payment = $this->Invoices->PaymentTransactions->patchEntity( $payment, $this->request->data );
if( $new = $this->Invoices->PaymentTransactions->save($payment) ) {
// die( print_r( $invoices ) );
$invoices->each( function( $invoice, $key ) use ( $new ) {
$this->Invoices->PaymentTransactions->link( $invoice, [$new] );
} );
$this->Flash->success(__('The payment has been saved.'));
$this->set( 'new', $new->id );
// return $this->redirect(['action' => 'applyPayments', $new->id]);
} else {
$this->Flash->error(__('The invoice could not be saved. Please, try again.'));
}
}
$incoming = $this->request->data( 'invoices' );
$this->set( compact( 'incoming', 'invoices', 'batch', 'payment_methods' ) );
$this->viewBuilder()->layout('partial');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment