Page 1 of 1

CakePHP framework oddity... fresh pair of eyes pls

Posted: Wed Apr 23, 2008 10:44 am
by mattcooper
Hello all,

I am a seasoned developer and have been building apps with Cake for some time. Today I came up against a bug that I cannot figure out - Cake just STOPPED working. I'm using the 1.2 beta on php 5.x and all has been well throughout this dev. However, I moved a block of code into the AppController file to make it available to everything and it was a bit buggy. So, I moved it back to where it came from... that's all. Now I am getting a fatal error:

Call to a member function findByUri() on a non-object in .....\htdocs\app\app_controller.php on line 52

In spite of this:

Code: Select all

var $uses = array([b]'Page'[/b], 'User', 'PageModule', 'News', 'Location', 'Content', 'UserLocation');
...and in spite of the fact it was working fine before! As I said, I know what I'm doing and I cannot understand why Cake isn't creating an instance of the "Page" model - or any classes for that matter: the problem exists for components as well. Here is the (abridged) beforeRender() method I'm using for this:

Code: Select all

 
<?php
    function beforeRender()
    {
        // see if there's any $bodytext set to pass into the page along with the dynamic title
       $q = $this->[b]Page[/b]->findByUri(str_replace('/', '', $this->params['url']['url'])); // line 52
       if (!empty($q)) {
            $q = $this->Content->findById($q['Page']['content_id']);
            $this->set('pagetitle', $q['Content']['title']);
            $this->set('bodytext', $q['Content']['body']);
        } 
    }
?>
 
Here is the stack trace... my head spins...
AppController::beforeRender() - APP\app_controller.php, line 52
Controller::render() - CORE\cake\libs\controller\controller.php, line 663
ErrorHandler::missingTable() - CORE\cake\libs\error.php, line 214
ErrorHandler::__construct() - CORE\cake\libs\error.php, line 90
Object::cakeError() - CORE\cake\libs\object.php, line 169
Model::setSource() - CORE\cake\libs\model\model.php, line 785
Model::__construct() - CORE\cake\libs\model\model.php, line 366
ClassRegistry::init() - CORE\cake\libs\class_registry.php, line 130
Controller::loadModel() - CORE\cake\libs\controller\controller.php, line 433
Controller::constructClasses() - CORE\cake\libs\controller\controller.php, line 390
ErrorHandler::__construct() - CORE\cake\libs\error.php, line 76
Object::cakeError() - CORE\cake\libs\object.php, line 169
Model::setSource() - CORE\cake\libs\model\model.php, line 785
Model::__construct() - CORE\cake\libs\model\model.php, line 366
ClassRegistry::init() - CORE\cake\libs\class_registry.php, line 130
Controller::loadModel() - CORE\cake\libs\controller\controller.php, line 433
Controller::constructClasses() - CORE\cake\libs\controller\controller.php, line 390
I have made the mistake of not developing this under version control as it is a personal project :( I am desperate to get to the bottom of this... has anyone experienced this before? I really need a hand here! I have (somewhat desperately!) reinstalled Cake's Core, restarted my machine (in the hope that PHP itself was the culprit following several FastCGI crashes earlier today) and looked everywhere for the cause of the bug. No joy. The full AppController file is pasted at the bottom of this message, hope a fresh pair of eyes can shed some light on this.

Looking forward to your responses,

Matt

-----------------------------------------------------------------------

Code: Select all

 
<?php
    class AppController extends Controller
    {
        var $components = array('ObAuth', 'Email', 'Notification', 'Submenu', 'Cookie', 'RequestHandler', 'Session');
        var $uses = array('Page', 'User', 'PageModule', 'News', 'Location', 'Content', 'UserLocation');
        var $helpers = array('Html', 'Form', 'Ajax', 'Session', 'Javascript', 'Crumb');
 
        var $defaultLocation = 'London';
        var $defaultExpiry = 31556926;
 
        function __construct()
        {
            parent::__construct();
            // Import this [abstract] class WITHOUT instantiating an object
            App::import('model', 'LookupModel');
        }
 
        function beforeFilter()
        {
            if ($this->referer() != '/users/login' && $this->here != '/users/login')
                $this->Session->write('UberReferer', $this->here);
 
            // Only instantiate if it doesn't already exist..
            if (!isset($this->ObAuth)) $this->ObAuth = new ObAuthComponent();
 
            // Just in case it hasn't been started up (Which it hasn't it seems, at beforeFilter() stage)
            $this->ObAuth->startup($this);
 
            if (!$loc = $this->Cookie->read('userLocation')) {
                $q = $this->Location->findByName($this->defaultLocation);
                $locationId = $q['Location']['id'];
            } else {
                $locationId = $loc;
            }
 
            // hard code user data until we have some test users
            if (!$this->ObAuth->getUserId()) {
                $this->userLocation = $locationId;
            } else {
                $q = $this->User->findById($this->ObAuth->getUserId());
                $this->userLocation = !empty($q['User']['location_id']) ? $q['User']['location_id'] : $locationId;
            }
 
            // set user id for the app
            $this->userId = $this->ObAuth->getUserId();
        }
 
        function beforeRender()
        {
            // see if there's any $bodytext set to pass into the page along with the dynamic title
            $q = $this->Page->findByUri(str_replace('/', '', $this->params['url']['url']));
            if (!empty($q)) {
                $q = $this->Content->findById($q['Page']['content_id']);
                $this->set('pagetitle', $q['Content']['title']);
                $this->set('bodytext', $q['Content']['body']);
            }
            // App-wide admin protection function. Override it if you want..
            if (eregi('admin_', $this->params['action'])) {
 
                $this->layout = 'admin';
                $this->ObAuth->lock('admin');
            }
 
            // set the modules that are available for this section
            if (isset($this->params['controller']) && $this->params['action'] == 'index' && array_key_exists(low($this->params['controller']), $this->PageModule->modules))
                $this->modules = $this->PageModule->modules[low($this->params['controller'])];
 
            // Pass in the authentication component to allow all pages to access user/session data
            $this->set('authObject', $this->ObAuth);
 
            // Oh go on let's give them a session too
            $this->set('session', $this->Session);
 
            $this->set('route', @$this->params['controller']);
 
            if (!isset($this->Submenu)) $this->Submenu = new SubmenuComponent();
 
            $this->set('sectionMenu', $this->Submenu->submenuItems());
 
             //pr($this->params);
            $this->set('params', $this->params);
 
            if ($this->params['action'] == 'index' && array_key_exists(low($this->params['controller']), $this->PageModule->modules)) {
 
                $this->set('modules', $this->PageModule->modules[low($this->params['controller'])]);
 
                if ($storedModules = $this->Cookie->read(low($this->params['controller']) .'ModulePrefs')) {
                    $this->data = unserialize($storedModules);
                } else {
                    // display the default modules
                    $this->data['PageModule']['modules'] = array(0, 1, 2, 3, 4);
                    $this->Cookie->write(low($this->params['controller']) .'ModulePrefs', serialize($this->data['PageModule']['modules']), (mktime()+3600));
                }
 
                $this->data[low($this->params['controller']) .'_content'] = array();
 
                // this is temporary: an array of exclusions that will gradually grow as things are built::
                $exclusions = array('news');
 
                foreach ($this->data['PageModule']['modules'] as $key => $module) {
 
                    if (!in_array($this->PageModule->modules[low($this->params['controller'])][$module], $exclusions))  {
                        $moduleContentVar = $this->PageModule->modules[low($this->params['controller'])][$module] .'_content';
                            $this->data[low($this->params['controller']) .'_content'][$this->PageModule->modules[low($this->params['controller'])][$module]] =
                            'This is dynamic data for the '. $this->PageModule->modules[low($this->params['controller'])][$module] .' module';
                    }
                }
            }
        }
    }
?>