yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

LEFT JOIN #3382

Closed ghost closed 10 years ago

ghost commented 10 years ago

LEFT JOIN not working. should return records even without the relational table.

$cliente = Cliente::model()->with([
            'permitidoBloqueioPeloSistema', // Should return if any "Cliente" even without "permitidoBloqueioPeloSistema"
         ])->together()->findByPk(12);

Same statement in relation to the "joinType" attribute does not return.

Thank You.

samdark commented 10 years ago

Please provide more details:

  1. Which SQL query is executed.
  2. What do you expect.
  3. Your table structure and some sample data.
  4. Relation definitions of your models.

Thanks.

ghost commented 10 years ago
  1. 2014/05/22 06:59:17 [trace] [system.db.CDbCommand] Querying SQL: SELECT t.id AS t0_c0, t.nome AS t0_c1, t.razao_social AS t0_c2, t.cpf_cnpj AS t0_c3, t.rg AS t0_c4, t.inscricao_estadual AS t0_c5, t.orgao_emissor AS t0_c6, t.passaporte AS t0_c7, t.data_nascimento AS t0_c8, t.nome_mae AS t0_c9, t.nome_pai AS t0_c10, t.estado_id AS t0_c11, t.cidade_id AS t0_c12, t.ssid_id AS t0_c13, t.mac AS t0_c14, t.mac_vinculado AS t0_c15, t.ccq AS t0_c16, t.sinal AS t0_c17, t.cep AS t0_c18, t.endereco_bairro AS t0_c19, t.endereco_rua AS t0_c20, t.endereco_numero AS t0_c21, t.endereco_complemento AS t0_c22, t.endereco_latitude AS t0_c23, t.endereco_longitude AS t0_c24, t.endereco_usar_caixa_postal AS t0_c25, t.endereco_numero_caixa_postal AS t0_c26, t.coordenadas_verificadas AS t0_c27, t.modo_pagamento_id AS t0_c28, t.consta_spc_serasa AS t0_c29, t.tipo_conexao AS t0_c30, t.login AS t0_c31, t.situacao_id AS t0_c32, t.numero_bloqueio AS t0_c33, t.plano_id AS t0_c34, t.plano_valor_especial AS t0_c35, t.cobranca_dia_id AS t0_c36, t.senha AS t0_c37, t.equipamento_comodato AS t0_c38, t.ponto_cliente_id AS t0_c39, t.ponto_numero AS t0_c40, t.ip_fixo AS t0_c41, t.modo_envio_cobranca AS t0_c42, t.modo_envio_cobranca_outros_descricao AS t0_c43, t.telefonia_ativa AS t0_c44, t.isento AS t0_c45, t.sistema_externo_id AS t0_c46, t.revenda_id AS t0_c47, t.data_tempo_ativacao AS t0_c48, t.data_tempo AS t0_c49, t.bemtevi_codcliente AS t0_c50, t.bemtevi_endereco_rua AS t0_c51, t.usar_endereco_bemtevi AS t0_c52, t.bloquear_automaticamente AS t0_c53, permitidoBloqueioPeloSistema.id AS t1_c0, permitidoBloqueioPeloSistema.cliente_id AS t1_c1, permitidoBloqueioPeloSistema.ate_data AS t1_c2, permitidoBloqueioPeloSistema.data_tempo AS t1_c3 FROM radcliente t LEFT OUTER JOIN radcliente_impedir_bloqueio_automatico_sistema permitidoBloqueioPeloSistema ON (permitidoBloqueioPeloSistema.cliente_id=t.id) WHERE ((t.revenda_id IN (:ycp122, :ycp123, :ycp124, :ycp125, :ycp126, :ycp127, :ycp128, :ycp129, :ycp130, :ycp131, :ycp132, :ycp133, :ycp134, :ycp135, :ycp136, :ycp137, :ycp138, :ycp139, :ycp140, :ycp141, :ycp142, :ycp143, :ycp144, :ycp145)) AND (t.id=12)) AND (permitidoBloqueioPeloSistema.ate_data >= '2014-05-22'). Bound with :ycp122='1', :ycp123='2', :ycp124='3', :ycp125='4', :ycp126='5', :ycp127='6', :ycp128='7', :ycp129='8', :ycp130='9', :ycp131='10', :ycp132='11', :ycp133='12', :ycp134='13', :ycp135='14', :ycp136='15', :ycp137='16', :ycp138='17', :ycp139='18', :ycp140='19', :ycp141='20', :ycp142='21', :ycp143='22', :ycp144='23', :ycp145='24'
  2. I define "with" and "together" to do everything in the same query, and then when I treat "permitidoBloqueioPeloSistema" do not run the query lazy. Only if no table lists a "permitidoBloqueioPeloSistema" record also does not return "Cliente".
  3. Attached is the schematic of the two tables and records. If you search the Customer by id returns 1 because the table "radcliente_impedir_bloqueio_automatico_sistema". There is a related record. If you look at the "Cliente" id 2, the "Cliente" does not return, because there is no connection of it on the table "radcliente_impedir_bloqueio_automatico_sistema".
CREATE TABLE IF NOT EXISTS `radcliente` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(100) CHARACTER SET latin1 NOT NULL,
  `razao_social` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `cpf_cnpj` varchar(20) CHARACTER SET latin1 NOT NULL,
  `rg` varchar(20) CHARACTER SET latin1 NOT NULL,
  `inscricao_estadual` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `orgao_emissor` varchar(20) CHARACTER SET latin1 NOT NULL,
  `passaporte` varchar(100) DEFAULT NULL,
  `data_nascimento` date NOT NULL,
  `nome_mae` varchar(100) CHARACTER SET latin1 NOT NULL,
  `nome_pai` varchar(100) CHARACTER SET latin1 NOT NULL,
  `estado_id` int(11) DEFAULT NULL,
  `cidade_id` int(11) DEFAULT NULL,
  `ssid_id` int(11) DEFAULT NULL,
  `mac` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `mac_vinculado` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `ccq` tinyint(3) DEFAULT NULL,
  `sinal` tinyint(3) DEFAULT NULL,
  `cep` varchar(45) CHARACTER SET latin1 NOT NULL,
  `endereco_bairro` varchar(45) CHARACTER SET latin1 NOT NULL,
  `endereco_rua` varchar(45) CHARACTER SET latin1 NOT NULL,
  `endereco_numero` varchar(45) CHARACTER SET latin1 NOT NULL,
  `endereco_complemento` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `endereco_latitude` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `endereco_longitude` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `endereco_usar_caixa_postal` tinyint(4) NOT NULL DEFAULT '0',
  `endereco_numero_caixa_postal` int(3) NOT NULL,
  `coordenadas_verificadas` tinyint(1) NOT NULL DEFAULT '0',
  `modo_pagamento_id` tinyint(2) NOT NULL DEFAULT '1',
  `consta_spc_serasa` tinyint(1) NOT NULL DEFAULT '0',
  `tipo_conexao` varchar(20) CHARACTER SET latin1 NOT NULL,
  `login` varchar(100) CHARACTER SET latin1 NOT NULL,
  `situacao_id` int(11) NOT NULL,
  `numero_bloqueio` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Se o cliente estiver bloqueado, esse campo indica qual o numero do bloqueio do cliente. Se é o primeiro (1) ou o segundo (2).',
  `plano_id` int(11) DEFAULT NULL,
  `plano_valor_especial` varchar(45) NOT NULL DEFAULT '0,00',
  `cobranca_dia_id` int(11) DEFAULT NULL,
  `senha` varchar(100) CHARACTER SET latin1 NOT NULL,
  `equipamento_comodato` tinyint(1) NOT NULL DEFAULT '1',
  `ponto_cliente_id` int(11) DEFAULT '0',
  `ponto_numero` int(11) DEFAULT NULL,
  `ip_fixo` varchar(45) CHARACTER SET latin1 DEFAULT NULL COMMENT 'IP fixo do cliente, se existir.',
  `modo_envio_cobranca` tinyint(4) NOT NULL DEFAULT '1',
  `modo_envio_cobranca_outros_descricao` varchar(100) NOT NULL,
  `telefonia_ativa` tinyint(4) NOT NULL DEFAULT '0',
  `isento` tinyint(1) NOT NULL DEFAULT '0',
  `sistema_externo_id` int(11) DEFAULT NULL,
  `revenda_id` int(11) NOT NULL DEFAULT '1',
  `data_tempo_ativacao` datetime DEFAULT NULL,
  `data_tempo` datetime NOT NULL,
  `bemtevi_codcliente` int(11) DEFAULT NULL,
  `bemtevi_endereco_rua` varchar(100) DEFAULT NULL,
  `usar_endereco_bemtevi` int(1) NOT NULL DEFAULT '1',
  `bloquear_automaticamente` tinyint(4) DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `radius_fk_cliente_x_estado` (`estado_id`),
  KEY `radius_fk_cliente_x_cidade` (`cidade_id`),
  KEY `radius_fk_cliente_x_cobranca_dia` (`cobranca_dia_id`),
  KEY `radius_fk_cliente_x_plano` (`plano_id`),
  KEY `nome` (`nome`),
  KEY `cpf_cnpj` (`cpf_cnpj`),
  KEY `nome_mae` (`nome_mae`),
  KEY `nome_mae_2` (`nome_mae`),
  KEY `data_nascimento` (`data_nascimento`),
  KEY `mac` (`mac`),
  KEY `sinal` (`sinal`),
  KEY `ccq` (`ccq`),
  KEY `cep` (`cep`),
  KEY `telefonia_ativa` (`telefonia_ativa`),
  KEY `revenda_id` (`revenda_id`),
  KEY `data_tempo` (`data_tempo`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6095 ;

INSERT INTO `radcliente` (`id`, `nome`, `razao_social`, `cpf_cnpj`, `rg`, `inscricao_estadual`, `orgao_emissor`, `passaporte`, `data_nascimento`, `nome_mae`, `nome_pai`, `estado_id`, `cidade_id`, `ssid_id`, `mac`, `mac_vinculado`, `ccq`, `sinal`, `cep`, `endereco_bairro`, `endereco_rua`, `endereco_numero`, `endereco_complemento`, `endereco_latitude`, `endereco_longitude`, `endereco_usar_caixa_postal`, `endereco_numero_caixa_postal`, `coordenadas_verificadas`, `modo_pagamento_id`, `consta_spc_serasa`, `tipo_conexao`, `login`, `situacao_id`, `numero_bloqueio`, `plano_id`, `plano_valor_especial`, `cobranca_dia_id`, `senha`, `equipamento_comodato`, `ponto_cliente_id`, `ponto_numero`, `ip_fixo`, `modo_envio_cobranca`, `modo_envio_cobranca_outros_descricao`, `telefonia_ativa`, `isento`, `sistema_externo_id`, `revenda_id`, `data_tempo_ativacao`, `data_tempo`, `bemtevi_codcliente`, `bemtevi_endereco_rua`, `usar_endereco_bemtevi`, `bloquear_automaticamente`) VALUES
(1, 'ABEL DE FREITAS PORTALUPI', '', '008.297.790-94', '', '', '', NULL, '0000-00-00', '', '', 21, 1, NULL, NULL, NULL, NULL, NULL, '96690000', 'CENTRO', 'RUA PAPA JOÃO XXIII', '404', '', NULL, NULL, 0, 0, 0, 1, 0, 'ppp', '', 1, 0, NULL, '0,00', 1, '123456', 1, 0, NULL, '0', 1, '', 0, 0, NULL, 1, '0000-00-00 00:00:00', '2010-12-06 14:03:17', 770, 'RUA PAPA JOÃO XXIII, 404', 1, 1);
INSERT INTO `radcliente` (`id`, `nome`, `razao_social`, `cpf_cnpj`, `rg`, `inscricao_estadual`, `orgao_emissor`, `passaporte`, `data_nascimento`, `nome_mae`, `nome_pai`, `estado_id`, `cidade_id`, `ssid_id`, `mac`, `mac_vinculado`, `ccq`, `sinal`, `cep`, `endereco_bairro`, `endereco_rua`, `endereco_numero`, `endereco_complemento`, `endereco_latitude`, `endereco_longitude`, `endereco_usar_caixa_postal`, `endereco_numero_caixa_postal`, `coordenadas_verificadas`, `modo_pagamento_id`, `consta_spc_serasa`, `tipo_conexao`, `login`, `situacao_id`, `numero_bloqueio`, `plano_id`, `plano_valor_especial`, `cobranca_dia_id`, `senha`, `equipamento_comodato`, `ponto_cliente_id`, `ponto_numero`, `ip_fixo`, `modo_envio_cobranca`, `modo_envio_cobranca_outros_descricao`, `telefonia_ativa`, `isento`, `sistema_externo_id`, `revenda_id`, `data_tempo_ativacao`, `data_tempo`, `bemtevi_codcliente`, `bemtevi_endereco_rua`, `usar_endereco_bemtevi`, `bloquear_automaticamente`) VALUES
(2, 'ADELINO BERTUDI TEIXEIRA NUNES', '', '424.736.320-49', '', '', '', NULL, '2013-08-19', '', '', 21, 1, NULL, NULL, NULL, 56, 96, '96690000', 'CENTRO', 'RUA OLAVO BILAC', '414', '', NULL, NULL, 0, 0, 0, 2, 0, 'ppp', 'adelino@kzbrazil.com', 1, 0, 3, '0,00', 1, '123456', 1, 0, NULL, '0', 1, '', 0, 0, NULL, 1, '0000-00-00 00:00:00', '2010-12-06 14:03:18', 771, 'RUA OLAVO BILAC, 414', 1, 1);

ALTER TABLE `radcliente`
  ADD CONSTRAINT `radius_fk_cliente_x_cidade` FOREIGN KEY (`cidade_id`) REFERENCES `radcidade` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `radius_fk_cliente_x_cobranca_dia` FOREIGN KEY (`cobranca_dia_id`) REFERENCES `radcobranca_dia` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `radius_fk_cliente_x_estado` FOREIGN KEY (`estado_id`) REFERENCES `radestado` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  ADD CONSTRAINT `radius_fk_cliente_x_plano` FOREIGN KEY (`plano_id`) REFERENCES `radplano` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE TABLE IF NOT EXISTS `radcliente_impedir_bloqueio_automatico_sistema` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cliente_id` int(11) NOT NULL,
  `ate_data` date NOT NULL,
  `data_tempo` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

INSERT INTO  `radius5`.`radcliente_impedir_bloqueio_automatico_sistema` (
`id` ,
`cliente_id` ,
`ate_data` ,
`data_tempo`
)
VALUES (
NULL ,  '1',  '2014-05-22', NULL
);
  1. No model "Cliente" está é a relação:
            'permitidoBloqueioPeloSistema' => [self::HAS_ONE, 'ClienteImpedirBloqueioAutomaticoSistema', 'cliente_id', 'condition' => 'permitidoBloqueioPeloSistema.ate_data >= \'' . date('Y-m-d') . '\''],

This is an example. But there is another: the "Client" has two forms of payments, "faturas" and "boletos", and I need to do together, but then it just returns if the "client" has at least an "fatura" and a "boleto" slip.

Thanks.

klimov-paul commented 10 years ago

You have added a 'where' condition for the table you joined. This elimitates the feature of LEFT JOIN to select empty values from joined table - such results will be filtered by your where condition. You should use 'on' to specify additional join condition.

There is no framework issue here.

ghost commented 10 years ago

It's Works. Thanks.