I have lots of notes in my local files and also I plan to move them to gists.
// TODO: improve&refactor
// symfony convert request body to array:
function someAction(Request $request){
// x-www-form-urlencoded:
$wwwForm = parse_str($request->getContent(), $data);
// json:
$json = json_decode($request->getContent(), true);
// form-data:
$formData = $request->request->all();
}
pass data to jms serializer:
// in controller:
$context = $view->getContext();
$context->setAttribute('answer', 42);
// in listener:
$answer = $event->getContext()->attributes->get('answer')->getOrElse(null);
// or
$attrs = $event->getContext()->attributes->all();
queryBuilder order by object
$qb->addSelect('IDENTITY(object.category) as HIDDEN object___category');
$qb->orderBy('object___category', 'ASC');
php convert to t format with tz:
$datetime->format('c');
// doesn't work:
$d->setTimezone(DateTimeZone::UTC);
$d->setTimezone(new \DateTimeZone(DateTimeZone::UTC));
// correct:
$d->setTimezone(new \DateTimeZone('UTC'))
function constant_exists($class, $name){
if(is_string($class)){
return defined("$class::$name");
} else if(is_object($class)){
return defined(get_class($class)."::$name");
}
return null;
}
virtualbox-ndb:
- once:
- sudo modprobe nbd
- mount:
- sudo qemu-nbd -c /dev/nbd0 $vdi
- sudo mount /dev/nbd0p$i $mnt
- umount:
- sudo umount $mnt
- sudo qemu-nbd -d /dev/nbd0
easy filesystem sandbox using aufs+chroot it can be useful to prevent garbage in your system fs so you can sudo make install without spoiling your system
# you should be root here
# you can be in any folder
mkdir box root
mount -t aufs -o br=box:/ none root
cd root
# optional or if needed by program
mount --bind /proc proc
mount --bind /sys sys
mount --bind /dev dev
# then do it!
chroot .
symfony render twig:
// from string template
$template = $container->get('twig')->createTemplate('Hello {{ name }}');
$result = $template->render(array('name'=>'World'));
// from file template:
/** @var \Symfony\Bundle\TwigBundle\TwigEngine $templating */
$templating = $container->get('templating');
$html = $templating->render('MyBundle:folder:template.html.twig', $params);
mysql import data to file:
SELECT col FROM table WHERE 1 INTO OUTFILE '/absolute/path'
!!file will be created by mysql server, not client
default path is defined by server, so use abs path
some mysql users may not have rights to do it
mysql -h db_host --execute "SELECT * FROM my_table" --silent --raw > outfile
queryBuilder order by object
$qb->addSelect('IDENTITY(object.category) as HIDDEN object___category');
$qb->orderBy('object___category', 'ASC');
Docrine search elements have at least 1 child:
$qb = $em->createQueryBuilder()
// ->select("COUNT(p.id) as num")
->select("p")
->from($parentClass, 'p')
->join('p.children', 'children')
;
how to add all extensions from beberlei/DoctrineExtensions add to config.yml::imports:
- { resource: '../../vendor/beberlei/DoctrineExtensions/config/mysql.yml' }
mysql logic...
SELECT (t.x * t.x) as calculated
FROM tablename t
WHERE calculated < 1000
doesn't work
you should replace WHERE
with HAVING
TOASK: is it safe to pass user data to DateTime constructor asked https://stackoverflow.com/questions/36984316/is-it-safe-to-construct-datetime-from-get yes, it's safe