Use the model from inside the cakephp library

I created several files in the app / Lib folder and would like to access one of my models from the library classes:

<?php App::uses('CrawlerBase','Lib'); App::uses('Deal', 'Model'); class SampleCrawler extends CrawlerBase { public $uses = array('Deal'); function __construct(){ $this->Deal->create(); 

However, cake can not seems to find the Deal and im model, receiving a call to the create () member function for the non-object in the model creation line.

Rate the help.

+4
source share
3 answers

Another way to do this:

 APP::import('Model', 'Deal'); $this->Deal = new Deal(); $this->Deal->create(); 
+3
source

Always include models manually, if not in the controller / shell:

 $this->Deal = ClassRegistry::init('Deal'); 

and then

 $this->Deal->create(); // etc 

Advantage: you allow Cake to be downloaded and run the model for you, so if you have done this before, it will try to reuse it.

EDIT: for the sake of completeness, inside the controller / shell you can just do

 $this->loadModel('Deal'); $this->Deal->create(); 
+8
source

Try

  $ deal = new Deal ();  // to create Deal Object

 // if that doesnot work then, do
 ClassRegistry :: init ("Deal");
 $ deal = new Deal ();
0
source

Source: https://habr.com/ru/post/1387971/


All Articles