CakePHP: access model in another model / in app_model.php to check Banknumber

I am wondering how I can use the data from model B while I check model A, here, to check if the entered Banknumber is correct:

My users determine their bank account during registration. For example. "banknumber". I check this in the usual way in my user.php model

var $validate = array( 'banknumber' => array( 'minLength' => array( 'rule' => array('minLength', 8), 'message' => '...', 'required' => true, ), 

Now I want to know if the entered Banknumber is real, so I got the β€œBanks” table in my database with all the real Banknumbers, and I use some of the own verification functions that I specify in app_model.php.

 function checkBankExists($data) { if (!$this->Bank->findByBanknumber($data)) { return false; } else { return true; } } 

But this never works, because while I test the User-Model, I can only use this one in the app_model function, accessing it with the name this this this this this> the this this the this this this this this this this this the this this the this this this the the this this the the this this the IMPOSSIBLE i get:

Undefined property: User :: $ Bank [APP \ app_model.php

Call member function findByBanknumber () for a non-object

Is there any way to import / access other models in a function in app_model.php?

Thanks!

+7
source share
2 answers

you can import your model, create an instance and use it as you like:

 App::import('model','Bank'); $bnk = new Bank(); $bnk->findByBanknumber($data); 
+2
source

ClassRegistry should usually be used instead of AppImport, because AppImport loads the file, rather than registering it correctly, with a cake style.

Using the example above.

 $bnk = ClassRegistry::init('Bank'); $bnk->findByBanknumber($data); 
+24
source

All Articles