YII CActiveRecord->find() [英] YII CActiveRecord->find()

查看:17
本文介绍了YII CActiveRecord->find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在还在博客教程上学习 YII 并且对一些代码感到好奇.

Im now still learning YII on blog tutorial and curious with some code.

在这个链接上
http://www.yiiframework.com/doc/blog/1.1/en/prototype.auth

有这样的代码

<?php
class UserIdentity extends CUserIdentity
{
private $_id;

public function authenticate()
{
    $username=strtolower($this->username);
    $user=User::model()->find('LOWER(username)=?',array($username));
    if($user===null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    else if(!$user->validatePassword($this->password))
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
    {
        $this->_id=$user->id;
        $this->username=$user->username;
        $this->errorCode=self::ERROR_NONE;
    }
    return $this->errorCode==self::ERROR_NONE;
}

public function getId()
{
    return $this->_id;
}
}

我对一些代码很好奇.

  1. 为什么代码的最后一行没有?>?
  2. 在这一行 $user=User::model()->find('LOWER(username)=?',array($username)); 为什么使用 LOWER(username)=? 不是 LOWER(username)=.为什么需要?,这是一些我还不知道的查询条件吗?
  1. Why there is no ?> at the end line of the code?
  2. at this line $user=User::model()->find('LOWER(username)=?',array($username)); why using LOWER(username)=? not LOWER(username)=. WHy there is need ?, is this some query conditional that i didn't know yet maybe?

推荐答案

  1. ?> 根据 此链接:

文件末尾的 PHP 块的结束标记是可选的,在某些情况下省略它在使用 include() 或 require() 时很有帮助,因此文件末尾不会出现不需要的空格,并且稍后您仍然可以向响应添加标头.如果您使用输出缓冲,并且不希望在包含文件生成的部分末尾添加不需要的空格,这也很方便.

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

  • ? 与 SQL 语法相关,从 此处.另外这里的第二个答案说:

  • the ? is related to SQL syntax as seen from here. Also the second answer here says that:

    问号代表稍后将被替换的参数.使用参数化查询比将参数直接嵌入到查询中更安全.

    The question mark represents a parameter that will later be replaced. Using parameterized queries is more secure than embedding the parameters right into the query.

  • 这篇关于YII CActiveRecord->find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆