0

I am writing a very general script for data manipulation. For this I need to know the table name associated to the current controller. Somehow I cannot figure out how to get it.


UPDATE

Usually, each Controller has in the template/<name> directory the following PHP files:

  1. add.php
  2. edit.php
  3. index.php
  4. view.php

This means that for 20 tables I have to maintain 80 files and this is really stupid. Therefore I am using the CRUD plugin which minimizes it significantly and now I have only one my_index.php which covers the 20 physically versions of index.php for all affected tables.

In order to generate a proper view for each table I need the table's schema and for this I need to know the table name.

Therefore:

Any solution which adds a code into the proper Controller is not a solution as I have to touch/maintain again 20 files with the same line of code and this is definitely not the DRY concept. The solution must be something where I have the code ONCE in my_index.php.


What is the code to get the current table name?

I can run this code as described here https://book.cakephp.org/4/en/orm/table-objects.html#getting-instances-of-a-table-class but this has to be ran in each controller and it violates a bit the DRY concept as I have to add it manually into each controller... I am looking for a more general method.

I am running CakePHP Strawberry 4.1.

4 Answers 4

2

Your controller(s) should extend AppController. If you need the schema in the view, then AppController::beforeFilter can do all of the work required to find it, right in the controller, and set that as a $schema variable that your view can use. Something like this (untested, but should be quite close):

public function beforeFilter(Event $event) { parent::beforeFilter($event); $this->set('schema', $this->loadModel()->getSchema()); } 
Sign up to request clarification or add additional context in comments.

2 Comments

You can use loadModel() without any arguments to retrieve the default model instance, it will take care of plugin handling automatically.
Awesome! Updated the answer to reflect this simpler way to get there.
0

The default table short class name is derived from the controller name, and the possible plugin name, and it is being stored in the $modelClass property of \Cake\Datasource\ModelAwareTrait that the controller inherits.

In a controller named ArticlesController, the $this->modelClass proeprty will hold Articles, and if the controller would live in a plugin named Blog, the property would hold Blog.Articles.

Comments

0

After playing around I found this solution:

function getTableNameFromController() { $controller = $this->getRequest()->getParam('controller'); $controllerResolved = sprintf("%s%sController", 'App\Controller\\', $controller ); $dummy = new $controllerResolved(); $tableObject = $dummy->getTableLocator()->get($controller); $tableName = $tableObject->getTable(); print_r($tableName); } 

I have to place this code ONCE in my_index.php and I am done.

I don't think this is an optimal solution as I have to create a dummy instance of the controller from which I want to get the table name.

Any better solution is more than welcome and accepted.

6 Comments

So, your question is really not about how to get the table associated with the controller, but how to get access to the controller from some other part of the code? Your question says "the current controller", which makes it sound like you're already in the controller. What's the context in which this code is running?
Then how about simply passing the name from within your controller, ie set it as a view variable? Or even pass the whole table object down as a view variable?
Or better yet, tell us what exactly you actually want to do with that table object in the view? I have a feeling we could deal with an xy-question here.
What do you mean where and how? Where to put the explanation? As an edit in your question.
If you just need the name in the view, then setting that as a view variable in AppController::beforeFilter could be a good way to go. If you're needing to access the table for something in the view, that's a good sign that your design is flawed; views shouldn't generally be dealing directly with tables.
|
0

You need only define controller property

protected $defaultTable = 'ModelTable'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.