Caros colegas.
Estou com dificuldades em obter o total de registros de uma tabela B relacionados a um registro da tabela A. No model foram criados os relacionamentos automaticamente pelo GII, sendo que nas operações baseados na tabela B, estou conseguindo obter as informações da tabela A sem problemas.
O problema ocorre quando ao mostrar as informações da tabela A, tento obter o total de registros da tabela B relacionados aquele registros para informar o total de notas abertas para aquele registro.
No modelo da tabela A (model Chamados) está com o seguintes relacionamentos.
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'callStatus' => array(self::BELONGS_TO, 'SiteTypes', 'call_status'),
'callDevice' => array(self::BELONGS_TO, 'SiteTypes', 'call_device'),
'callDepartment' => array(self::BELONGS_TO, 'SiteTypes', 'call_department'),
'callRequest' => array(self::BELONGS_TO, 'SiteTypes', 'call_request'),
'callStaff' => array(self::BELONGS_TO, 'SiteTypes', 'call_staff'),
'siteNotes' => array(self::HAS_MANY, 'SiteNotes', 'note_relation'),
'notasTotal' => array(self::HAS_MANY, 'SiteNotes', 'note_relation',
'select' => 'COUNT(note_id) AS totalNotas',
),
);
}
No modelo da tabela B (model SiteNotes) está com o seguinte relacionamento.
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'noteRelation' => array(self::BELONGS_TO, 'SiteCalls', 'note_relation'),
);
}
2876
Conforme em outros tópicos disponíveis aqui, habilitei o YII para gerar os logs dos comando SQL, o qual para minha surpresa está efetuando a consulta que espero.
[sql]
end:system.db.CDbCommand.query(SELECT COUNT(note_id) AS totalNotas,
notasTotal
.note_id
AS t1_c0
FROM site_notes
notasTotal
WHERE
(notasTotal
.note_relation
=:ypl0). Bound with :ypl0=‘497’)
[/sql]
Peguei a consulta gerada e fiz o ajuste que o YII iria fazer e executei, sendo que trouxe a informação que eu espera. O problema é ao fazer a referencia $data->notasTotal->totalNotas, a aplicação está apresentando uma exceção "Trying to get property of non-object".
Gostaria de saber se estou fazendo da maneira correta ou existe outra forma de obter essa informação?
Estou enviando em anexo uma figura com as tabelas e suas relações.
Obrigado.