Navigation Menu

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 / hash.h
Created January 25, 2012 14:03
php hash function "from the source"
/*
* DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
*
* This is Daniel J. Bernstein's popular `times 33' hash function as
* posted by him years ago on comp.lang.c. It basically uses a function
* like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
* known hash functions for strings. Because it is both computed very
* fast and distributes very well.
*
* The magic of number 33, i.e. why it works better than many other
@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" />
<?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 / 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 / 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 / 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' => '',