cfwheels

CFWheels: Model FindAll() appends letter "s" in related table name


checklist = model("user_checklist").findAll(include="tb_machine_checklist");

Above is the query I am using to fetch records from a table called user_checklist and it is related to table called tb_machine_checklist.

Now there is a table called "tb_machine_checklist", however it it gives me an error saying;

The "tb_machine_checklists" table could not be found in the database.

Why is the "s" being added when I didn't specify?


Solution

  • Be sure to read the chapter in the documentation about Conventions.

    CFWheels adopts a Rails-style naming conventions for database tables and model files. Think of a database table as a collection of model objects; therefore, it is named with a plural name. Think of a model object as a representation of a single record from the database table; therefore, it is named with a singular word.

    Fortunately, CFWheels lets you override most conventions with a little bit of extra code.

    In your tb_machine_checklist model's init method, add this line of code:

    <cffunction name="init">
      <cfset table("tb_machine_checklist")>
    </cffunction>
    

    Then any queries that CFWheels writes for you will use your singular table name instead of the conventional plural one.