php - 使用 phalcon 检查数据库中是否存在电子邮件

标签 php sql database forms phalcon

我有以下代码:

<?php

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Model\Query;

class SignupController extends Controller
{
    public function indexAction()
    {

    }

    public function registerAction()
    {


        $user = new Users();

        // Store and check for errors
        $success = $user->save(
            $this->request->getPost(),
            [
                "name",
                "email",
            ]
        );

        // Instantiate the Query
        $query = new Query(
            'SELECT email FROM users',
            $this->getDI()
        );

        // Execute the query returning a result if any
        $users = $query->execute();


        if($user==$users)
        {
            echo "Email already exists";
        }

        else if ($success)
        {
            echo "Thanks for registering!";
        } else
            {
            echo "Sorry, the following problems were generated: ";

            $messages = $user->getMessages();

            foreach ($messages as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }


        $this->view->disable();
    }
}

我想做的是检查给定的电子邮件是否已在数据库中注册。

我尝试用这部分代码来做到这一点

// Instantiate the Query
        $query = new Query(
            'SELECT email FROM users',
            $this->getDI()
        );

        // Execute the query returning a result if any
        $users = $query->execute();


        if($user==$users)
        {
            echo "Email already exists";
        }

它似乎不起作用,我收到以下错误

异常(exception):无法从模型列表中获取模型的来源:“用户”,准备时:从用户中选择电子邮件

我的模型列表代码如下

<?php

use Phalcon\Mvc\Model;

class Users extends Model
{
    public $id;
    public $name;
    public $email;
}

有人可以告诉我我做错了什么,或者我应该如何检查给定的电子邮件是否已添加到数据库中,然后“回显”适当的消息。

预先感谢您的宝贵时间

最终工作代码

<?php

use Phalcon\Mvc\Controller;


class SignupController extends Controller
{
    public function indexAction()
    {

    }

    public function getSource()
    {
        return 'users';
    }




    public function registerAction()
    {

        $user = new Users();

        $email = $this->request->getPost('email', 'string', '');
        $result = users::findFirst(
            [
                'conditions' => 'email = :email:',
                'bind' => [
                    'email' => $email,
                ]
            ]
        );

        if (false !== $result) {
            echo 'The email exists in the database';
        } else {

            // Store and check for errors
            $success = $user->save(
                $this->request->getPost(),
                [
                    "name",
                    "email",
                ]
            );

            if ($success) {
                echo "Thanks for registering!";
            } else {
                echo "Sorry, the following problems were generated: ";

                $messages = $user->getMessages();

                foreach ($messages as $message) {
                    echo $message->getMessage(), "<br/>";
                }
            }


            $this->view->disable();
        }
    }
}

最佳答案

对于您的错误,您需要告诉模型它映射到哪个表:

public function getSource()
{
    return 'users';
}

一个更简单的方法是在模型上使用 findFirst ,如下所示:

$email  = $this->request->getPost('email', 'string', '');
$result = Users::findFirst(
    [
        'conditions' => 'email = :email:',
        'bind'       => [
            'email' => $email,
        ]
    ]
);

if (false !== $result) {
    echo 'The email exists in the database';
}

上面的代码做了什么: - 从发布的数据中获取电子邮件。将其 sanitizer 为字符串。如果尚未发布,则返回空字符串 - 在Users 模型中搜索email 字段中任何记录中存在的电子邮件。 - 如果没有返回任何内容,则继续,如果找到匹配项,则回显结果。

关于php - 使用 phalcon 检查数据库中是否存在电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54771390/

相关文章:

php - 单击按钮时如何插入表格?

php - 显示周围没有页面模板的 Drupal View

sql - 如何在此表中找到重复的连续值?

mysql - 需要有关 SQL 查询的帮助

php - mysql---留言数据库如何设计?

mysql - 类似类型的数据保留一张表还是多张表,在考虑高性能的情况下哪一个最好

php - 为 AJAX-PHP-MySQL 生成的表创建动态 Div 标签

sql - 如何进一步指定一个sqlite查询

php - 完成后不关闭与数据库的连接对安全有何影响?

php - php中内部函数无法调用外部函数的变量