开发者

Cakephp with underscore table ignores model

开发者 https://www.devze.com 2022-12-27 14:03 出处:网络
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.

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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号