validation

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

  }






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…

Remove $mod=new User. And query like this:


$mod=User::model()->findAll($criteria);

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

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.

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

hey can u tell me how to turn on the logging

hey can u tell me how to turn on the logging

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.yiiframework.com/forum/index.php?/topic/2692-how-to-echo-or-print-sql-query/page__hl__cweblogroute__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

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

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,

        ),




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