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 al开发者_C百科so 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
Below example may useful:
App::import('Model', 'Student');
$Student = new Student();
$dataArray = $Student->getDataArray("name like '%".$keword."%'");
$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
App::import('Model', 'MyModel');
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();
精彩评论