1
votes

Currently working on Cakephp 2.x and went through a following error. We are upgrading an old project which is written in procedural php to cake php with more concepts. So we gotta use the existing database which doesnt have any cake conventions. Currently the problem i am facing is there is a table called tranz_vehicle in database. I have a model named vehicle.php and following is the code for it

<?php
class Vehicle extends AppModel{

    public $useTable="tranz_vehicle";

}
?>

Even after i used $useTable, It shows the following error

Table vehicles for model Vehicle was not found in datasource default.

I cant modify the table name.. Any suggestions? Am a total newbie to cakephp

Also there is table users in database already and i created a model user and it works perfectly. The problem is with rest of tables like tranz_vehicle

2
table name must be plural vehicles and Model name must be singular - Sharma Vikram
@SharmaVikram i know that constraint but the problem is database is already created with that name and has tons of data. Also many key relationships. So its not possible to change the table name to vehicles. Thats why i am looking for options to make model point another table. - Karthik Nk

2 Answers

3
votes

Is your Model file name is Vehicle.php or vehicle.php ? If it is lowercase that mean your model is generating on fly and cant read useTable from file because it is not using that file. Also can you show your table primary key ? If it is not 'id' you must specify it with this code.

public $primaryKey = 'example_id';
1
votes

Don't forget to empty tmp/cache/models and tmp/cache/persistent before anything.

To force CakePHP to use an other DB you have basically two choices :

1) Use a table Prefix

class Example extends AppModel {
public $tablePrefix = 'alternate_'; // will check 'alternate_examples' table

}

2) Use $useTable like you did

class Exemple extends AppModel {
 public $useTable = 'exmp'; // will check 'exmp' table

}