Sortowanie, Limity

$this->paginate = [
   'limit' => 200,
   'maxLimit' => 200,
   'sortableFields' => [
      'date', 'netto', 'brutto', 'termin_platnosci', 'is_payed',
      'Brands.name', 'Projects.number', 'numer', 'position',
      'Companies.name', 'Companies.nip', 'Companies.bank_number',
    ],
   'order' => ['CostInvoices.date' => 'DESC']
];

Różne: Tabela, wyszukiwanie Ajax,

Referer – redirect – queryParams

if($this->request->referer()) {
    return $this->redirect($this->request->referer());
} else {
    return $this->redirect(['action' => 'articles', '?' => $this->request->getQueryParams()]);
}

get Table object

$project = $this->fetchTable("Projects")->get($id);   // załaduj Model

URL

let url = new URL(location.href);

$('#s_brand_id').on('change', function(){
  var s_brand_id = $(this).val();

  if(s_brand_id) {
    url.searchParams.set('s_brand_id', s_brand_id);
  } else {
    url.searchParams.delete('s_brand_id');
  }
  location.href = url.toString();
});
$s_brand_id = (int) $this->request->getQuery('s_brand_id', null);

if($s_brand_id) {   
  $query->where(['brand_id' => $s_brand_id]);
}

$this->set(compact('s_brand_id'));

Wirtualne pola

Pobranie największej/najwcześniejszej lub namniejszej/najpóźniejszej wartości

protected $_accessible = [
   ......
   'schedule_first_date' => true,    // Pierwsza data w harmonogramie
   'schedule_last_date' => true,     // Ostatnia data w harmonogramie
];

// Dla projektu pobiera ostatnią datę z harmonogramu (schedules - tablica powiązanych rekordów)
protected function _getScheduleLastDate()
{
   if (isset($this->_fields['schedule_last_date'])) {
      return $this->_fields['schedule_last_date'];
   }
   if (empty($this->schedules)) { return ; }

   $last_day = (new Collection($this->schedules))->max('date_to')->date_to;
   return $last_day;
}  

// Dla projektu pobiera ostatnią datę z harmonogramu (schedules)
protected function _getScheduleFirstDate()
{
  if (isset($this->_fields['schedule_first_date'])) {
       return $this->_fields['schedule_first_date'];
  }
  if (empty($this->schedules)) { return ; }

  $first_fday = (new Collection($this->schedules))->min('date_from')->date_from;
  return $first_fday;
}

Czytaj dalej Wirtualne pola

Formatowanie wartości finansowych

Entity – usuwanie spacji, zamiana przecinków na kropki

protected function _setNetto($netto) {
   $netto = str_replace(',', '.', $netto);
   return trim($netto);
}

protected function _setBrutto($brutto) {
   $brutto = str_replace(',', '.', $brutto);
   return trim($brutto);
}

Kontroler  – pobieranie sumy pozycji

$query = $this->Invoices->find();

$query->enableAutoFields()
      ->select([
          'total_netto'  => $query->func()->sum('Positions.netto'),
          'total_brutto' => $query->func()->sum('Positions.brutto'),
          'total_pos'    => $query->func()->count('Positions.id')
       ]);

$query->leftJoinWith('Positions');

$query->group(['Invoices.id']);

$query->order(['Invoices.id' => 'DESC'])
      ->all();

View /

<?= number_format($brutto, 2, ".", " ") ?>