0

i have a huge problem using the public variable "uses" in the CakePHP-Controller.

I set up my application with the following schema.

PagesController extends an AdminController which extends the AppController.

class PagesController extends AdminController 
class AdminController extends AppController

I do this because i also got an ApiController instead of the AdminController which used for (as you can mention) an additional API-Layer in my Application.

I include the additional-Layer by using this

 App::uses('AdminController', 'Controller');

in the PagesController and

App::uses('AppController', 'Controller');

in the AdminController.

It work fine for me this way.

EDIT:

class AppController extends Controller {

    public $uses = array('Print');

}

I want to make a Model called "Print" accessable in every of my Child-Controllers. That is why I initialize it in my AppController

class PagesController extends AdminController {

    public $uses = array('Article');

}

In PagesController I want to use "Print"-Model and in addition that declared "Article"-Model. In the Commentary for that $uses-variable is written down that every addition in the Child-Controller will merged into the parent-$uses-variable. But it doesnt do. it overwrite it.

Where is the mistake? Please Help.

user2140111
  • 122
  • 1
  • 9
  • 1
    It doesn't make a lot of difference to the question, but that's not a variable, it's a function / method call. – IMSoP Apr 16 '14 at 13:04
  • /** * An array containing the class names of models this controller uses. * public $uses containing the table-Models set up in the Controller to use there * Example: `public $uses = array('Product', 'Post', 'Comment');` * App::uses is just the static call to add additional php-Classes. – user2140111 Apr 16 '14 at 13:11
  • I really don't get your question but one thing to mention, Cakephp has a default `PagesController` with `public $uses = array();`. So, I think if you don't mention `public $uses` on your child controller it will take default `$uses`.... – Fazal Rasel Apr 16 '14 at 14:32
  • I just used the PagesController in my example here, in my code there are alot of other Controllers with the same problem. – user2140111 Apr 16 '14 at 14:53
  • you could use `$this->loadModel('Article')` in PagesController and will still be able to use `$this->Article`. I had the same issue and did a similar workaround – cornelb Apr 16 '14 at 15:11

2 Answers2

2

And it works ok:

AppController.php

App::uses('Controller', 'Controller');
class AppController extends Controller {
    public $uses = array('Print');
    public function test() {
        echo 'AppController => ';
    }
}

AdminController.php

App::uses('AppController', 'Controller');
class AdminController extends AppController {
    public $uses = array('Photo');
    public function test() {
        parent::test();
        echo 'AdminController';
        if (is_subclass_of('AdminController', 'AppController')) {
            echo ' (Subclass of AppController)';
        }
        echo ' => ';
    }
}

PagesController.php

App::uses('AdminController', 'Controller');
class PagesController extends AdminController {
    public $uses = array('Article');
    public function test() {
        parent::test();
        echo 'PagesController';
        if (is_subclass_of('PagesController', 'AdminController')) {
            echo ' (Subclass of AdminController)';
        }
        if (is_subclass_of('PagesController', 'AppController')) {
            echo ' (Subclass of AppController)';
        }
    }
    public function index() {
        self::test();
        pr($this->uses);
        exit;
    }
}

/pages/index

AppController => AdminController (Subclass of AppController) => PagesController (Subclass of AdminController) (Subclass of AppController)
Array
(
    [0] => Article
    [1] => Print
)

Cake merges only with one controller (AppController by default), not all the way up (AdminController->AppController->Controller). That's why there is no Photo model in Pages.

Hope this helps.

EDIT: You can try to merge $uses from Pages, Admin, App (and any class between) like this: AppController.php

protected function _mergeControllerVars() {
        parent::_mergeControllerVars();
        $parent = get_parent_class($this);
        while ($parent != 'Controller') {
            $parentVars = get_class_vars($parent);
            if (isset($parentVars['uses']) && $parentVars['uses'] !== true) {
                $this->uses = array_merge(
                    $this->uses,
                    array_diff($parentVars['uses'], $this->uses)
                );
            }
            $parent = get_parent_class($parent);
        }
    }
Kamil Kosiński
  • 296
  • 2
  • 3
-1

I think you have syntax problem, can you try below solution. Please write following code in your controller:

public var  $uses = array('Print');
Flexo
  • 87,323
  • 22
  • 191
  • 272
Dhaval Rajani
  • 191
  • 1
  • 2
  • 15
  • The var keyword has been deprecated since PHP 5.0, it has no functional use in PHP any longer and is only supported for backwards compatibility. – Morgan Apr 16 '14 at 21:05
  • My friend i am using php version 5.4 and it is running in a good manner – Dhaval Rajani Apr 17 '14 at 04:30
  • Sir, it works, because it is left in to support old applications. It is no longer proper syntax and should not be used in new code. Please see the "Note" section under the Property Visibility heading on this page: http://www.php.net/manual/en/language.oop5.visibility.php – Morgan Apr 17 '14 at 07:50