Join tables error - need help please

Hi everybody, I tried to rewrite following SQL statement:

SELECT subject_id, workplace_id,,supervisors_rulings.supervisor_id,, people.surname, ruling_number
FROM supervisors_rulings
INNER JOIN rulings ON supervisors_rulings.ruling_id =
INNER JOIN people ON supervisors_rulings.supervisor_id =;
WHERE subject_id=1
ORDER BY surname;

into CakePHP as follows (code is a part of my view method in SubjectsController.php):

Unfortunately, my code does not work but SQL code work as expected. Error page is below:

Supervisors is and alias of People table (see code from RulingsTable.php below):

$this->belongsToMany(‘Supervisors’, [
‘className’ => ‘People’,
‘foreignKey’ => ‘ruling_id’,
‘targetForeignKey’ => ‘supervisor_id’,
‘joinTable’ => ‘supervisors_rulings’,

Thank you for your help in advance.

If you’re in the SubjectsController, the only model that will be auto-loaded for you is Subjects. Anything else you’ll need to either use loadModel (fetchTable if you’re on Cake 4.3 or newer), or else access the table object through some association to Subjects.

Not wanting to hijack this thread, and I think I’m kind-of on topic, but fetchTable doesn’t behave like loadModel - and the Book doesn’t explain the difference.

This works: -

        $user = $this->Users->get(1);

But this does not: -

        $user = $this->Users->get(1);

Error being $this->Users is null.

These work: -

        $user = $this->fetchTable('Users')->get(1);


        $userTable = $this->fetchTable('Users');
        $user = $userTable->get(1);

Could you please explain the difference, and why we don’t get the property of the loaded table merged into the $this object? Tnanks

$this->fetchTable('Users'); doesn’t set the property Users in the current class which $this->loadModel('Users'); previously did.

So if you replace $this->loadModel('Users'); with $this->Users = $this->fetchTable('Users'); its the same

Oh, that’s cunning - I like that. I’m still so mind-set on strongly typed languages I forget to do stuff like that. Thanks for the quick reply :slight_smile:

Thank you all for your help. Looks like a non-trivial topic.

May I have another question? Is my join table function written properly or there is an error? I know that this forum is probabely not intended to be used by beginners but if you are beginner, then it is really tough task to start with Cake … The latest available book for self-studying beginners is for cakephp 2 and there is nothing newer. Or am I wrong? is all the documentation for Cake 4.

Thank you. One think I can say that Cookbook is not studybook and it is very difficult to understand for someone who is not used to work with object oriented code.

Unfortunately, there is not engough books like CakePHP 2 Application Cookbook from Jorge Gonzales and James Watts …

Yeah, the Cookbook makes certain assumptions about a level of familiarity with PHP namespaces, object-oriented programming, HTML, CSS, web server configuration, database structure, and various other topics. It’s not realistic to try to cover everything anyone might need to know to use it.

And that is a pitty. For example, I am not a programmer, I was used to make some simple HTML/PHP/CSS pages, write some code in the R and MATLAB (data evaluation) and I offered to my boss to make a simple database system based on CakePHP (just a database of issued rulings, people who are allowed to do some activities etc.). I know very little from cakephp but I like it very much. I believe that if there was a good “study book” and not only “cake reference cookbook”, cakephp would be used more widely.

By the way, is there any online paid course of cakephp where I could enroll? Tought simply and by “real” experts in cakephp?

Just wondering, what makes you think that?

Well, not a programmer myself, my experience is that it doesn’t have to be that hard:

  • set up a local environment running PHP + CakePHP + DBMS (e.g. mariadb) + graphical databasemanager (e.g. phpmyadmin). In Windows using xammp deals with most of it. In Linux it can be more of a burden. When keeping things to defaults, doesn’t need that much configuration.

  • build a simple database (e.g. just 2 tables) with the graphical databasemanager. Take into account CakePHP database conventions. Don’t start with an existing real-life complex database, that will get you in trouble.

  • just run ‘bake all --everything’, and you will have a full working CRUD-app! Don’t need to be a programmer to do this.

My experience is to first get the database to its full complexity, and just bake, bake and bake. Not a complex programmer’s task I would say. And when things don’t seem to work after an edit of the database structure, just empty the caches!

At some point the just looking at the baked code and reading the more general parts of the cookbook will give you a feel how things are working.

Now it depends on what you want to change what knowledge you need. You certainly don’t need to have all knowledge at a profound level to just make a start.

Keep a fresh baked app apart from the code you’re working on (baked with a prefix (e.g. ‘/baked’) or in another project) and start working like a script-kiddie. Just copy/paste code and see what’s working. If it’s getting a mess, replace the messed up code with the baked code.

Just writing this down because when googling around you can read more messages like ‘CakePHP is hard to learn’ ‘steep learning curve’ etc. At some point it becomes a self fulfilling prophecy. And I don’t think that does justice to CakePHP, nor to the people who put their time in developing and helping with CakePHP.

To me it’s more like ‘start small, think big’, and you will get where you want to be.

Thank you for your reaction. I agree with you. Baking is perfect, I like cakephp in general. You can make a perfect setup or first version of your project very very easily.

My problem is that I do not have enough experience with object programming and web app creation in general. I would appreciate more detailed and simple tutorials on how to make multilevel user authentication, how to implement generation of pdf files, how to make controller code slim as possible and how to work with functions in model etc.

I like for example these excellent tutorials from Alimon Pito - YouTube, but I would prefer to have also a similar study book with real teaching approach to the reader.

Maybe, if someone knows to work with other frameworks like e.g. Laravel etc., then cake is supereasy for him. But generally I think that if people from CakePhp put more focus on explaining CakePHP to people like me, they could obtain even more users.

You still seem stuck on expecting Cake to provide documentation on general programming topics. There will be tons of material out there about object oriented programming concepts in general, how OO works in PHP, how web applications work. It’s all applicable to anything written in CakePHP or any other framework.

Zuluru, yes, you are right. Unfortunately there is no time for an extensive study of the problem … Nevertheless, thank you all here in the forum for your help. Even if the cookbook is not understandable for me as much as I desired, support of admins and users of this forum is excellent.