Function name is used as SQL query

I've got a problem very similar to the ones found here and here, but these solutions aren't helping me fix my version of the problem. I'm using CakePHP 1.3 and I'm trying to call some model functions from within a view controller:

//Class setup
var $uses = array('StudentsTelephone', 'AddressesStudent');


function someFunction(){
    $this->set('telephones', $this->StudentsTelephone->getActiveStudentTelepone($id));
    $this->set('addresses', $this->AddressesStudent->getActiveByStudentId($id));

This first of the model function calls (StudentsTelephone) works, but the second (AddressesStudent) fails. It attempts to use the function name as the SQL call resulting in:

SQL Error: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'getActiveByStudentId' at line 1 [CORE\cake\libs\model\datasources\dbo_source.php, line 684]

Using a little bit of debugging, I find out that the function is being looked for in the AppModel and AddressesStudent is never checked. Based on those other solutions it looks like it should be a typo in the name. However, everything appears to be correctly named. The above code is as stated, the model file is addresses_student.php, the class name is AddressesStudent and this was working fine before I updated Cake (just recently switched from 1.1 to 1.3). Any suggestions as to what might be causing the problem is not a naming error? Or maybe some other naming error that I'm missing? Thank you much!

EDIT: Also, here's the backtrace telling me that the model PHP file was never found:

DboSource::showQuery() - CORE\cake\libs\model\datasources\dbo_source.php, line 684
DboSource::execute() - CORE\cake\libs\model\datasources\dbo_source.php, line 266
DboSource::fetchAll() - CORE\cake\libs\model\datasources\dbo_source.php, line 410
DboSource::query() - CORE\cake\libs\model\datasources\dbo_source.php, line 364
Model::call__() - CORE\cake\libs\model\model.php, line 502
Overloadable::__call() - CORE\cake\libs\overloadable_php5.php, line 50
AppModel::getActiveByStudentId() - [internal], line ??
StudentsController::edit() - APP\controllers\students_controller.php, line 277
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 204
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
[main] - APP\webroot\index.php, line 83


CakePHP uses the 'cached' auto-generated Model

For HABTM relations, CakePHP will automatically generate a 'generic' model for the JOIN-table (by creating an instance AppModel) even if a Model exists for the join-table (don't ask me why!)

CakePHP stores this auto-generated model in its cache so that it can be reused in other locations.

In your example, you try to use the AddressesStudent model. CakePHP will look for a model with that name, and finds a model with that name inside the cache. This is, however the auto-generated model for the HABTM relation, not your actual model!

Tell CakePHP to use your model and not an auto-generated model

To prevent CakePHP from auto-generating a model for HABTM relations, add the with option to those relations; be sure to specify this at both sides of the relation (if you defined it at both sides, that is)

Inside the Student model;

$public hasAndBelongsToMany = array(
    'Address' => array(
        // tell CakePHP to use the AddressesStudent model,
        // NOT and auto-generated model
        'with' => 'AddressesStudent',

And/or inside the Address model;

$public hasAndBelongsToMany = array(
    'Student' => array(
        'with' => 'AddressesStudent',

By doing this, you're preventing that the cache contains auto-generated models.;

with: Defines the name of the model for the join table. By default CakePHP will auto-create a model for you. Using the example above it would be called RecipesTag. By using this key you can override this default name. The join table model can be used just like any “regular” model to access the join table directly.

More information; hasAndBelongsToMany (HABTM)

Need Your Help


sql sql-server tsql sql-server-2005

Here is a basic example of what I am trying to achieve:

VB and sql datetime

sql sql-server sql-server-ce

How can I format a date in Visual Basic to be in sql 121 style (that is yyyy-mm-dd hh:mi:ss.mmm).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.