Search code examples
phpsqlcodeignitersymfonypropel

custom query with propel symfony 2


This is my first time using symfony 2. For database integration i am thinking of using propel as I doctrine and annotations seems really difficult for me. But it seems to me that to make a query you have to use propels own functions. I have used codeigniter. In codeigniter I used to send query string and it used to send me data. Is there something similar in propel symfony 2? Like -

$query = 'select * from table where column1 natural join column2';
$this->db->query($query);

Solution

  • You should look at docs of sf2:
    http://symfony.com/doc/current/book/propel.html

    If you want to use raw SQL:

    $em = $this->getDoctrine()->getEntityManager();
    $connection = $em->getConnection();
    $statement = $connection->prepare("SELECT something FROM somethingelse");
    $statement->execute();
    $results = $statement->fetchAll();
    

    Or "propel way":

    $connection = Propel::getConnection();
    $query = 'SELECT MAX(?) AS max FROM ?';
    $statement = $connection->prepareStatement($query);
    $statement->setString(1, ArticlePeer::CREATED_AT);
    $statement->setString(2, ArticlePeer::TABLE_NAME);
    $resultset = $statement->executeQuery();
    $resultset->next();
    $max = $resultset->getInt('max');