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 / fill.ctp
Created November 24, 2015 09:26 — forked from mnithya/fill.ctp
<div class="container">
<div class="form-group">
<?= $this->Form->create($assessmentForm) ?>
<fieldset>
<legend><?= __($assessmentForm->title) ?></legend>
<?php foreach ($questions as $k => $question): ?>
<?php echo $this->Form->hidden("$k.qid", ['value'=> $question->qid]) ?>
<?php echo $this->Form->input("$k.answer_text", ['label' => $question['text']]) ?>
<?php endforeach; ?>
</fieldset>
<?php
$date = new DateTime();
$someClass->findTimeFrame($date);
$otherClass->doStuff($date);
function findTimeFrame($fromDate) {
$this->conditions('from_date', '>=', $date);
...
<?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 / file.ctp
Last active October 29, 2015 08:09
<?php
$this->Html->link($artist->name, ['action' => 'view', $artis->id, 'name' => $artist->name]);
Table
public function validationDefault(Validator $validator)
{
$validator
->requirePresence('device_id', 'create')
->notEmpty('device_id');
return $validator;
}
<?php
collection($items)
// I only care about elements having the id = a or id = b
->filter(function ($value, $key) use ($array) {
return isset($array[$value['id']]); // If the key is in the array, the it should be kept in the collection
})
/// Let's now index the collection by the id
->groupBy('id')
@lorenzo
lorenzo / gist:210fc430a0afe6469fc2
Last active September 19, 2015 12:49 — forked from fly2279/gist:4edee4cb14e35c133806
Query with belongsToMany and belongsTo association
<?php
$query = $this->Articles->find('all')
->leftJoinWith('ArticlesAuthors')
->leftJoinWith('Authors')
->where(['Authors.id' => 1])
->orWhere(['ArticlesAuthors.author_id' => 1]);
<?php
namespace App\Database\Type;
use Cake\Database\Type\DateTimeType;
/**
* Datetime type converter.
*
* Use to convert datetime instances to strings & back.
*/
<?php
/**
* Configures authentication and the basic fields to show on each screen
*
* @param Event $event The given event
* @return void
*/
public function beforeFilter(Event $event)
{
if ($this->request->public) {