Skip to content

Instantly share code, notes, and snippets.

View lorenzo's full-sized avatar

José Lorenzo Rodríguez lorenzo

View GitHub Profile
<?php
namespace App\Controller\Component;
use Cake\Controller\Component\PaginatorComponent;
use Cake\Datasource\RepositoryInterface;
use Cake\Network\Exception\NotFoundException;
use Cake\ElasticSearch\Query;
class ElasticPaginatorComponent extends PaginatorComponent
@lorenzo
lorenzo / 0_reuse_code.js
Last active August 29, 2015 14:21
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
<?php
use Cake\Core\Plugin;
use Cake\Routing\Router;
Router::defaultRouteClass('Route');
$languageCodes = array('fr_FR', 'pt_BR');
$builder = function ($routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
<?php
$data = $this->Table->find()->all();
$file = new \SplFileObject(TMP . "my_export.csv", 'w');
$title = function ($row) use ($file) {
static $done = false;
if (!$done) {
$file->fputcsv(array_keys($row));
$done = true;
}
<?php
require 'vendor/autoload.php';
$connection = new PDO(getenv('MYSQL'));
$looper = function () use ($connection) {
foreach ([1, 2, 3] as $number) {
yield $connection->query('select * from categories');
}
};
$list = (new \Cake\Collection\Collection($looper()))->map(function($r) { return $r; });
iterator_to_array($list, false);
@lorenzo
lorenzo / gist:78c89a0a176410c68db9
Created March 3, 2015 20:00
default_order.php
public function beforeFind ($event, $query, $options, $primary) {
$order = $query->clause('order');
if ($order === null || !count($order)) {
$query->order('my_column');
}
}
public function assetUrl($path, $options = array()) {
if (is_array($path)) {
return $this->url($path, !empty($options['fullBase']));
}
if (strpos($path, '://') !== false) {
return $path;
}
if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
public function buildRules(RulesChecker $rules) {
$rules->add(
function ($entity) {
$errors = $this->validator('special')->errors($entity->toArray(), $entity->isNew());
$entity->errors($errors);
return !$errors;
});
return $rules;
}
public function getowner() {
$id = $_GET['id'];
$data = $this->Powners->get($id);
$this->response->body(json_encode($data));
return $this->response;
}
$article = $this->Articles->newEntity($this->request->data);
$this->Articles->save($article, ['validate' => 'myCustomValidatorName']);