Conheça  A Ferramenta LowCode mais moderna e veloz para desenvolvimento PHP: Adianti Creator
Exibir Nome em vez de ID, no formulário de consulta Mestre Detal Não sei o que está a se passar, não consigo exibir nome das tabelas relacionadas usando esta técnica: Exemplo Tomador->nome. Usando este método na tela aparece sem informação, conforme mostra a imagem... Help Me Segue o codigo ...
AE
Exibir Nome em vez de ID, no formulário de consulta Mestre Detal  
Não sei o que está a se passar, não consigo exibir nome das tabelas relacionadas usando esta técnica: Exemplo Tomador->nome.
Usando este método na tela aparece sem informação, conforme mostra a imagem... Help Me

Segue o codigo

 
  1. <?php
  2. $text_codigo = new TTextDisplay($master_object->codigo, '#333333', '14px', '');
  3. $text_valor_premio = new TTextDisplay(number_format($master_object->valor_premio,'2',',','.'), '#333333', '14px', '');
  4. //$text_forma_pagamento = new TTextDisplay($master_object->forma_pagamento, '#333333', '14px', '');
  5. $text_data_inicio = new TTextDisplay($master_object->data_inicio, 'red', '14px', '');
  6. $text_data_fim = new TTextDisplay($master_object->data_fim, 'red', '14px', '');
  7. $text_tempo = new TTextDisplay($master_object->tempo, '#333333', '14px', '');
  8. $text_tipo_apolice = new TTextDisplay($master_object->tipo_apolice, '#333333', '14px', '');
  9. $text_codigo_plano = new TTextDisplay($master_object->codigo_plano, '#333333', '14px', '');
  10. $text_codigo_tomador = new TTextDisplay($master_object->Tomador->nome, '#333333', '14px', ''); // EXEMPLO
  11. $text_codigo_funcionario = new TTextDisplay($master_object->codigo_funcionario, '#333333', '14px', '');
  12. ?>

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (7)


MG

Bom dia.
Os models possuem os relacionamentos?
O campo Tomador é com "T" maiúsculo mesmo?
AE

Sim, os models possuem relacionamentos. O Tomador tanto com letra maiúscula e minúscula retorna valor null... Já tentei de várias formas não sei onde estou a falhar.
MG

Só mais uma pergunta, este código está no método __consruct()??
Teria como postar a classe para podermos tentar ajudar?
AE

Sim, em anexo o código.

CLASSE DE MODELO

 
  1. <?php
  2. /**
  3. * Apolice Active Record
  4. * @author <your-name-here>
  5. */
  6. class Apolice extends TRecord
  7. {
  8. const TABLENAME = 'apolice';
  9. const PRIMARYKEY= 'codigo';
  10. const IDPOLICY = 'max'; // {max, serial}
  11. private $tomador;
  12. private $funcionario;
  13. private $plano_seguro;
  14. /**
  15. * Constructor method
  16. */
  17. public function __construct($id = NULL, $callObjectLoad = TRUE)
  18. {
  19. parent::__construct($id, $callObjectLoad);
  20. parent::addAttribute('valor_premio');
  21. //parent::addAttribute('forma_pagamento');
  22. parent::addAttribute('data_inicio');
  23. parent::addAttribute('data_fim');
  24. parent::addAttribute('tempo');
  25. parent::addAttribute('tipo_apolice');
  26. parent::addAttribute('referencia');
  27. parent::addAttribute('codigo_plano');
  28. parent::addAttribute('codigo_tomador');
  29. parent::addAttribute('codigo_funcionario');
  30. parent::addAttribute('estado_apolice');
  31. }
  32. /**
  33. * Method set_tomador
  34. * Sample of usage: $apolice->tomador = $object;
  35. * @param $object Instance of Tomador
  36. */
  37. public function set_tomador(Tomador $object)
  38. {
  39. $this->tomador = $object;
  40. $this->tomador_id = $object->id;
  41. }
  42. /**
  43. * Method get_tomador
  44. * Sample of usage: $apolice->tomador->attribute;
  45. * @returns Tomador instance
  46. */
  47. public function get_tomador()
  48. {
  49. // loads the associated object
  50. if (empty($this->tomador))
  51. $this->tomador = new Tomador($this->tomador_id);
  52. // returns the associated object
  53. return $this->tomador;
  54. }
  55. /**
  56. * Method set_funcionario
  57. * Sample of usage: $apolice->funcionario = $object;
  58. * @param $object Instance of Funcionario
  59. */
  60. public function set_funcionario(Funcionario $object)
  61. {
  62. $this->funcionario = $object;
  63. $this->funcionario_id = $object->id;
  64. }
  65. /**
  66. * Method get_funcionario
  67. * Sample of usage: $apolice->funcionario->attribute;
  68. * @returns Funcionario instance
  69. */
  70. public function get_funcionario()
  71. {
  72. // loads the associated object
  73. if (empty($this->funcionario))
  74. $this->funcionario = new Funcionario($this->funcionario_id);
  75. // returns the associated object
  76. return $this->funcionario;
  77. }
  78. /**
  79. * Method set_plano_seguro
  80. * Sample of usage: $apolice->plano_seguro = $object;
  81. * @param $object Instance of PlanoSeguro
  82. */
  83. public function set_plano_seguro(PlanoSeguro $object)
  84. {
  85. $this->plano_seguro = $object;
  86. $this->plano_seguro_id = $object->id;
  87. }
  88. /**
  89. * Method get_plano_seguro
  90. * Sample of usage: $apolice->plano_seguro->attribute;
  91. * @returns PlanoSeguro instance
  92. */
  93. public function get_plano_seguro()
  94. {
  95. // loads the associated object
  96. if (empty($this->plano_seguro))
  97. $this->plano_seguro = new PlanoSeguro($this->plano_seguro_id);
  98. // returns the associated object
  99. return $this->plano_seguro;
  100. }
  101. //METODO USER
  102. public function set_system_user(SystemUser $object)
  103. {
  104. $this->system_user = $object;
  105. $this->system_user_id = $object->id;
  106. }
  107. public function get_system_user()
  108. {
  109. // loads the associated object
  110. if (empty($this->system_user))
  111. $this->system_user = new SystemUser($this->system_user_id);
  112. // returns the associated object
  113. return $this->system_user;
  114. }
  115. }
  116. ?>
AE


FORMULÁRIO DA VIEW

 
  1. <?php
  2. /**
  3. * ApoliceFormView Master/Detail
  4. * @author <your name here>
  5. */
  6. class ApoliceFormView extends TPage
  7. {
  8. protected $form; // form
  9. protected $detail_list;
  10. /**
  11. * Page constructor
  12. */
  13. public function __construct($param)
  14. {
  15. parent::__construct();
  16. parent::setTargetContainer('adianti_right_panel');
  17. TTransaction::open('sgs');
  18. $this->form = new BootstrapFormBuilder('form_Apolice');
  19. $this->form->setFormTitle('APÓLICE');
  20. $master_object = new Apolice($param['key']);
  21. //$master_object['codigo_plano'] = PlanoSeguro::find(object->codigo_plano)->designacao;
  22. $label_codigo = new TLabel('Código:', '#333333', '14px', '');
  23. $label_valor_premio = new TLabel('Prémio:', '#333333', '14px', '');
  24. $label_forma_pagamento = new TLabel('Forma de Pagto:', '#333333', '14px', '');
  25. $label_data_inicio = new TLabel('Início:', '#333333', '14px', '');
  26. $label_data_fim = new TLabel('Fim:', '#333333', '14px', '');
  27. $label_tempo = new TLabel('Tempo:', '#333333', '14px', '');
  28. $label_tipo_apolice = new TLabel('Tipo:', '#333333', '14px', '');
  29. $label_codigo_plano = new TLabel('Plano:', '#333333', '14px', '');
  30. $label_codigo_tomador = new TLabel('Tomador:', '#333333', '14px', '');
  31. $label_codigo_funcionario = new TLabel('Operador:', '#333333', '14px', '');
  32. $text_codigo = new TTextDisplay($master_object->codigo, '#333333', '14px', '');
  33. $text_valor_premio = new TTextDisplay(number_format($master_object->valor_premio,'2',',','.'), '#333333', '14px', '');
  34. //$text_forma_pagamento = new TTextDisplay($master_object->forma_pagamento, '#333333', '14px', '');
  35. $text_data_inicio = new TTextDisplay($master_object->data_inicio, 'red', '14px', '');
  36. $text_data_fim = new TTextDisplay($master_object->data_fim, 'red', '14px', '');
  37. $text_tempo = new TTextDisplay($master_object->tempo, '#333333', '14px', '');
  38. $text_tipo_apolice = new TTextDisplay($master_object->tipo_apolice, '#333333', '14px', '');
  39. $text_codigo_plano = new TTextDisplay($master_object->codigo_plano, '#333333', '14px', '');
  40. $text_codigo_tomador = new TTextDisplay($master_object->tomador->nome, '#333333', '14px', '');
  41. $text_codigo_funcionario = new TTextDisplay($master_object->codigo_funcionario, '#333333', '14px', '');
  42. $row1 = $this->form->addFields([$label_codigo],[$text_codigo],[$label_valor_premio],[$text_valor_premio]);
  43. //$this->form->addFields([$label_forma_pagamento],[$text_forma_pagamento]);
  44. $row2 = $this->form->addFields([$label_data_inicio],[$text_data_inicio],[$label_data_fim],[$text_data_fim]);
  45. $row3 = $this->form->addFields([$label_tempo],[$text_tempo],[$label_tipo_apolice],[$text_tipo_apolice]);
  46. $row4 = $this->form->addFields([$label_codigo_plano],[$text_codigo_plano],[$label_codigo_tomador],[$text_codigo_tomador]);
  47. $this->form->addFields([$label_codigo_funcionario],[$text_codigo_funcionario]);
  48. $this->detail_list = new TQuickGrid;
  49. $this->detail_list->style = 'width:100%';
  50. $this->detail_list->disableDefaultClick();
  51. $segurado = $this->detail_list->addQuickColumn('Beneficiário', 'codigo_segurado', 'left');
  52. $segurado->setTransformer(function($value){
  53. return Segurado::findInTransaction('sgs',$value)->nome;
  54. });
  55. $this->detail_list->createModel();
  56. $items = ApoliceSegurado::where('codigo_apolice', '=', $master_object->codigo)->load();
  57. $this->detail_list->addItems($items);
  58. $icon = new TImage('fa:cubes #000000');
  59. $title = new TTextDisplay("{$icon} Itens da Apólice", '#333333', '15px', '');
  60. $panel = new TPanelGroup($title, '#f5f5f5');
  61. $panel->class = 'panel panel-default formView-detail';
  62. $panel->add(new BootstrapDatagridWrapper($this->detail_list));
  63. $this->form->addContent([$panel]);
  64. //$btnLabel = new TLabel('Editar')
  65. $this->form->addHeaderAction('Editar', new TAction(['ApoliceForm', 'onEdit'],['key'=>$master_object->codigo]), 'fa:pencil-square-o blue');
  66. $this->form->addHeaderAction('Nova', new TAction(['ApoliceForm', 'onShow']), 'fa:plus blue');
  67. $this->form->addHeaderActionLink( _t('Close'), new TAction([$this, 'onClose']), 'fa:times red');
  68. // vertical box container
  69. $container = new TVBox;
  70. $container->style = 'width: 100%';
  71. // $container->add(new TXMLBreadCrumb('menu.xml', 'ApoliceList'));
  72. $container->add($this->form);
  73. TTransaction::close();
  74. parent::add($container);
  75. }
  76. public function onLoad($param)
  77. {
  78. }
  79. public static function onClose($param)
  80. {
  81. TScript::create("Template.closeRightPanel()");
  82. }
  83. }
  84. ?>
</your>
MG

Veja, no seu relacionamento você faz a ligação com o atributo $this->tomador_id,
porém o atributo listado é código_tomador
Tente substituir

De :
$this->tomador = new Tomador($this->tomador_id);


Por:
$this->tomador = new Tomador($this->codigo_tomador);



AE

Okay funcionou, muito obrigado Marcelo Gomes... Continua assim.