【问题标题】:CodeIgniter DataMapper Unable To Relate tree tablesCodeIgniter DataMapper 无法关联树表
【发布时间】:2013-08-26 12:11:26
【问题描述】:

我有树表:

CREATE TABLE `bairros` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `cidade_id` INT(11) UNSIGNED NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `FK_bairros_cidades` (`cidade_id`),
    CONSTRAINT `FK_bairros_cidades` FOREIGN KEY (`cidade_id`) REFERENCES `cidades` (`id`)
)

CREATE TABLE `cidades` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `estado_id` TINYINT(4) UNSIGNED NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `FK_cidades_estados` (`estado_id`),
CONSTRAINT `FK_cidades_estados` FOREIGN KEY (`estado_id`) REFERENCES `estados` (`id`)
)

CREATE TABLE `estados` (
    `id` TINYINT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
)

在我的 Bairros 课程中,我有一个函数:

public function get_by_id($bairro_id)
{
    $bairro = new Bairro();
    $bairro->include_related('cidade');
    $bairro->include_related('estado');
    return $bairro->get_by_id($bairro_id);
}

在我的模型中,我有:

Bairro 模型:

var $has_one = array(
    'cidade' => array(
        'class' => 'cidade',
        'other_field' => 'bairro',
        'join_other_as' => 'cidade',
        'join_table' => 'bairros'
    )

Cidade 模型:

var $has_one = array(
    'estado' => array(
        'class' => 'estado',
        'other_field' => 'cidade',
        'join_other_as' => 'estado',
        'join_table' => 'cidades'
    )
);
var $has_many = array(
    'bairro' => array(
        'class' => 'bairro',
        'other_field' => 'cidade',
        'join_self_as' => 'cidade',
        'join_table' => 'bairros'
    )
);

Estado 模型

var $has_many = array(
    'cidade' => array(
        'class' => 'cidade',
        'other_field' => 'estado',
        'join_self_as' => 'estado',
        'join_table' => 'cidades'
    )
);

我想制作类似的东西:

SELECT bairros.*, cidades.*, estados.*
FROM bairros
LEFT OUTER
JOIN cidades ON cidades.id = bairros.cidade_id
LEFT OUTER
JOIN estados ON estados.id = cidades.estado_id
WHERE bairros.id = 1;

我收到这条消息:

“无法将 bairro 与 estado 联系起来。”

我该怎么做?

【问题讨论】:

    标签: php codeigniter codeigniter-2 codeigniter-datamapper


    【解决方案1】:

    已解决:

    $pedido->include_related('cidade/estado');
    

    因此可以正常工作。谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2015-12-28
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多