2013-08-26 16 views
0

我有樹表:笨的DataMapper不懂得如何跟樹表

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' 
    ) 

Cid ADE型號:

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' 
    ) 
); 

埃斯塔型號

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與埃斯塔「。

我該怎麼做?

回答

0

解決:

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

因此正常工作。謝謝