douyin8809
2013-07-09 22:41
浏览 51
已采纳

在类中使用PHP Activerecord Finders

I'm trying to test if a record exists with the following code using PHP Activerecord:

class User extends ActiveRecord\Model {

    public function user_exists() {
        $user = User::find_by_email($this->email);
        if ( $user ) {
            return true;
        } else {
            return false;
        }
    }

}

$user = new User();
$user->email = "test@email.com";

if ( $user->user_exists() ) {
    echo "user exists";
} else {
    echo "no user";
}

For some reason, I'm getting a 'Call to undefined method: find_by_email' error. What am I doing wrong?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhi3105 2013-07-09 22:55
    已采纳

    Try the exists method:

    User::exists(array('email' => $this->email));
    

    Which is faster because it only checks for existence but does not fetch the data.

    Or even better; use model validations:

    http://www.phpactiverecord.org/projects/main/wiki/Validations#validates_uniqueness_of

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题