Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Other stuff.
*
* @ORM\Table("user_extra")
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks()
*/
class LazyUserData
<?php
// in the controller
$user = Customer::isCustomer($this->getUser());
// in the entity
/**
* @param UserInterface $user
*
* @return Customer|null
@ABM-Dan
ABM-Dan / stuff.php
Last active February 22, 2016 17:07
Embedded Form shenanigans
<?php
// in the controller
$user->addCreditCard(new CreditCard());
// this actually shows up in the form.
$user->getCreditCards()[0]->setCvv(123);
// in the outer form type
$builder->add(
'credit_card',
CollectionType::class,
<?php
class Parent{
private static $attribute;
public getAttribute(){
return self::$attribute;
}
}
<?php
namespace MyBundle\Form\Type;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
class MyFormType extends AbstractType implements ContainerAwareInterface
mybundle.repository.mymodelrepo:
class: MyBundle\Repository\MyModelRepository
calls:
- [ unpack, [@=service('snc_redis.default').get('MyModel')] ]
<?php
$qb = $this->createQueryBuilder('l')
->select('l, c')
->join('l.country', 'c')
->where($expr->in('l.id', '?1'))
->setParameter(1, $ids);
$qb = $this->createQueryBuilder('l')
->leftJoin('l.sales', 's')
->where('l.is_active = 1')
->andWhere('s.sale_ts = (SELECT MAX(s2.sale_ts) FROM MyBundle:Sale s2 WHERE s2.location = l)')
->orderBy('l.id', 'ASC');
@ABM-Dan
ABM-Dan / qb.php
Last active January 27, 2016 22:27
<?php
// Trying to emulate
// SELECT l.*, MAX(s.sale_ts) FROM location AS `l` LEFT JOIN sales AS `s` ON l.id = s.location_id GROUP BY(s.location_id);
// But the group selects the last result.
$qb = $this->createQueryBuilder('l')
->leftJoin('l.sales', 's')
->where('l.is_active = 1')
->orderBy('l.id', 'ASC')
->addOrderBy('s.sale_ts', 'DESC')
->groupBy('l.id');
SELECT l.*, MAX(s.sale_ts) FROM location AS `l` LEFT JOIN sales AS `s` ON l.id = s.location_id GROUP BY(s.location_id);