How to register user in Cake PHP (Check if Company ID exists in database and if yes- allow registration)
<p>I'm trying to create a registration form for a user with Cake PHP. The user can be registered only, when the company ID exist and if he is allowed to register. The problem is that during registration I ask the user to specify the Company ID (company is created by an administrator, and to allow user registration the company ID must exists in database. User belongs to company)</p>

<p>These are my tables in database:</p>

<p>users
id INT(11) NN,
email VARCHAR(30) NN,
password VARCHAR(255) NN,
name VARCHAR(30) NN,
company_id INT(11) NN,
created DATE NN,
modified DATE NN</p>

<p>companies
id INT(11) NN,
name VARCHAR(30) NN,
phone VARCHAR(30),
email VARCHAR(30) NN,
address1 VARCHAR(50) NN,
address2 VARCHAR(50) NN,
town VARCHAR(30) NN,
postcode VARCHAR(20),
password VARCHAR(255),
country_id INT(11) NN,
created DATE,
modified DATE,</p>

<p><strong>This is User's Registration Page (View):</strong></p>

<pre><code>&lt;div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns"&gt;
    &lt;div class="panel"&gt;
        &lt;h3 class="text-center"&gt;Register New User&lt;/h3&gt;
        &lt;?= $this-&gt;Form-&gt;create($user); ?&gt;
            &lt;?= $this-&gt;Form-&gt;input('companyid'); ?&gt;
            &lt;?= $this-&gt;Form-&gt;input('name'); ?&gt;
            &lt;?= $this-&gt;Form-&gt;input('email'); ?&gt;
            &lt;?= $this-&gt;Form-&gt;input('password', array('type' =&gt; 'password')); ?&gt;
            &lt;?= $this-&gt;Form-&gt;submit('Register', array('class' =&gt; 'button')); ?&gt;
        &lt;?= $this-&gt;Form-&gt;end(); ?&gt;
    &lt;/div&gt;
&lt;/div&gt;



**This is UsersController:**
    //register
    public function register(){

        $user = $this-&gt;Users-&gt;newEntity();
        //if the form is submitted
        if($this-&gt;request-&gt;is('post')){
            $user = $this-&gt;Users-&gt;patchEntity($user, $this-&gt;request-&gt;data);
            if($this-&gt;Users-&gt;save($user)){
                $this-&gt;Flash-&gt;success('Congratulation! You have successfully registered. Now you can log in');
                return $this-&gt;redirect(['action' =&gt; 'login']);
            } else {
                $this-&gt;Flash-&gt;error('Something went wrong! You did not register to the system');
            }
        }
        $this-&gt;set(compact('user'));
        $this-&gt;set('_serialize', ['user']);
    }



**AppController- Function Initialize**

public function initialize()
    {
        parent::initialize();

        $this-&gt;loadComponent('RequestHandler');
        $this-&gt;loadComponent('Flash');
        $this-&gt;loadComponent('Auth', [
            'authenticate' =&gt; [
                'Form' =&gt; [
                    'fields' =&gt; [
                        'username' =&gt; 'email',
                        'password' =&gt; 'password'
                    ]
                ]
            ],
            'loginAction' =&gt; [
                'controller' =&gt; 'Users',
                'action' =&gt; 'login'
            ]
        ]);
    }
</code></pre>

<p>Now. I just don't know how should I implement Registration for User in UserController in a way: Check if CompanyID exists in companies table- if yes- allow the user Registration and insert id, email, password, name, and company_id into users table. If doesn't exists display error "Company ID doesn't exists".</p>