Yii Framework Forum: validation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

validation Rate Topic: ***** 1 Votes

#1 User is offline   Programming_freak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 08-January 10

Posted 13 January 2010 - 04:58 AM

I am using the following code to check whether the username already exists

Here User is one model and i am accessing data from table 'user'.

Can anyone tell me wats wrong in the code...why its not echoing the result
I checked everything fine till criteria part.
some thing wrong with accessing data.

function checkUserName($username) {

    $mod = new User;
    $criteria = new CDbCriteria;
    $criteria->condition = "UserName=:UserName";
    $criteria->params = array(":UserName"=>$username);
    
    $mod = $mod->findAll($criteria);
    echo $mod->UserName;
    die();
  }

0

#2 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 13 January 2010 - 05:29 AM

View PostProgramming_freak, on 13 January 2010 - 04:58 AM, said:

I am using the following code to check whether the username already exists

Here User is one model and i am accessing data from table 'user'.

Can anyone tell me wats wrong in the code...why its not echoing the result
I checked everything fine till criteria part.
some thing wrong with accessing data.

function checkUserName($username) {

    $mod = new User;
    $criteria = new CDbCriteria;
    $criteria->condition = "UserName=:UserName";
    $criteria->params = array(":UserName"=>$username);
    
    $mod = $mod->findAll($criteria);
    echo $mod->UserName;
    die();
  }



use find, finAll return array
try

function checkUserName($username) {
    // $mod = new User;  
    $criteria = new CDbCriteria;
    $criteria->condition = "UserName=:UserName";
    $criteria->params = array(":UserName"=>$username);
    
    $mod = User::model()->find($criteria); //modify
    echo $mod->UserName;
    die(); 
  }

KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#3 User is offline   Programming_freak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 08-January 10

Posted 13 January 2010 - 05:56 AM

function checkUserName($username) {
    // $mod = new User;  
    $criteria = new CDbCriteria;
    $criteria->condition = "UserName=:UserName";
    $criteria->params = array(":UserName"=>$username);
    
    $mod = User::model()->find($criteria); //modify
    echo $mod->UserName;
    die(); 
  }



Nops ....its not working....
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 January 2010 - 06:03 AM

Remove $mod=new User. And query like this:
$mod=User::model()->findAll($criteria);


Edit: Oops, sorry, waited to long with my reply ;)
0

#5 User is offline   Programming_freak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 08-January 10

Posted 13 January 2010 - 06:07 AM

Ya I tried this also but ..... no luck
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 January 2010 - 06:08 AM

View PostProgramming_freak, on 13 January 2010 - 06:07 AM, said:

Ya I tried this also but ..... no luck


Maybe $mod===null because there's no user? Also turn on logging to see the generated SQL.
0

#7 User is offline   Programming_freak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 08-January 10

Posted 13 January 2010 - 06:17 AM

I checked it thoroughly...and its not the mysql error...


hey can u tell me how to turn on the logging
0

#8 User is offline   Programming_freak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 08-January 10

Posted 13 January 2010 - 06:28 AM

hey can u tell me how to turn on the logging
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 January 2010 - 06:30 AM

Empty results are not a DB error :). So your $mod could be null if no user with specified name was found.

Search for CWebLogroute on the forum, check the guide on logging or check this on how to enable logging:

http://www.yiiframew...__fromsearch__1
0

#10 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 13 January 2010 - 07:08 AM

View PostMike, on 13 January 2010 - 06:30 AM, said:

Empty results are not a DB error :). So your $mod could be null if no user with specified name was found.

Search for CWebLogroute on the forum, check the guide on logging or check this on how to enable logging:

http://www.yiiframew...__fromsearch__1


or put this in config/main.php
.....
    // application components
    'components'=>array(
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning,info,trace',
                ),
                            array(
                                 'class' => 'CProfileLogRoute',
                                 'enabled' => YII_DEBUG ? true : false,
                                 'showInFireBug' =>false,//true,
                                 'report'=>'summary',
                              ),
            ),
        ),
.................
        'db'=>array(
            'connectionString'=>'mysql:host='.$ip_servidor_mysql.';dbname=XXXXX','charset'=>'utf8', 'username'=>'uuuuuuuu','password'=>$clave_mysql,

                       'enableProfiling' => true,
        ),



;)

Edit: and see the runtime/application.log
or in the browser at end

This post has been edited by Horacio Segura: 13 January 2010 - 07:09 AM

KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#11 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 January 2010 - 07:12 AM

Uhm, profiling !== sql logging.

You don't need CProfileLogRoute to log SQL statements and also no enableProfiling. You should set enableParamLogging instead.
0

#12 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 13 January 2010 - 07:21 AM

View PostMike, on 13 January 2010 - 07:12 AM, said:

Uhm, profiling !== sql logging.

You don't need CProfileLogRoute to log SQL statements and also no enableProfiling. You should set enableParamLogging instead.


but it works for me
shown at the bottom of the page the trace (or in FireBug if enabled)
you can put as the configuration is correct?
thanks
KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#13 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 January 2010 - 07:24 AM

Profiling is for measuring the time of SQL statements and produces slightly different output. CWebLogRoute + enableParamLogging simply displays the generated SQL + parameters.
1

#14 User is offline   gallego123 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 535
  • Joined: 11-August 09
  • Location:Argentina

Posted 13 January 2010 - 07:32 AM

View PostMike, on 13 January 2010 - 07:24 AM, said:

Profiling is for measuring the time of SQL statements and produces slightly different output. CWebLogRoute + enableParamLogging simply displays the generated SQL + parameters.


thanks Mike!

for others user like me:
.................
        'db'=>array(
            'connectionString'=>'mysql:host='.$ip_servidor_mysql.';dbname=XXXXX','charset'=>'utf8', 'username'=>'uuuuuuuu','password'=>$clave_mysql,

                       //'enableProfiling' => true,
                       'enableParamLogging'=>true,
        ),


This post has been edited by Horacio Segura: 13 January 2010 - 07:34 AM

KISS - Keep It Simple Stupid
ASAP-As Soon As Possible
http://www.yiiframew...oc/cookbook/71/
0

#15 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 13 January 2010 - 09:00 AM

Just as an aside, you are aware of CUniqueValidator, right?

Also, you might want to do a count() instead of a findAll(), since you're not actually interested in the associated row, just whether it exists or not.
0

#16 User is offline   Programming_freak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 08-January 10

Posted 14 January 2010 - 12:12 AM

View PostSander, on 13 January 2010 - 09:00 AM, said:

Just as an aside, you are aware of CUniqueValidator, right?

Also, you might want to do a count() instead of a findAll(), since you're not actually interested in the associated row, just whether it exists or not.





Ya the function count() works for me instead of using the findall()......nice suggestion

thanx a lot
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users