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

时间:2019-02-19 17:00:17

标签: 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";
        }

它似乎不起作用,并且出现以下错误

例外:在准备:选择来自用户的电子邮件时,无法从模型列表:“用户”中获取模型的来源

我的型号列表代码如下

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

1 个答案:

答案 0 :(得分:2)

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

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';
}

以上代码的作用: -从发布的数据中获取email。将其消毒为字符串。如果尚未发布,则返回一个空字符串 -在Users模型中搜索email字段的任何记录中存在的电子邮件。 -如果未返回任何内容,请继续,如果找到匹配项,则回显结果。