cakephp 3 commonly used code snippets

Find conditions

$data = $this->ProductComponents->find('all')
	->contain(['ProductComponentGroups','ProductGroups'])
	->select([
		'id', 'name', 'created', 'modified', 'product_component_group_id', 'product_group_id', 'ProductComponentGroups.id', 'ProductComponentGroups.name','ProductGroups.name'
	])
	->where([$conditions])	
	->order($order)
	->limit($limit)
	->page($page)->toArray();


Table Date time formating

use Cake\I18n\Time;		
foreach ($data as &$row) {
	$created = Time::parse($row->created);
	$row['created'] = $created->i18nFormat('yyyy-MM-dd HH:mm:ss');
	$modified = Time::parse($row->modified);
	$row['modified'] = $modified->i18nFormat('yyyy-MM-dd HH:mm:ss');
}

 

Find condition with anonymous funtion using a contain()

use Cake\ORM\Query;
$data = $this->EmailBroadcastQueues->find()
			->where(['EmailBroadcastQueues.product_broadcast_id' => $logId ])
		   ->contain('Clients', function (Query $q) {
					return $q->select(['name']);
			})
			->order(['EmailBroadcastQueues.id desc'])->toArray();