Error Object 2 not found in author Olá, tudo bem? Estou iniciando minhas pesquisas no Adianti Framework a pouco tempo, estou estudando o módulo Biblioteca (library) disponibilizado pelo Pablo, estou mudando o seu template para o tema 3 e estou com o seguinte Error Object 2 not found in author. Se alguma alma caridosa puder ajudar, serei grato! Mais notei que as pessoas que participam do fórum são bastante colaborativas! ...
JA
Error Object 2 not found in author  
Olá, tudo bem?

Estou iniciando minhas pesquisas no Adianti Framework a pouco tempo, estou estudando o módulo Biblioteca (library) disponibilizado pelo Pablo, estou mudando o seu template para o tema 3 e estou com o seguinte Error Object 2 not found in author.

Se alguma alma caridosa puder ajudar, serei grato! Mais notei que as pessoas que participam do fórum são bastante colaborativas!

Att,
Jonathas Alves

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


LA

ola Jonathas, resolveu?
posta como fez no BookList e no Model da tabela.
JA

Olá, Luis Alberto!

Tudo bem?
Ainda não consegui resolver

Control

 
  1. <?php
  2. /**
  3. * BookList Listing
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage library
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class BookList extends TPage
  13. {
  14. private $form; // registration form
  15. private $datagrid; // listing
  16. private $pageNavigation;
  17. private $loaded;
  18. /**
  19. * Class constructor
  20. * Creates the page, the form and the listing
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. // security check
  26. if (TSession::getValue('logged') !== TRUE)
  27. {
  28. throw new Exception(('Not logged'));
  29. }
  30. // creates the form
  31. $this->form = new TForm('form_search_Book');
  32. $this->form->class = 'tform';
  33. // creates a table
  34. $table = new TTable;
  35. $table->width = '100%';
  36. $table->addRowSet(new TLabel(('Books')), '')->class='tformtitle';
  37. // add the table inside the form
  38. $this->form->add($table);
  39. // create the form fields
  40. $title = new TEntry('title');
  41. $author_id = new TSeekButton('author_id');
  42. $author_name = new TEntry('author_name');
  43. $collection_id = new TDBCombo('collection_id', 'library', 'Collection', 'id', 'description');
  44. $classification_id = new TDBCombo('classification_id', 'library', 'Classification', 'id', 'description');
  45. $title->setValue(TSession::getValue('Book_title'));
  46. $author_id->setValue(TSession::getValue('Book_author_id'));
  47. $author_name->setValue(TSession::getValue('Book_author_name'));
  48. $collection_id->setValue(TSession::getValue('Book_collection_id'));
  49. $classification_id->setValue(TSession::getValue('Book_classification_id'));
  50. $author_name->setEditable(FALSE);
  51. $title->setSize(320);
  52. $author_id->setSize(100);
  53. $obj = new TStandardSeek;
  54. $action = new TAction(array($obj, 'onSetup'));
  55. $action->setParameter('database', 'library');
  56. $action->setParameter('parent', 'form_search_Book');
  57. $action->setParameter('model', 'Author');
  58. $action->setParameter('display_field', 'name');
  59. $action->setParameter('receive_key', 'author_id');
  60. $action->setParameter('receive_field', 'author_name');
  61. $author_id->setAction($action);
  62. // add a row for the title field
  63. $row=$table->addRow();
  64. $row->addCell(new TLabel(('Title') . ': '));
  65. $cell=$row->addCell($title);
  66. // add a row for the title field
  67. $row=$table->addRow();
  68. $row->addCell(new TLabel(('Author') . ': '));
  69. $row->addMultiCell($author_id, $author_name);
  70. // add a row for the title field
  71. $row=$table->addRow();
  72. $row->addCell(new TLabel(('Collection') . ': '));
  73. $cell=$row->addCell($collection_id);
  74. // add a row for the title field
  75. $row=$table->addRow();
  76. $row->addCell(new TLabel(('Classification') . ': '));
  77. $cell=$row->addCell($classification_id);
  78. // create two action buttons to the form
  79. $find_button = new TButton('find');
  80. $new_button = new TButton('new');
  81. // define the button actions
  82. $find_button->setAction(new TAction(array($this, 'onSearch')), ('Find'));
  83. $find_button->setImage('fa:search');
  84. $new_button->setAction(new TAction(array('BookForm', 'onEdit')), ('New'));
  85. $new_button->setImage('fa:plus-square green');
  86. $table->addRowSet('', array($find_button, $new_button))->class='tformaction';
  87. // define wich are the form fields
  88. $this->form->setFields(array($title, $author_id, $author_name, $collection_id, $classification_id, $find_button, $new_button));
  89. // creates a DataGrid
  90. $this->datagrid = new TDataGrid;
  91. $this->datagrid->style = 'width: 100%';
  92. $this->datagrid->setHeight(280);
  93. // creates the datagrid columns
  94. $id = new TDataGridColumn('id', ('Code'), 'right', 50);
  95. $title = new TDataGridColumn('title', ('Title'), 'left', 200);
  96. $main_author = new TDataGridColumn('author_name', ('Author'), 'left', 160);
  97. $edition = new TDataGridColumn('edition', ('Edition'), 'left', 50);
  98. $call = new TDataGridColumn('call_number', ('Call'), 'left', 80);
  99. // creates the datagrid actions
  100. $order1= new TAction(array($this, 'onReload'));
  101. $order2= new TAction(array($this, 'onReload'));
  102. // define the ordering parameters
  103. $order1->setParameter('order', 'id');
  104. $order2->setParameter('order', 'title');
  105. // assign the ordering actions
  106. $id->setAction($order1);
  107. $title->setAction($order2);
  108. // add the columns to the DataGrid
  109. $this->datagrid->addColumn($id);
  110. $this->datagrid->addColumn($title);
  111. $this->datagrid->addColumn($main_author);
  112. $this->datagrid->addColumn($edition);
  113. $this->datagrid->addColumn($call);
  114. // creates two datagrid actions
  115. $action1 = new TDataGridAction(array('BookForm', 'onEdit'));
  116. $action1->setLabel(('Edit'));
  117. $action1->setImage('fa:pencil-square-o blue fa-lg');
  118. $action1->setField('id');
  119. $action2 = new TDataGridAction(array($this, 'onDelete'));
  120. $action2->setLabel(('Delete'));
  121. $action2->setImage('fa:trash-o red fa-lg');
  122. $action2->setField('id');
  123. // add the actions to the datagrid
  124. $this->datagrid->addAction($action1);
  125. $this->datagrid->addAction($action2);
  126. // create the datagrid model
  127. $this->datagrid->createModel();
  128. // creates the page navigation
  129. $this->pageNavigation = new TPageNavigation;
  130. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  131. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  132. // creates the page structure using a vbox
  133. $container = new TVBox;
  134. $container->style = 'width: 100%';
  135. $container->add($this->form);
  136. $container->add($this->datagrid);
  137. $container->add($this->pageNavigation);
  138. // add the vbox inside the page
  139. parent::add($container);
  140. }
  141. /**
  142. * method onSearch()
  143. * Register the filter in the session when the user performs a search
  144. */
  145. function onSearch()
  146. {
  147. // get the search form data
  148. $data = $this->form->getData();
  149. $filters = array();
  150. TSession::setValue('Book_title', '');
  151. TSession::setValue('Book_author_id', '');
  152. TSession::setValue('Book_author_name', '');
  153. TSession::setValue('Book_collection_id', '');
  154. TSession::setValue('Book_classification_id', '');
  155. TSession::setValue('Book_filters', array());
  156. // check if the user has filled the form
  157. if ($data->title)
  158. {
  159. // creates a filter using what the user has typed
  160. $filter = new TFilter('title', 'like', "%{$data->title}%");
  161. // stores the filter in the session
  162. TSession::setValue('Book_title', $data->title);
  163. $filters[] = $filter;
  164. }
  165. if ($data->author_id)
  166. {
  167. // creates a filter using what the user has typed
  168. $filter = new TFilter('author_id', '=', "{$data->author_id}");
  169. // stores the filter in the session
  170. TSession::setValue('Book_author_id', $data->author_id);
  171. TSession::setValue('Book_author_name', $data->author_name);
  172. $filters[] = $filter;
  173. }
  174. if ($data->collection_id)
  175. {
  176. // creates a filter using what the user has typed
  177. $filter = new TFilter('collection_id', '=', "{$data->collection_id}");
  178. // stores the filter in the session
  179. TSession::setValue('Book_collection_id', $data->collection_id);
  180. $filters[] = $filter;
  181. }
  182. if ($data->classification_id)
  183. {
  184. // creates a filter using what the user has typed
  185. $filter = new TFilter('classification_id', '=', "{$data->classification_id}");
  186. // stores the filter in the session
  187. TSession::setValue('Book_classification_id', $data->classification_id);
  188. $filters[] = $filter;
  189. }
  190. if ($filters)
  191. {
  192. TSession::setValue('Book_filters', $filters);
  193. }
  194. // fill the form with data again
  195. $this->form->setData($data);
  196. $param=array();
  197. $param['offset'] =0;
  198. $param['first_page']=1;
  199. $this->onReload($param);
  200. }
  201. /**
  202. * method onReload()
  203. * Load the datagrid with the database objects
  204. */
  205. function onReload($param = NULL)
  206. {
  207. try
  208. {
  209. // open a transaction with database 'library'
  210. TTransaction::open('library');
  211. // creates a repository for Book
  212. $repository = new TRepository('Book');
  213. $limit = 10;
  214. // creates a criteria
  215. $criteria = new TCriteria;
  216. $criteria->setProperties($param); // order, offset
  217. $criteria->setProperty('limit', $limit);
  218. if (TSession::getValue('Book_filters'))
  219. {
  220. foreach (TSession::getValue('Book_filters') as $filter)
  221. {
  222. if ($filter instanceof TFilter)
  223. {
  224. // add the filter stored in the session to the criteria
  225. $criteria->add($filter);
  226. }
  227. }
  228. }
  229. // load the objects according to criteria
  230. $objects = $repository->load($criteria);
  231. $this->datagrid->clear();
  232. if ($objects)
  233. {
  234. // iterate the collection of active records
  235. foreach ($objects as $object)
  236. {
  237. // add the object inside the datagrid
  238. $this->datagrid->addItem($object);
  239. }
  240. }
  241. // reset the criteria for record count
  242. $criteria->resetProperties();
  243. $count= $repository->count($criteria);
  244. $this->pageNavigation->setCount($count); // count of records
  245. $this->pageNavigation->setProperties($param); // order, page
  246. $this->pageNavigation->setLimit($limit); // limit
  247. // close the transaction
  248. TTransaction::close();
  249. $this->loaded = true;
  250. }
  251. catch (Exception $e) // in case of exception
  252. {
  253. // shows the exception error message
  254. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  255. // undo all pending operations
  256. TTransaction::rollback();
  257. }
  258. }
  259. /**
  260. * method onDelete()
  261. * executed whenever the user clicks at the delete button
  262. * Ask if the user really wants to delete the record
  263. */
  264. function onDelete($param)
  265. {
  266. // get the parameter $key
  267. $key=$param['key'];
  268. // define two actions
  269. $action = new TAction(array($this, 'Delete'));
  270. // define the action parameters
  271. $action->setParameter('key', $key);
  272. // shows a dialog to the user
  273. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  274. }
  275. /**
  276. * method Delete()
  277. * Delete a record
  278. */
  279. function Delete($param)
  280. {
  281. try
  282. {
  283. // get the parameter $key
  284. $key=$param['key'];
  285. // open a transaction with database 'library'
  286. TTransaction::open('library');
  287. // instantiates object Book
  288. $object = new Book($key);
  289. // deletes the object from the database
  290. $object->delete();
  291. // close the transaction
  292. TTransaction::close();
  293. // reload the listing
  294. $this->onReload();
  295. // shows the success message
  296. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
  297. }
  298. catch (Exception $e) // in case of exception
  299. {
  300. // shows the exception error message
  301. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  302. // undo all pending operations
  303. TTransaction::rollback();
  304. }
  305. }
  306. /**
  307. * method show()
  308. * Shows the page
  309. */
  310. function show()
  311. {
  312. // check if the datagrid is already loaded
  313. if (!$this->loaded)
  314. {
  315. $this->onReload();
  316. }
  317. parent::show();
  318. }
  319. }
  320. ?>



Model

 
  1. <?php
  2. /**
  3. * Book Active Record
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage library
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class Book extends TRecord
  13. {
  14. const TABLENAME = 'book';
  15. const PRIMARYKEY= 'id';
  16. const IDPOLICY = 'max'; // {max, serial}
  17. use SystemChangeLogTrait;
  18. private $authors;
  19. private $subjects;
  20. private $items;
  21. private $author;
  22. private $publisher;
  23. private $collection;
  24. /**
  25. * Returns the book author name
  26. */
  27. function get_author_name()
  28. {
  29. if (empty($this->author))
  30. $this->author = new Author($this->author_id);
  31. return $this->author->name;
  32. }
  33. /**
  34. * Returns the book collection description
  35. */
  36. function get_collection_description()
  37. {
  38. if (empty($this->collection))
  39. $this->collection = new Collection($this->collection_id);
  40. return $this->collection->description;
  41. }
  42. /**
  43. * Returns the book publisher name
  44. */
  45. function get_publisher_name()
  46. {
  47. if (empty($this->publisher))
  48. $this->publisher = new Publisher($this->publisher_id);
  49. return $this->publisher->name;
  50. }
  51. /**
  52. * Reset aggregates
  53. */
  54. public function clearParts()
  55. {
  56. $this->authors = array();
  57. $this->subjects = array();
  58. $this->items = array();
  59. }
  60. /**
  61. * Aggregation with Author
  62. */
  63. public function addAuthor(Author $author)
  64. {
  65. $this->authors[] = $author;
  66. }
  67. /**
  68. * Aggregation with Subject
  69. */
  70. public function addSubject(Subject $subject)
  71. {
  72. $this->subjects[] = $subject;
  73. }
  74. /**
  75. * Composition with Item
  76. */
  77. public function addItem(Item $item)
  78. {
  79. $this->items[] = $item;
  80. }
  81. /**
  82. * Return Author aggregates
  83. */
  84. public function getAuthors()
  85. {
  86. return $this->authors;
  87. }
  88. /**
  89. * Return Subject aggregates
  90. */
  91. public function getSubjects()
  92. {
  93. return $this->subjects;
  94. }
  95. /**
  96. * Return Items composition
  97. */
  98. public function getItems()
  99. {
  100. return $this->items;
  101. }
  102. /**
  103. * Load the object and the aggregates
  104. */
  105. public function load($id)
  106. {
  107. $book_author_rep = new TRepository('BookAuthor');
  108. $book_subject_rep = new TRepository('BookSubject');
  109. $item_rep = new TRepository('Item');
  110. $criteria = new TCriteria;
  111. $criteria->add(new TFilter('book_id', '=', $id));
  112. // load the Author aggregates
  113. $book_authors = $book_author_rep->load($criteria);
  114. if ($book_authors)
  115. {
  116. foreach ($book_authors as $book_author)
  117. {
  118. $author = new Author($book_author-> author_id);
  119. $this->addAuthor($author);
  120. }
  121. }
  122. // load the Subject aggregates
  123. $book_subjects = $book_subject_rep->load($criteria);
  124. if ($book_subjects)
  125. {
  126. foreach ($book_subjects as $book_subject)
  127. {
  128. $subject = new Subject($book_subject-> subject_id);
  129. $this->addSubject($subject);
  130. }
  131. }
  132. // load the Item composition
  133. $items = $item_rep->load($criteria);
  134. if ($items)
  135. {
  136. foreach ($items as $item)
  137. {
  138. $this->addItem($item);
  139. }
  140. }
  141. // load the object itself
  142. return parent::load($id);
  143. }
  144. /**
  145. * Stores the book and the aggregates (authors, subjects, items)
  146. */
  147. public function store()
  148. {
  149. // stores the Book
  150. parent::store();
  151. // delete the aggregates
  152. $criteria = new TCriteria;
  153. $criteria->add(new TFilter('book_id', '=', $this->id));
  154. $repository = new TRepository('BookAuthor');
  155. $repository->delete($criteria);
  156. $repository = new TRepository('BookSubject');
  157. $repository->delete($criteria);
  158. // collect persistent item ids
  159. if ($this->items)
  160. {
  161. foreach ($this->items as $item)
  162. {
  163. if ($item->id)
  164. {
  165. $item_ids[] = $item->id;
  166. }
  167. }
  168. }
  169. // delete all items, except for those that persist
  170. $criteria->add(new TFilter('id', 'NOT IN', $item_ids));
  171. $repository = new TRepository('Item');
  172. $repository->delete($criteria);
  173. // store the authors
  174. if ($this->authors)
  175. {
  176. foreach ($this->authors as $author)
  177. {
  178. $book_author = new BookAuthor;
  179. $book_author-> book_id = $this-> id;
  180. $book_author-> author_id = $author-> id;
  181. $book_author->store();
  182. }
  183. }
  184. // store the subjects
  185. if ($this->subjects)
  186. {
  187. foreach ($this->subjects as $subject)
  188. {
  189. $book_subject = new BookSubject;
  190. $book_subject-> book_id = $this-> id;
  191. $book_subject-> subject_id = $subject-> id;
  192. $book_subject->store();
  193. }
  194. }
  195. // store the items
  196. if ($this->items)
  197. {
  198. foreach ($this->items as $item)
  199. {
  200. $item-> book_id = $this-> id;
  201. $item->store();
  202. }
  203. }
  204. }
  205. /**
  206. * Delete the book and its aggregates
  207. */
  208. public function delete($id = NULL)
  209. {
  210. $id = isset($id) ? $id : $this->{'id'};
  211. $criteria = new TCriteria;
  212. $criteria->add(new TFilter('book_id', '=', $id));
  213. $repository = new TRepository('BookAuthor');
  214. $repository->delete($criteria);
  215. $repository = new TRepository('BookSubject');
  216. $repository->delete($criteria);
  217. $repository = new TRepository('Item');
  218. $repository->delete($criteria);
  219. // delete the object itself
  220. parent::delete($id);
  221. }
  222. }
  223. ?>