I have the following code:
<?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();
}
}
what i am trying to do is to check if the given email is already registerd in the database.
I tried to do that with this part of the code
// 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";
}
It doesn't seems to work and I am getting the following error
"Exception: Can't obtain model's source from models list: 'Users', when preparing: SELECT email FROM users"
My models list code is the following
<?php
use Phalcon\Mvc\Model;
class Users extends Model
{
public $id;
public $name;
public $email;
}
Can someone tell me what I am doing wrong or how I suppose to check if the given email is already added to the database and then "echo" the appropriate message .
Thanks in advance for your time
Final worked Code
<?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();
}
}
}
For your error you need to tell the model what table it maps to:
public function getSource()
{
return 'users';
}
A bit of an easier approach is using findFirst
on the model like this:
$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';
}
What the above code does:
- Get the email
from the posted data. Sanitize it as string. If it has not been posted, return an empty string
- Search the Users
model for an email that exists in any record for the email
field.
- If nothing has been returned continue, if a match has been found echo the result out.