开发者

Symfony framework - check database entry existence

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');


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜