Skip to content

Instantly share code, notes, and snippets.

View hmic's full-sized avatar

Hans-Joachim Michl hmic

  • Living in China/Shenzhen, from Germany, Hessen, nearby Frankfurt/Main
View GitHub Profile
@hmic
hmic / QueueShell.php
Last active February 2, 2017 15:41
Queuewrapper Testapp
<?php
namespace App\Shell;
use Cake\Console\Shell;
use Josegonzalez\CakeQueuesadilla\Queue\Queue;
/**
* Queue shell command.
*/
class QueueShell extends Shell
@hmic
hmic / QueueWrapper.php
Last active February 2, 2017 15:40
Wrapper class to allow arbitrary number of arguments to jobs with queuesadilla
<?php
namespace App\Lib;
class QueueWrapper
{
static function extract($job)
{
$callable = $job->data('callable');
if(is_array($callable) && count($callable) == 2) {
$instance = new $callable[0];
@hmic
hmic / UsersController.php
Last active June 20, 2016 15:28
cakephp3 - use Controller::isAuthorized to authorize access to actions depending on auth user
namespace App\Controller;
class UsersController extends AppController
{
public function isAuthorized($user = null)
{
$action = $this->request->params['action'];
// admin users are allowed all actions
<?php
namespace App;
use Cake\Event\Event;
use Cake\I18n\I18n;
use Cake\Routing\DispatcherFilter;
use Locale;
class LocaleSessionFilter extends DispatcherFilter
{
public function initialize() {
// ...
$this->loadModel('Historico');
// ...
}
public function add()
{
$solicitud = $this->Solicitud->newEntity();
if ($this->request->is('post')) {
@hmic
hmic / template.ctp
Created December 4, 2015 10:15
Using CakePHP3 StringTemplat
<?php $this->start('registration')?>
<?php $template = new StringTemplate(['registration' => $attendee->event->registration_ctp])?>
<?= $template->format('registration', Hash::flatten($attendee->toArray()))?>
<?php $this->end()?>
<?= $this->fetch('registration') ?>
@hmic
hmic / MyController.php
Last active November 6, 2015 11:03
Using beforeFilter Event with custom eventListener class
<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Controller\MyEventListener;
class MyController extends AppController {
public function initialize() {
// this sets up the eventManager, loads Components and the like.
<?php
namespace App\Model\Table;
trait saveAllTrait {
public function saveAll($entities = [], $options = null) {
if($options === null) {
return array_map([$this, 'save'], $entities);
}
$saved_entities = [];
@hmic
hmic / atable.php
Created October 20, 2015 08:39
cakephp3, raw query to update 2 tables
<?php
$this
->connection()
->newQuery()
->update('registrations, employees')
->set(['registrations.employee_id = employees.id'])
->where([
'registrations.employee_id IS NULL',
'registrations.employee_number = employees.employee_number',
])
@hmic
hmic / CompaniesController.php
Last active October 19, 2015 14:42
soft fail paginate (Cakephp 3)
<?php
use App\Controller;
class CompaniesController extends AppController {
public function index() {
try {
$this->set('companies', $this->paginate($this->Companies->find()));
} catch(\Cake\Network\Exception\NotFoundException $e) {
return $this->redirect(['page' => 1] + $this->request->query);