【问题标题】:belongsTo in Cake php蛋糕php中的belongsTo
【发布时间】:2011-05-07 03:27:53
【问题描述】:

我遵循 CakePHP 命名约定

我需要建立“多对一”-$belongsTo 关联 Citie to Countrie --------意思是许多城市属于一个国家

这是城市模型

class Citie extends AppModel
{
    var $name = 'Citie';
    var $belongsTo = array(
        'Countrie' => array(
            'className' => 'Countrie',
            'foreignKey' => 'countrie_id'
        )
    ); 
}

在这个链接返回结果时可以看到没有关联数据 DisplayCity

这是国家模式

class Countrie extends AppModel 
{
    var $name = 'Countrie';
}

在这里您可以看到我遵循命名约定。 Display all countries

【问题讨论】:

    标签: php cakephp belongs-to


    【解决方案1】:

    如果您遵守约定
    所以你必须有城市表的城市模型,国家表的国家模型,foreignKey 将是 country_id

    <?php
    class City extends AppModel {
        var $name = 'City';
        var $belongsTo = array(
            'Country' => array(
                'className' => 'Country',
                'foreignKey' => 'country_id',
            )
        );
    }
    ?>
    

    <?php
    class Country extends AppModel {
        var $name = 'Country';
        var $hasMany = array(
            'City' => array(
                'className' => 'City',
                'foreignKey' => 'country_id',
            )
        );
    
    }
    ?>
    

    【讨论】:

    • Tnx 的答案。但结果是一样的。我的模型文件名称错误。现在好了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多