Piyush Pagariya
Piyush Pagariya

Reputation: 21

Calling one model from another in CakePHP

I have one user model and other is accesscode model. During registration I am using the user model where I have set some validation rules for form data.

On the registration page I have one field which does not belong to the user model i.e. the access code field. I want to validate this field in user model to check whether the code entered by user is present in accesscode table or not and also want to check in third model i.e. useraccesscode for that access code.

How I can do this? I am using CakePHP v1.2.

Thank you

Upvotes: 2

Views: 2342

Answers (4)

Chandresh M
Chandresh M

Reputation: 3828

You can create instances of other models from within any model/controller using one of these two below methods:

If using Cake 1.2:

App::import('model','Attribute');
$attr = new Attribute();

If using Cake 1.1:

loadModel('Attribute');
$attr = new Attribute();

Upvotes: 0

Shah Alom
Shah Alom

Reputation: 1055

Below example may useful:

App::import('Model', 'Student');
$Student = new Student();
$dataArray =  $Student->getDataArray("name like '%".$keword."%'");

Upvotes: 2

Piotr Chabros
Piotr Chabros

Reputation: 475

$validated = $this->User->Accesscode->find('list', array('conditions' => array('Accesscode.name' => $this->data['User']['accesscode'])));

if(count($validated) > 0)
     //means that accesscode supplied in the form matches some accesscode in the database

Upvotes: 1

chetanspeed511987
chetanspeed511987

Reputation: 2025

App::import('Model', 'MyModel');

Upvotes: 0

Related Questions