0

Using symfony framework, which code is best to query database table to check if entry is already there?

I need query like this:

$q = $this->createQuery('t')
    ->where('t.email = ?', $email)
    ->andWhere('t.type = ?','newsletter');
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
Almas Adilbek
  • 4,371
  • 10
  • 58
  • 97

2 Answers2

3

The easist way, assuming you're in a Doctrine_Table instance, which it appears you are is:

$this->findOneByEmail($email);

You shouldn't need type if you're using concrete inheritance because it will be added via DQL callback (assuming you have them enabled), but for completeness:

$this->findOneByEmailAndType($email, 'newsletter');

These will return the Doctrine_Record if it exists or null if it doesn't.

You can also use a count on your query:

$exists = $this->createQuery('t')
    ->where('t.email = ?', $email)
    ->andWhere('t.type = ?','newsletter') // your probably don't need this
    ->count();

This will return either the number of records that match the query. This a more efficient as it does not hydrate the results.

xzyfer
  • 13,937
  • 5
  • 35
  • 46
1

Try this,

You can directly defined in form class.


$this->validatorSchema['email'] = new sfValidatorAnd(array(
    new sfValidatorString(array('required' => true, 'trim' => true)),
    new sfValidatorDoctrineUnique(array('model'=>'User','column'=>'email'),
    array('invalid' =>'Email Address already exist')),                   
    new sfValidatorRegex(
    array('pattern' => '~^(\s)*[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})(\s)*$~i'), 
    array('invalid' => 'Please enter valid email ID'))),
    array(),
    array('required' =>'Please enter email ID')
);

I think its much easier then other.

alexf
  • 1,303
  • 9
  • 20
Prakash
  • 39
  • 1
  • 5