Dostęp do innej tabeli z Modelu

W modelu Table nie można użyć metody $this->fetchTable(„Projects”)  – ta metoda dostępna jest tylko dla Controllers.

src/Model/Table/InvoicesTable.php

use Cake\ORM\TableRegistry;

class InvoicesTable extends Table
{

  public function getProjects() { 
    
    $projects = TableRegistry::getTableLocator()->get('Projects')
                  ->find()
                  ->where(['is_active' => 1])
                  ->all();
    ....
  } // getProjects()
...

} // class

CakePHP 5 – Formularz – Powiązania modeli

$this->Form->create($article);

// Article controls.
echo $this->Form->control('title');

// Author controls (belongsTo)
echo $this->Form->control('author.id');
echo $this->Form->control('author.first_name');
echo $this->Form->control('author.last_name');

// Author profile (belongsTo + hasOne)
echo $this->Form->control('author.profile.id');
echo $this->Form->control('author.profile.username');

// Tags controls (belongsToMany)
// as separate inputs
echo $this->Form->control('tags.0.id');
echo $this->Form->control('tags.0.name');
echo $this->Form->control('tags.1.id');
echo $this->Form->control('tags.1.name');

// Inputs for the joint table (articles_tags)
echo $this->Form->control('tags.0._joinData.starred');
echo $this->Form->control('tags.1._joinData.starred');

// Comments controls (hasMany)
echo $this->Form->control('comments.0.id');
echo $this->Form->control('comments.0.comment');
echo $this->Form->control('comments.1.id');
echo $this->Form->control('comments.1.comment');

$this->Form->end();

Czytaj dalej CakePHP 5 – Formularz – Powiązania modeli

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, ".", " ") ?>