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
<?php
namespace App\Listener;
use Cake\Utility\Inflector;
use Crud\Listener\BaseListener;
use Crud\Event\Subject;
class ContainedModelsListener extends BaseListener {
@hmic
hmic / View.php
Created February 2, 2018 14:07
$registry->normalizeArray() broken
<?php
$registry = $this->helpers();
$helpers = $this->helpers;
pr($helpers);
/*
Array(
[CrudView] => Array(
[className] => App\View\Helper\CrudViewHelper
)
)
@hmic
hmic / ParticipantsTable.php
Created December 19, 2017 15:38
CakePHP 3.x Table::beforeMarshall
<?php
class ParticipantsTable extends AppTable
{
public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options) {
if(isset($data['partner']) && is_array($data['partner'])) {
$participant = $this->get($data['id']);
$data['partner']['participant_type_id'] = $participant->type_id;
}
}
@hmic
hmic / UpdateCounterCache.php
Last active April 19, 2022 00:06
Cake3 update counter caches for association
<?php
namespace App\Model\Table\Behavior;
trait UpdateCounterCacheTrait {
public function cleanupBelongsToMany($return_count = null) {
if($return_count) {
return $count;
}
return $this;
@hmic
hmic / httpPostData.php
Last active September 24, 2021 08:38
PHP post data and file - multipart-form-data
<?php
/*
* use like:
* httpPostDataFile(
* 'https://www.google.com/search', // the url to post to, optionally including get parameters like: ?this=that&here=there
* [
* 'q' => 'php http upload file', // post data like: <input name="q" value="php http upload file" />
* ...
* ], [
* 'image' => [ // the input name used like: <input name="image" type="file" />
@hmic
hmic / ATable.php
Last active November 6, 2017 18:19
CakePHP, dependent, multiple fields, validation example
<?php
class ATable extends \Cake\ORM\Table
{
public function validationDefault(Validator $validator)
{
$fields = ['salutation', 'firstname', 'lastname'];
$validator
->provider('allEmptyOrFilled', $fields);
foreach($fields as $field) {
@hmic
hmic / AppController-fixed.php
Created August 31, 2017 09:28
Events and Inheritance in cakephp3
<?php
namespace App\Controller;
class AppController extends \Cake\Controller\Controller
{
public function initialize()
{
parent::initialize();
/*
* Can't listen on the "initialize" event here obviously, "startup" and all later ones do work though.
@hmic
hmic / amodel.php
Created August 10, 2017 13:59
hasMany association on cake2 with *very* custom finder example
<?php
class AModel {
var $hasMany = array(
'Field' => array(
'className' => 'Field',
'foreignKey' => '',
'dependent' => false,
'conditions' => '',
'fields' => '',
@hmic
hmic / AController.php
Last active August 16, 2017 10:22
paginate past data
<?php
namespace App\Controller;
class AController extends AppController {
public function index() {
try {
$this->set('data', $this->paginate());
} catch(\Cake\Network\Exception\NotFoundException $e) {
$this->redirect(['page' => 1] + $this->request->query);
}
@hmic
hmic / gist:98059450bac2b8dbb396b87d39e4ec4e
Created June 6, 2017 13:26
Adding blacklist actions to related entites für FoC/CrudView
In your controller action set it up like that:
$action->config('scaffold.relations_actions_blacklist', [
'Registrations' => ['edit', 'delete'], // only allow view action
'Rewards' => ['delete'], // don't allow delete
]);
Change/add this in the appropriate place in the Template/Element/view/relations/has_many.ctp which you have copied from the CrudView plugin to your own src/ dir:
foreach (${$viewVar}->{$details['entities']} as ${$otherSingularVar}) :
.