Skip to content

Instantly share code, notes, and snippets.

@estheban
Created March 3, 2016 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estheban/3eae41271f6cf5f3180a to your computer and use it in GitHub Desktop.
Save estheban/3eae41271f6cf5f3180a to your computer and use it in GitHub Desktop.
<?php
namespace Elcweb\LegacyBundle\Entity;
/**
* Class RawSQLTrait.
*/
trait RawSQLTrait
{
/**
* @param string $sql
*
* @return array
*
* @throws \Doctrine\DBAL\DBALException
*/
private function fetchAll($sql)
{
return $this->execute($sql)->fetchAll();
}
/**
* @param string $sql
*
* @return mixed
*/
private function execute($sql)
{
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
return $stmt;
}
/**
* @param string $sql
* @param string $fieldName
*
* @return mixed
*
* @throws \Doctrine\DBAL\DBALException
*/
private function fetchSingleField($sql, $fieldName)
{
return $this->fetch($sql)[$fieldName];
}
/**
* @param string $sql
*
* @return array
*
* @throws \Doctrine\DBAL\DBALException
*/
private function fetch($sql)
{
return $this->execute($sql)->fetch();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment