0
votes

I have a legacy (not written to Cake naming conventions) database on a CakePHP 3.5.13 application.

One of the database tables is named article_95.

When I attempted to bake the application it's showing the entity name as Article95. It's then producing loads of error messages saying:

 Table 'article95' doesn't exist

So I've read CakePHP error: cake bake is using the wrong table name and How to use table name different then in database in cake php 3 and decided to do it manually using setTable().

So I have created src/Model/Table/Article95Table.php with the following code in it:

namespace App\Model\Table;

use Cake\ORM\Table;

class Article95Table extends Table
{
    public function initialize(array $config)
    {
        $this->setTable('article_95');
    }
}

But it won't seem to recgonise this. In a controller I've created a testing method and done the following:

    Cache::clear(false);
    $Article95 = TableRegistry::get('Article95');
    debug($Article95->find()->list());

But it's coming up with:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'article95' doesn't exist

It's as if it's not reading the setTable() method.

I've used the Cache::clear(false) to ensure it's not being cached and have inspected the tmp/cache directory for any suspect files - nothing relevant in there.

Any ideas?

Equally if I change my debug() statement to just debug($Article95) I get the following, with the wrong table name:

object(Cake\ORM\Table) {

    'registryAlias' => 'Article95',
    'table' => 'article95',
    'alias' => 'Article95',
    'entityClass' => '\Cake\ORM\Entity',
    'associations' => [],
    'behaviors' => [],
    'defaultConnection' => 'default',
    'connectionName' => 'default'

}

Incidentally, if I put a non-existent class/entity name in TableRegistry::get() - for example TableRegistry::get('dsfdsfdsfsd'); it won't produce an error message but will show the object above with the non-existent table name. Surely this is also wrong?

1
Something you're showing here probably isn't what you're actually using. The object that you have there is a so called "generic table" or "auto table", a table object created from \Cake\ORM\Table, because the concrete table class Article95Table could not be found. The same thing that happens for dsfdsfdsfsd.ndm
How can the concrete table class not be found? There's a file Article95Table.php inside the appropriate directory! It's working for other models I've created but bake seems to have been happy to work with their naming scheme.Andy
I can't really tell from here, what you are showing looks correct, but as I said, maybe that's not what you're actually using? Maybe there's a typo in the path, or in the namespace, maybe it's a permission issue, maybe a cache issue in case you're using an opcode cache, etc...ndm

1 Answers

0
votes

I've managed to get this working so wanted to share my solution.

After some trial and error it seems that appending an 's' to the class name (presumably to make Cake consider it plural?) works.

So I renamed the Table class to Article95sTable.php and then used the following code inside:

class Article95sTable extends Table
{

    public function initialize(array $config)
    {
        $this->setTable('article_95'); 
    }
}

Now when I call the following it picks up the table correctly:

$foo = TableRegistry::get('Article95s');
debug($foo);

To confirm this is working if I rename the table inside my setTable() method the output will change accordingly.