marcov4lente
5/12/2016 - 10:19 AM

Symfony Operations

Symfony Operations

Requests

$request->get('comment')

Responses

return View::create(null, Response::HTTP_CREATED);
return View::create(null, Response::HTTP_BAD_REQUEST);

##Doctrine

Query

$product = $this->getDoctrine()
  ->getRepository('AppBundle:Product')
  ->find($productId);

Create / Update

$product = new Product();
$product->setName('Keyboard');
$product->setPrice(19.99);
$product->setDescription('Ergonomic and stylish!');

$em = $this->getDoctrine()->getManager();

// tells Doctrine you want to (eventually) save the Product (no queries yet)
$em->persist($product);

// actually executes the queries (i.e. the INSERT query)
$em->flush();

Query Results Limits

$result = $qb->getQuery()
  ->setFirstResult(0)
  ->setMaxResults(2);
  ->getResult();
            

Native Query

use Doctrine\DBAL\Connection

$conn = $this->getEntityManager()
            ->getConnection();
            
$sql = "SELECT * FROM articles WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, $id);
$stmt->execute();

OR

$em = $this->getEntityManager();
  $rsm = new ResultSetMapping();

  $query = $em->createNativeQuery('
    SELECT * FROM wb_trainers_racehorses wtr
    LEFT JOIN wb_hnd_horses
    ON wb_hnd_horses.id = wb_trainers_racehorses.animalId
    WNERE wtr = :id
    ', $rsm);



  $query->setParameter($id, 'id');
  $records = $query->getResult();

Manual Doctrine Join

$qb = $this->entityManager->createQueryBuilder();
$qb->select('a', 'u')
  ->from('Credit\Entity\UserCreditHistory', 'a')
  ->leftJoin(
    'User\Entity\User',
    'u',
    \Doctrine\ORM\Query\Expr\Join::WITH,
    'a.user = u.id'
    )
    ->where('u = :user')
    ->setParameter('user', $users)
    ->orderBy('a.created_at', 'DESC');

return $qb->getQuery()->getResult();

Get Raw Query

$qb = $this->createQueryBuilder('a');
$query = $qb->getQuery();

// Show SQL
$sql = $query->getSQL();

// Show Parameters
$parameters = $query->getParameters();