Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Model from inside a Library cakephp

Tags:

php

cakephp

I created a few files in 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 cant seems to find the Deal model and im getting a call to member function create() on a non-object in the model creation line.

Appreciate the help.

like image 583
Yehia A.Salam Avatar asked Dec 25 '11 23:12

Yehia A.Salam


1 Answers

Always include models manually if not in a controller/shell:

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

and then

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

The advantage: You let Cake load and init the model for you, so if you already did that earlier it will try to reuse it.

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

$this->loadModel('Deal');
$this->Deal->create();
like image 63
mark Avatar answered Sep 23 '22 04:09

mark