Doctrine DBAL bank operations DB

Asked

Viewed 133 times

1

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

Query-Builder

To work with data manipulation ?

and how I would do to make bank operations will go with it 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 initiating the dbal Doctrine package, I’m reading the documentation, for example in PDO I make the call from $pdo->bindValues as in the example above, in the Doctrine package as I would do these parameters ?

  • I use Query-Builder if it does not answer ... I end up using DRM or Data Manipulation

  • I did not understand what your doubt, you want to know how to run these queries with Dbal Doctrine ?

  • exactly. I gave the above example using PDO, how do I run with Doctrine ? I saw in the documentation but could not understand right.

1 answer

2

You can use Querybuilder to facilitate the creation of Sqls, in your case 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()
;
  • Thanks a lot. @Fábio Lemos Elizandro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.