CakePHP doesn’t like models named “List”

Posted by wanchan on January 17th, 2011 filed in Programming

Creating a gift want-list management app (mostly to learn how to use CakePHP). Naturally, this includes Lists of things a User wants. So, naturally, I called the DB table lists, implying a model named List. Seems fine, right?

When using the ‘cake bake all’ tool, I got a bunch of parse errors to the effect of ‘unexpected token T_LIST, expected T_STRING’ or something close. I forgot to copy/paste that error code before I fixed it ><; I’ll have to go reproduce that later so this will be a useful hit on Google later for the error message.

TL;DR: I tried to use names that were already used by either PHP itself or CakePHP

UPDATE: using php -l to do a compile check on app/models/list.php,  I get the same eror:

php -l models/list.php

Parse error: syntax error, unexpected T_LIST, expecting T_STRING in models/list.php on line 2
Errors parsing models/list.php

Anyway, after changing the DB table to want_lists (and having to change all of the foreign key reference column names from list_id to want_list_id), I deleted all of the model, controller, view and test files, then reran the ‘cake bake all’ commands for each model it detected, the error seems to have disappeared. Testing still required, of course, both to confirm that this is where the error stemmed from, and that my fix did in fact fix it.

TL;DR: CakePHP doesn’t like models named List. Use a different name, like WantList.

UPDATE:  I’ve recreated the error. I’ve done the bake for the User model already; here’s the dump from trying to bake the List model:

Welcome to CakePHP v1.3.6 Console
---------------------------------------------------------------
App : app
Path: /home/wanchan/abandonedfactory.net/gifter_test/app
---------------------------------------------------------------
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
Use Database Config: (default/test)
[default] >
Possible Models based on your current database:
1. Family
2. FamiliesUser
3. FamiliesWantList
4. Item
5. List
6. User
Enter a number from the list above,
type in the name of another model, or 'q' to exit
[q] > 5

Baking model class for List...

Creating file /home/wanchan/abandonedfactory.net/gifter_test/app/models/list.php
Wrote `/home/wanchan/abandonedfactory.net/gifter_test/app/models/list.php`
List Model was baked.
SimpleTest is not installed. Do you want to bake unit test files anyway? (y/n)
[y] >

You can download SimpleTest from http://simpletest.org

Baking test fixture for List...

Creating file /home/wanchan/abandonedfactory.net/gifter_test/app/tests/fixtures/list_fixture.php
Wrote `/home/wanchan/abandonedfactory.net/gifter_test/app/tests/fixtures/list_fixture.php`

Parse error: syntax error, unexpected T_LIST, expecting T_STRING in /home/wanchan/abandonedfactory.net/gifter_test/app/models/list.php on line 2

You’ll subsequently get similar errors when baking any other models that interact with Lists. Again, renaming the model to something else (I used WantList) seems to correct this, so I’m guessing CakePHP has an internal List class that this conflicts with.

I’d propose that the ‘cake bake’ command should be updated to at least warn the user about potential name conflicts. Or, alternatively, a separate ‘cake bake_check’ or something. Even just a simple published list of “names to avoid in PHP/CakePHP/whatever” would be pretty useful. Maybe I’ll start figuring out how to do all that after lunch…

Update 2: Looks to me like the conflict was probably with the PHP builtin function list(). Hmm… I wonder if it’s even possible to have a program generate a list of all names that it can’t use. I’m guessing that the best I’d be able to do is add/modify a bake command to check if any of the classes you’re ordering it to operate on are illegal. Not sure how to do that offhand… maybe have it write some temp code snippet files, then see if any of them generate parser errors like I just encountered?


5 Responses to “CakePHP doesn’t like models named “List””

  1. Anca Says:

    Thanks! Your article was very helpful :) I hope to see that list of “names to avoid” soon..

  2. wanchan Says:

    So far, I’ve also learned that naming a model after a helper or component also causes strange failures, even if you’re not using that component. Specifically, I tried naming a model Session, and that produced some interesting errors. I was not using the Sessions component, which is no longer used by default in CakePHP, but apparently a lot of the core code still assumes it is there if there is a model named Session defined.

    That one was fun to debug.

  3. dogmatic69 Says:

    1) you cant use List because php has a class called List already. nothing to do with cake. this holds true for any model that will have the same name as a class that your already have defined (be it in php or cake or you app)

    2) you cant use things like a Session model with SessionComponent because they are both loaded into $this->Session in the controller.

  4. wanchan Says:

    Nice, error in commenting timestamps. Ugh, that’ll be fun.

    Oh, there’s actually a List class in PHP? I hadn’t found that, but in retrospect I should’ve guessed as much.

    I eventually remembered about $this->Session. I’m mildly annoyed that I didn’t get a more useful error message, but I can live with it, now that I’ve gone through this once.

    Thanks for the clarification!

  5. Heidi Says:

    Thanks! Just had the same problem with a lists table. I realised when comparing the model classes and saw that my IDE had put “List” in bold as if it recognised it as a PHP function, which of course it did. Off to rename my tables/fields as you did now but happy to know it wasn’t just me!

Leave a Comment

*