Doctrine DBAL database DB operations

0

To make bank operations better and to use (or which to use): Data Manipulation

Query-builder

To work with data manipulation?

And how would I do it to do bank operations with him for example:

#inserção no banco
$sql = INSERT INTO tcorrida(nome_tCorrida, classificacao_tCorrida, ref_tCorrida, meta_tCorrida) VALUES ('?, ?, ?, ?');
$pdo->bindValue(1, $nome_tCorrida); //variavel que vem do formulario
$pdo->bindValue(2, $classificacao_tCorrida);
$pdo->bindValue(3, $ref_tCorrida);
$pdo->bindValue(4, $meta_tCorrida);
$pdo->execute();

#Atualização no banco
UPDATE tcorrida SET nome_tCorrida='?',classificacao_tCorrida='?', ref_tCorrida='?',meta_tCorrida='?' WHERE id_tCorrida='?';
$pdo->bindValue(1, $nome_tCorrida); //variavel que vem do formulario
$pdo->bindValue(2, $classificacao_tCorrida);
$pdo->bindValue(3, $ref_tCorrida);
$pdo->bindValue(4, $meta_tCorrida);
$pdo->execute();

#Seleção de dados do banco
SELECT * FROM tcorrida WHERE id_tCorrida='?';
$pdo->bindValue(1, $id_tCorrida);
$pdo->execute();

#Deletar dados do banco
DELETE FROM tcorrida WHERE id_tCorrida='?';
$pdo->bindValue(1, $id_tCorrida);
$pdo->execute();

I'm starting in the doctrine dbal package, I'm reading the documentation, for example in the PDO I make the $pdo->bindValues call as in the example above, in the doctrine package how would I do these parameters?

    
asked by anonymous 03.09.2014 / 18:53

1 answer

1

You can use QueryBuilder to make it easier to create SQL, in your case it would look like this:

SELECT

$tcorrida = $qb
                ->select('*')
                ->from('tcorrida', 'c')
                ->where($qb->expr()->eq('c.id_tCorrida', ':id'))
                ->setParameter('id', 1)
                ->execute()->fetch()
;

INSERT (only available in dev-master)

$result = $qb
        ->insert('tcorrida')
        ->setValue('nome_tCorrida', ':nome_tCorrida')
        ->setParameter('nome_tCorrida', 'Aqui vai o nome da corrida')
        ->execute()
;

UPDATE

$result = $qb
        ->update('tcorrida', 'c')
        ->set('nome_tCorrida', ':nome_tCorrida')
        ->where($qb->expr()->eq('c.id_tCorrida', ':id_tCorrida'))
        ->setParameter('nome_tCorrida', 'Outro nome')
        ->setParameter('id_tCorrida', 1)
        ->execute()
;

DELETE

$result = $qb
        ->delete('tcorrida', 'c')
        ->where($qb->expr()->eq('c.id_tCorrida', ':id_tCorrida'))
        ->setParameter('id_tCorrida', 1)
        ->execute()
;
    
04.09.2014 / 02:54