Dúvida sobre TDBSeekButton Pessoal, me deparei com uma situação que não estou conseguindo uma solução: Neste TDBSeekButton coloquei o campo "description_codprod" (concatenação de codigo+description) que na verdade é um get na minha model Product , funciona perfeitamente até eu fazer uma pesquisa, aí recebo a mensagem que o campo não existe. Como posso resolver ? já tentei usar TCriteria mais não tive sucesso...
AR
Dúvida sobre TDBSeekButton  
Fechado
Pessoal, me deparei com uma situação que não estou conseguindo uma solução:
Neste TDBSeekButton coloquei o campo "description_codprod" (concatenação de codigo+description) que na verdade é um get na minha model Product , funciona perfeitamente até eu fazer uma pesquisa, aí recebo a mensagem que o campo não existe. Como posso resolver ? já tentei usar TCriteria mais não tive sucesso.
Grato.


 
  1. <?php $product_id = new ">TDBSeekButton('product_id', 'catalogo', $this->form->getName(), 'Product', 'description_codprod', 'product_id', 'product_name',$criteria);?>

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 (5)


NR

Acredito que você tenha que criar uma seek manual. A TDBSeekButton usa o mesmo parâmetro para exibição e busca...
AR

Grato Nataniel. Vou tentar
AR

Ficou perfeito Nataniel, muito grato.
EE

Adriano, também tenho a mesma dificuldade. Poderia postar sua solução?
Obrigado.
AR

Olá Emanoel, vamos lá:
Vou tentar explicar, sou iniciante em PHP .

No primeiro bloco é o meu form, onde instancio ProductSeek que uso para pesquisa pelo o ID e ProductSeekCodigo para pesquisa pelo codigo, esse criado em minha tabela/model (Product como index unique) e consequentemente em SaleItem .

Então, no meu form SaleForm, posso chamar o produto tanto pelo o ID quanto pelo codigo, em campos distintos. Ficou legal. Se a sua dúvida persistir, estou a disposição.

Tudo exemplificado no Tutor:
//-------------------------------------
//Sale form (Master/detail)
www.adianti.com.br/framework_files/tutor/index.php?class=SaleForm&am
//-------------------------------------
//Seek manual
www.adianti.com.br/framework_files/tutor/index.php?class=FormSeekBut



//bl 1
 
  1. <?php
  2. //SaleForm
  3. $product_id = new TSeekButton('product_id');
  4. $product_name = new TEntry('product_name');
  5. $codigo = new TSeekButton('product_codigo');
  6. // define the action for city_id1
  7. $obj = new ProductSeek;
  8. $action = new TAction(array($obj, 'onReload'));
  9. $product_id->setAction($action);
  10. //preciso mexer
  11. $obj2 = new ProductSeekCodigo;
  12. $action2 = new TAction(array($obj2, 'onReload'));
  13. $codigo->setAction($action2);
  14. //--------------------------------------
  15. $subtable_product->addRowSet( $label_product = new TLabel('Produto (*)'), array($product_id,$product_name) );
  16. $subtable_product->addRowSet( $label_codigo = new TLabel('Codigo (*)'), array($codigo));
  17. ?>



//bl2
 
  1. <?php
  2. /**
  3. * Product Seek
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage tutor
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class ProductSeekCodigo extends TWindow
  13. {
  14. private $form; // form
  15. private $datagrid; // datagrid
  16. private $pageNavigation;
  17. private $loaded;
  18. /**
  19. * Class constructor
  20. * Creates the page, the search form and the listing
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. parent::setSize(700, 500);
  26. parent::setTitle('Search record');
  27. new TSession;
  28. // creates the form
  29. $this->form = new TQuickForm('form_search_product');
  30. $this->form->class = 'tform';
  31. $this->form->setFormTitle('Produtos');
  32. // create the form fields
  33. $codigo = new TEntry('codigo');
  34. $codigo->setValue(TSession::getValue('product_codigo'));
  35. $description = new TEntry('description');
  36. $description->setValue(TSession::getValue('product_name'));
  37. // add the form fields
  38. $this->form->addQuickField('Código', $codigo, 60);
  39. $this->form->addQuickField('Descrição', $description, 200);
  40. // define the form action
  41. $this->form->addQuickAction('Find', new TAction(array($this, 'onSearch')), 'ico_find.png');
  42. // creates a DataGrid
  43. $this->datagrid = new TQuickGrid;
  44. $this->datagrid->style = 'width: 100%';
  45. $this->datagrid->setHeight(230);
  46. $this->datagrid->enablePopover('Title', 'Descrição {description}');
  47. // creates the datagrid columns
  48. $this->datagrid->addQuickColumn('Id', 'id', 'right', 40);
  49. $this->datagrid->addQuickColumn('Código', 'codigo', 'right', 40);
  50. $this->datagrid->addQuickColumn('Descrição', 'description', 'left', 340);
  51. // creates two datagrid actions
  52. $this->datagrid->addQuickAction('Select', new TDataGridAction(array($this, 'onSelect')), 'codigo', 'ico_apply.png');
  53. // create the datagrid model
  54. $this->datagrid->createModel();
  55. // creates the page navigation
  56. $this->pageNavigation = new TPageNavigation;
  57. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  58. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  59. // creates a container
  60. $container = new TVBox;
  61. $container->style = 'width: 100%';
  62. $container->add($this->form);
  63. $container->add($this->datagrid);
  64. $container->add($this->pageNavigation);
  65. // add the container inside the page
  66. parent::add($container);
  67. }
  68. /**
  69. * method onSearch()
  70. * Register the filter in the session when the user performs a search
  71. */
  72. function onSearch()
  73. {
  74. // get the search form data
  75. $data = $this->form->getData();
  76. // TSession::setValue('product_name', NULL);
  77. // TSession::setValue('product_codigo', NULL);
  78. TSession::setValue('product_filter', NULL);
  79. // check if the user has filled the form
  80. if (isset($data->description) and ($data->description))
  81. {
  82. // creates a filter using what the user has typed
  83. $filter = new TFilter('description', 'like', "%{$data->description}%");
  84. // stores the filter in the session
  85. TSession::setValue('product_filter', $filter);
  86. TSession::setValue('product_name', $data->description);
  87. // fill the form with data again
  88. $this->form->setData($data);
  89. }
  90. // if (isset($data->codigo) and $data->codigo)
  91. if ($data->codigo)
  92. {
  93. // creates a filter using what the user has typed
  94. $filter = new TFilter('codigo', '=', "{$data->codigo}");
  95. // stores the filter in the session
  96. TSession::setValue('product_filter', $filter);
  97. TSession::setValue('product_codigo', $data->codigo);
  98. // fill the form with data again
  99. $this->form->setData($data);
  100. }
  101. // redefine the parameters for reload method
  102. $param=array();
  103. $param['offset'] =0;
  104. $param['first_page']=1;
  105. $this->onReload($param);
  106. }
  107. /**
  108. * Load the datagrid with the database objects
  109. */
  110. function onReload($param = NULL)
  111. {
  112. try
  113. {
  114. // open a transaction with database 'samples'
  115. TTransaction::open('catalogo');
  116. // creates a repository for Product
  117. $repository = new TRepository('Product');
  118. $limit = 10;
  119. // creates a criteria
  120. $criteria = new TCriteria;
  121. // default order
  122. if (!isset($param['order']))
  123. {
  124. $param['order'] = 'id';
  125. $param['direction'] = 'asc';
  126. }
  127. $criteria->setProperties($param); // order, offset
  128. $criteria->setProperty('limit', $limit);
  129. if (TSession::getValue('product_filter'))
  130. {
  131. // add the filter stored in the session to the criteria
  132. $criteria->add(TSession::getValue('product_filter'));
  133. }
  134. // TTransaction::setLogger(new TLoggerSTD); // standard output
  135. // TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  136. // load the objects according to the criteria
  137. $products = $repository->load($criteria);
  138. $this->datagrid->clear();
  139. if ($products)
  140. {
  141. foreach ($products as $product)
  142. {
  143. // add the object inside the datagrid
  144. $this->datagrid->addItem($product);
  145. }
  146. }
  147. // reset the criteria for record count
  148. $criteria->resetProperties();
  149. $count= $repository->count($criteria);
  150. $this->pageNavigation->setCount($count); // count of records
  151. $this->pageNavigation->setProperties($param); // order, page
  152. $this->pageNavigation->setLimit($limit); // limit
  153. // close the transaction
  154. TTransaction::close();
  155. $this->loaded = true;
  156. }
  157. catch (Exception $e) // in case of exception
  158. {
  159. // shows the exception error message
  160. new TMessage('error', $e->getMessage());
  161. // undo all pending operations
  162. TTransaction::rollback();
  163. }
  164. }
  165. /**
  166. * Executed when the user chooses the record
  167. */
  168. public function onSelect($param)
  169. {
  170. try
  171. {
  172. $key = $param['key'];
  173. TTransaction::open('catalogo');
  174. // load the active record
  175. // creates a repository for Product
  176. $repository = new TRepository('Product');
  177. $limit = 10;
  178. // creates a criteria
  179. $criteria = new TCriteria;
  180. $criteria->setProperties($param); // order, offset
  181. $criteria->setProperty('limit', $limit);
  182. $filter = new TFilter('codigo', '=', $key);
  183. TSession::setValue('product_filter', $filter);
  184. // add the filter stored in the session to the criteria
  185. $criteria->add(TSession::getValue('product_filter'));
  186. // TTransaction::setLogger(new TLoggerSTD); // standard output
  187. // TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  188. // load the objects according to the criteria
  189. $products = $repository->load($criteria);
  190. //$product = new Product($key);
  191. foreach ($products as $product)
  192. {
  193. $product = new Product($product->id);
  194. }
  195. // closes the transaction
  196. TTransaction::close();
  197. $object = new StdClass;
  198. $object->product_id = $product->id;
  199. $object->product_name = $product->description;
  200. $object->product_codigo = $product->codigo;
  201. $object->product_price = $product->sale_price;
  202. TForm::sendData('form_Sale', $object);
  203. parent::closeWindow(); // closes the window
  204. }
  205. catch (Exception $e) // em caso de exceção
  206. {
  207. // clear fields
  208. $object = new StdClass;
  209. $object->product_id = '';
  210. $object->product_name = '';
  211. $object->product_codigo = '';
  212. $object->product_price ='';
  213. TForm::sendData('form_Sale', $object);
  214. // undo pending operations
  215. TTransaction::rollback();
  216. }
  217. }
  218. /**
  219. * Shows the page
  220. */
  221. function show()
  222. {
  223. // if the datagrid was not loaded yet
  224. if (!$this->loaded)
  225. {
  226. $this->onReload();
  227. }
  228. parent::show();
  229. }
  230. }
  231. ?>




//bl3

 
  1. <?php
  2. /**
  3. * Product Seek
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage tutor
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class ProductSeek extends TWindow
  13. {
  14. private $form; // form
  15. private $datagrid; // datagrid
  16. private $pageNavigation;
  17. private $loaded;
  18. /**
  19. * Class constructor
  20. * Creates the page, the search form and the listing
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. parent::setSize(700, 500);
  26. parent::setTitle('Search record');
  27. new TSession;
  28. // creates the form
  29. $this->form = new TQuickForm('form_search_product');
  30. $this->form->class = 'tform';
  31. $this->form->setFormTitle('Produtos');
  32. // create the form fields
  33. $codigo = new TEntry('codigo');
  34. $codigo->setValue(TSession::getValue('product_codigo'));
  35. $description = new TEntry('description');
  36. $description->setValue(TSession::getValue('product_name'));
  37. // add the form fields
  38. $this->form->addQuickField('Código', $codigo, 60);
  39. $this->form->addQuickField('Descrição', $description, 200);
  40. // define the form action
  41. $this->form->addQuickAction('Find', new TAction(array($this, 'onSearch')), 'ico_find.png');
  42. // creates a DataGrid
  43. $this->datagrid = new TQuickGrid;
  44. $this->datagrid->style = 'width: 100%';
  45. $this->datagrid->setHeight(230);
  46. $this->datagrid->enablePopover('Title', 'Descrição {description}');
  47. // creates the datagrid columns
  48. $this->datagrid->addQuickColumn('Id', 'id', 'right', 40);
  49. $this->datagrid->addQuickColumn('Código', 'codigo', 'right', 40);
  50. $this->datagrid->addQuickColumn('Descrição', 'description', 'left', 340);
  51. // creates two datagrid actions
  52. $this->datagrid->addQuickAction('Select', new TDataGridAction(array($this, 'onSelect')), 'id', 'ico_apply.png');
  53. // create the datagrid model
  54. $this->datagrid->createModel();
  55. // creates the page navigation
  56. $this->pageNavigation = new TPageNavigation;
  57. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  58. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  59. // creates a container
  60. $container = new TVBox;
  61. $container->style = 'width: 100%';
  62. $container->add($this->form);
  63. $container->add($this->datagrid);
  64. $container->add($this->pageNavigation);
  65. // add the container inside the page
  66. parent::add($container);
  67. }
  68. /**
  69. * method onSearch()
  70. * Register the filter in the session when the user performs a search
  71. */
  72. function onSearch()
  73. {
  74. // get the search form data
  75. $data = $this->form->getData();
  76. // TSession::setValue('product_name', NULL);
  77. // TSession::setValue('product_codigo', NULL);
  78. TSession::setValue('product_filter', NULL);
  79. // check if the user has filled the form
  80. if (isset($data->description) and ($data->description))
  81. {
  82. // creates a filter using what the user has typed
  83. $filter = new TFilter('description', 'like', "%{$data->description}%");
  84. // stores the filter in the session
  85. TSession::setValue('product_filter', $filter);
  86. TSession::setValue('product_name', $data->description);
  87. // fill the form with data again
  88. $this->form->setData($data);
  89. }
  90. // if (isset($data->codigo) and $data->codigo)
  91. if ($data->codigo)
  92. {
  93. // creates a filter using what the user has typed
  94. $filter = new TFilter('codigo', '=', "{$data->codigo}");
  95. // stores the filter in the session
  96. TSession::setValue('product_filter', $filter);
  97. TSession::setValue('product_codigo', $data->codigo);
  98. // fill the form with data again
  99. $this->form->setData($data);
  100. }
  101. // redefine the parameters for reload method
  102. $param=array();
  103. $param['offset'] =0;
  104. $param['first_page']=1;
  105. $this->onReload($param);
  106. }
  107. /**
  108. * Load the datagrid with the database objects
  109. */
  110. function onReload($param = NULL)
  111. {
  112. try
  113. {
  114. // open a transaction with database 'samples'
  115. TTransaction::open('catalogo');
  116. // creates a repository for Product
  117. $repository = new TRepository('Product');
  118. $limit = 10;
  119. // creates a criteria
  120. $criteria = new TCriteria;
  121. // default order
  122. if (!isset($param['order']))
  123. {
  124. $param['order'] = 'id';
  125. $param['direction'] = 'asc';
  126. }
  127. $criteria->setProperties($param); // order, offset
  128. $criteria->setProperty('limit', $limit);
  129. if (TSession::getValue('product_filter'))
  130. {
  131. // add the filter stored in the session to the criteria
  132. $criteria->add(TSession::getValue('product_filter'));
  133. }
  134. TTransaction::setLogger(new TLoggerSTD); // standard output
  135. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  136. // load the objects according to the criteria
  137. $products = $repository->load($criteria);
  138. $this->datagrid->clear();
  139. if ($products)
  140. {
  141. foreach ($products as $product)
  142. {
  143. // add the object inside the datagrid
  144. $this->datagrid->addItem($product);
  145. }
  146. }
  147. // reset the criteria for record count
  148. $criteria->resetProperties();
  149. $count= $repository->count($criteria);
  150. $this->pageNavigation->setCount($count); // count of records
  151. $this->pageNavigation->setProperties($param); // order, page
  152. $this->pageNavigation->setLimit($limit); // limit
  153. // close the transaction
  154. TTransaction::close();
  155. $this->loaded = true;
  156. }
  157. catch (Exception $e) // in case of exception
  158. {
  159. // shows the exception error message
  160. new TMessage('error', $e->getMessage());
  161. // undo all pending operations
  162. TTransaction::rollback();
  163. }
  164. }
  165. /**
  166. * Executed when the user chooses the record
  167. */
  168. public function onSelect($param)
  169. {
  170. try
  171. {
  172. $key = $param['key'];
  173. TTransaction::open('catalogo');
  174. // load the active record
  175. $product = new Product($key);
  176. // closes the transaction
  177. TTransaction::close();
  178. $object = new StdClass;
  179. $object->product_id = $product->id;
  180. $object->product_name = $product->description;
  181. $object->product_codigo = $product->codigo;
  182. $object->product_price = $product->sale_price;
  183. TForm::sendData('form_Sale', $object);
  184. parent::closeWindow(); // closes the window
  185. }
  186. catch (Exception $e) // em caso de exceção
  187. {
  188. // clear fields
  189. $object = new StdClass;
  190. $object->product_id = '';
  191. $object->product_name = '';
  192. $object->product_codigo = '';
  193. $object->product_price ='';
  194. TForm::sendData('form_Sale', $object);
  195. // undo pending operations
  196. TTransaction::rollback();
  197. }
  198. }
  199. /**
  200. * Shows the page
  201. */
  202. function show()
  203. {
  204. // if the datagrid was not loaded yet
  205. if (!$this->loaded)
  206. {
  207. $this->onReload();
  208. }
  209. parent::show();
  210. }
  211. }
  212. ?>