0
votes

I'm trying make a validation in my db using doctrine where I just need to make this statement in SQL like, Select * from usuarios where usuario_email = someEmail

After I click the submit button from my form, I instantiate the class UsuarioDao, from a script that includes bootstrap.php (from doctrine default) and pass with parameter entityManager. Then I call method validacao(), I know I don't doing of form right, not using filters etc, I just want understand how doctrine works. Here are my model doctrine

/**
 * @Entity @Table(name="usuarios") 
 **/
class Usuario {
    /** @Id @Column(type="integer") @GeneratedValue **/
    public $usuario_id;
    /** @Column(type="string") **/
    public $usuario_nome;
    /** @Column(type="string") **/
    public $usuario_email;
    /** @Column(type="string") **/
    public $usuario_senha;
    /** @Column(type="string") **/
    public $usuario_status;

    //getters and setters omited

}

Here my classDao for my model

use Doctrine\ORM\EntityManager;
class UsuarioDao{

    /**
     * @var EntityManager
     */
    private $entityManager;

    function __construct(EntityManager $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function validacao(){

        $usuario = new Usuario();
        $usuario->setUsuario_email($_POST['email']);
        $usuario->setUsuario_senha($_POST['senha']);
        $em = $this->entityManager;
        $query = $em->createQuery('SELECT u FROM Usuario u WHERE u.usuario_email ='. $usuario->getUsuario_email());
        $resultado = $query->getResult();
        return $resultado;
    }

}

I get this wrong:

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'SELECT u FROM Usuario u WHERE u.usuario_email [email protected]' in C:\wamp\www\RicardoOfficial\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php on line 52

and this

Doctrine\ORM\Query\QueryException: [Syntax Error] line 0, col 54: Error: Expected end of string, got '@' in C:\wamp\www\RicardoOfficial\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php on line 52

1
check the variable name$usuario->setUsuario_nome($_POST['email']); $usuario->setUsuario_senha($_POST['senha']); they are diffreent. Camel case problem. check once - Anant Kumar Singh
try changing WHERE u.usuario_email ='. $usuario->getUsuario_email()); to WHERE u.usuario_email ='. $usuario->getUsuario_email()) . "'"; - it looks like you're not closing the single quotes around the email address - dbinns66
$usuario->setUsuario_senha($_POST['senha']) will be for set the password, it dont is in use yet, i trying just with email first, but get this fail that i post in question - ricardo patrick marinho de sou
dbinns66 are this bro i surround the variable $usuario->getUsuario_email()) with single quotes and work. should be more easy :). anyway now is work - ricardo patrick marinho de sou

1 Answers

0
votes

You forgot quotes surrounding the value.

    $query = $em->createQuery('SELECT u FROM Usuario u WHERE u.usuario_email ="'. $usuario->getUsuario_email(). '"');

If you have a problem with SQL statements best thing to do is copy the query you got paste it to your SQL editor and see normally you would have spotted the mistake quiet fast.