tags:

views:

141

answers:

2

Hi,

I have one table metrics and another table metric_levels. In the Metric model I have $hasMany = 'MetricLevels' but anything inside the MetricLevels model is totally ignored.

But, if I rename the model to 'Metric' and have $useTable = 'metric_levels' it works fine. I've tried all combinations, but none seem to work. I've tried plurals, singulars, underscores, nothing seems to work. The model is simply ignored and it just takes the value from the database.

Any help is appreciated.

+4  A: 

It's not terribly clear what your specific problem is, so just a few pointers:

  • Model names are always singular
    • Metric hasMany MetricLevel
  • Table names are plural and are translated into singular models, underscores converted to CamelCase
    • MetricLevel => metric_levels
  • Even if you didn't create a model, Cake will let you use it and make one up on the fly for you, inferring table names by above naming conventions
  • If Cake "ignores" your model, it means it's making up another model on the fly, because you're not using the right name for the model you actually want
  • Looking at the generated SQL queries in debug = 2 helps

http://book.cakephp.org/view/24/Model-and-Database-Conventions

deceze
Hi deceze,Thanks for your reply.I'm sure the problem is Cake is making up the model on the fly and ignoring the one I made. I tried to make one in the /app/models folder, but it gets ignored anyway.I have tried calling the actual model filename MetricLevel.php, MetricLevels.php, metric_level.php and metric_levels.php and none of them work. I have tried the same with naming the actual model.If I create a model with a single name and use the $useTable = 'metric_levels' it works and then point the Metric model's $hasMany to the single name model it works.
xiaohouzi79
A: 

It is working now. Here is how it is:

// metric.php
var $hasMany = 'MetricLevel'

// metric_level.php
<?php
    class MetricLevel extends AppModel {
        var $name = 'MetricLevel';
    var $useTable = 'metric_levels';
    var $order = 'upper_value DESC';
    var $belongsTo = 'colour';
    }
?>

The $order = 'upper_value DESC' is still being ignored, I don't know why. But at least it's now using the correct model.

xiaohouzi79