# Check Record exists in database

**URL:** <https://discourse.cakephp.org/t/check-record-exists-in-database/2013>\
**Category:** Need Help\
**Created:** [February 8, 2017, 6:37am UTC](https://discourse.cakephp.org/t/check-record-exists-in-database/2013 "2017-02-08T06:37:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nohastanvir](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/nohastanvir/32/419_2.png) [@Nohastanvir](https://discourse.cakephp.org/u/Nohastanvir)\
**Post date:** [February 8, 2017, 6:37am UTC](https://discourse.cakephp.org/t/check-record-exists-in-database/2013/1 "2017-02-08T06:37:29Z")

</div>

$user = $this-\>Users-\>newEntity();  
if ($this-\>request-\>is(‘post’)){  
$number = $this-\>request-\>data[‘number’];  
$num = $this-\>Users-\>findByEmail($number)-\>select([‘number’])-\>hydrate(false)-\>first();

```
		if(!$this->Users->exists($num)){				
			$user = $this->Users->patchEntity($user, $this->request->data);
			$this->Users->save($user);
			echo "1";
    	}
		else{
			echo "There is another...";
		}
	}
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
}
```

---

<div class="post-metadata">

**Author:** ![Lightbringer](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Lightbringer](https://discourse.cakephp.org/u/Lightbringer)\
**Post date:** [February 11, 2017, 1:29am UTC](https://discourse.cakephp.org/t/check-record-exists-in-database/2013/2 "2017-02-11T01:29:33Z")

</div>

You didn’t ask a question, but it looks like you are trying to validate that a form field corresponding to a database column contains a unique value. You could simply add a custom validation to the UsersTable.php $validator:

> -\>add(‘email’, [  
> ‘unique’ =\> [  
> ‘rule’ =\> ‘validateUnique’,  
> ‘provider’ =\> ‘table’,  
> ‘message’ =\> ‘This email is already associated with an active account’  
> ]  
> ])
