Object.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Everything Solved.
  16. # Portions created by Everything Solved are Copyright (C) 2006
  17. # Everything Solved. All Rights Reserved.
  18. #
  19. # Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
  20. # Frédéric Buclin <LpSolit@gmail.com>
  21. use strict;
  22. package Bugzilla::Object;
  23. use Bugzilla::Constants;
  24. use Bugzilla::Util;
  25. use Bugzilla::Error;
  26. use Date::Parse;
  27. use constant NAME_FIELD => 'name';
  28. use constant ID_FIELD => 'id';
  29. use constant LIST_ORDER => NAME_FIELD;
  30. use constant UPDATE_VALIDATORS => {};
  31. use constant NUMERIC_COLUMNS => ();
  32. use constant DATE_COLUMNS => ();
  33. ###############################
  34. #### Initialization ####
  35. ###############################
  36. sub new {
  37. my $invocant = shift;
  38. my $class = ref($invocant) || $invocant;
  39. my $object = $class->_init(@_);
  40. bless($object, $class) if $object;
  41. return $object;
  42. }
  43. # Note: Because this uses sql_istrcmp, if you make a new object use
  44. # Bugzilla::Object, make sure that you modify bz_setup_database
  45. # in Bugzilla::DB::Pg appropriately, to add the right LOWER
  46. # index. You can see examples already there.
  47. sub _init {
  48. my $class = shift;
  49. my ($param) = @_;
  50. my $dbh = Bugzilla->dbh;
  51. my $columns = join(',', $class->DB_COLUMNS);
  52. my $table = $class->DB_TABLE;
  53. my $name_field = $class->NAME_FIELD;
  54. my $id_field = $class->ID_FIELD;
  55. my $id = $param unless (ref $param eq 'HASH');
  56. my $object;
  57. if (defined $id) {
  58. # We special-case if somebody specifies an ID, so that we can
  59. # validate it as numeric.
  60. detaint_natural($id)
  61. || ThrowCodeError('param_must_be_numeric',
  62. {function => $class . '::_init'});
  63. $object = $dbh->selectrow_hashref(qq{
  64. SELECT $columns FROM $table
  65. WHERE $id_field = ?}, undef, $id);
  66. } else {
  67. unless (defined $param->{name} || (defined $param->{'condition'}
  68. && defined $param->{'values'}))
  69. {
  70. ThrowCodeError('bad_arg', { argument => 'param',
  71. function => $class . '::new' });
  72. }
  73. my ($condition, @values);
  74. if (defined $param->{name}) {
  75. $condition = $dbh->sql_istrcmp($name_field, '?');
  76. push(@values, $param->{name});
  77. }
  78. elsif (defined $param->{'condition'} && defined $param->{'values'}) {
  79. caller->isa('Bugzilla::Object')
  80. || ThrowCodeError('protection_violation',
  81. { caller => caller,
  82. function => $class . '::new',
  83. argument => 'condition/values' });
  84. $condition = $param->{'condition'};
  85. push(@values, @{$param->{'values'}});
  86. }
  87. map { trick_taint($_) } @values;
  88. $object = $dbh->selectrow_hashref(
  89. "SELECT $columns FROM $table WHERE $condition", undef, @values);
  90. }
  91. return $object;
  92. }
  93. sub check {
  94. my ($invocant, $param) = @_;
  95. my $class = ref($invocant) || $invocant;
  96. # If we were just passed a name, then just use the name.
  97. if (!ref $param) {
  98. $param = { name => $param };
  99. }
  100. # Don't allow empty names.
  101. if (exists $param->{name}) {
  102. $param->{name} = trim($param->{name});
  103. $param->{name} || ThrowUserError('object_name_not_specified',
  104. { class => $class });
  105. }
  106. my $obj = $class->new($param)
  107. || ThrowUserError('object_does_not_exist', {%$param, class => $class});
  108. return $obj;
  109. }
  110. sub new_from_list {
  111. my $invocant = shift;
  112. my $class = ref($invocant) || $invocant;
  113. my ($id_list) = @_;
  114. my $id_field = $class->ID_FIELD;
  115. my @detainted_ids;
  116. foreach my $id (@$id_list) {
  117. detaint_natural($id) ||
  118. ThrowCodeError('param_must_be_numeric',
  119. {function => $class . '::new_from_list'});
  120. push(@detainted_ids, $id);
  121. }
  122. # We don't do $invocant->match because some classes have
  123. # their own implementation of match which is not compatible
  124. # with this one. However, match() still needs to have the right $invocant
  125. # in order to do $class->DB_TABLE and so on.
  126. return match($invocant, { $id_field => \@detainted_ids });
  127. }
  128. # Note: Future extensions to this could be:
  129. # * Add a MATCH_JOIN constant so that we can join against
  130. # certain other tables for the WHERE criteria.
  131. sub match {
  132. my ($invocant, $criteria) = @_;
  133. my $class = ref($invocant) || $invocant;
  134. my $dbh = Bugzilla->dbh;
  135. return [$class->get_all] if !$criteria;
  136. my (@terms, @values);
  137. foreach my $field (keys %$criteria) {
  138. $class->_check_field($field, 'match');
  139. my $value = $criteria->{$field};
  140. if (ref $value eq 'ARRAY') {
  141. # IN () is invalid SQL, and if we have an empty list
  142. # to match against, we're just returning an empty
  143. # array anyhow.
  144. return [] if !scalar @$value;
  145. my @qmarks = ("?") x @$value;
  146. push(@terms, $dbh->sql_in($field, \@qmarks));
  147. push(@values, @$value);
  148. }
  149. elsif ($value eq NOT_NULL) {
  150. push(@terms, "$field IS NOT NULL");
  151. }
  152. elsif ($value eq IS_NULL) {
  153. push(@terms, "$field IS NULL");
  154. }
  155. else {
  156. push(@terms, "$field = ?");
  157. push(@values, $value);
  158. }
  159. }
  160. my $where = join(' AND ', @terms);
  161. return $class->_do_list_select($where, \@values);
  162. }
  163. sub _do_list_select {
  164. my ($class, $where, $values) = @_;
  165. my $table = $class->DB_TABLE;
  166. my $cols = join(',', $class->DB_COLUMNS);
  167. my $order = $class->LIST_ORDER;
  168. my $sql = "SELECT $cols FROM $table";
  169. if (defined $where) {
  170. $sql .= " WHERE $where ";
  171. }
  172. $sql .= " ORDER BY $order";
  173. my $dbh = Bugzilla->dbh;
  174. my $objects = $dbh->selectall_arrayref($sql, {Slice=>{}}, @$values);
  175. bless ($_, $class) foreach @$objects;
  176. return $objects
  177. }
  178. ###############################
  179. #### Accessors ######
  180. ###############################
  181. sub id { return $_[0]->{$_[0]->ID_FIELD}; }
  182. sub name { return $_[0]->{$_[0]->NAME_FIELD}; }
  183. ###############################
  184. #### Methods ####
  185. ###############################
  186. sub set {
  187. my ($self, $field, $value) = @_;
  188. # This method is protected. It's used to help implement set_ functions.
  189. caller->isa('Bugzilla::Object')
  190. || ThrowCodeError('protection_violation',
  191. { caller => caller,
  192. superclass => __PACKAGE__,
  193. function => 'Bugzilla::Object->set' });
  194. my %validators = (%{$self->VALIDATORS}, %{$self->UPDATE_VALIDATORS});
  195. if (exists $validators{$field}) {
  196. my $validator = $validators{$field};
  197. $value = $self->$validator($value, $field);
  198. trick_taint($value) if (defined $value && !ref($value));
  199. if ($self->can('_set_global_validator')) {
  200. $self->_set_global_validator($value, $field);
  201. }
  202. }
  203. $self->{$field} = $value;
  204. }
  205. sub update {
  206. my $self = shift;
  207. my $dbh = Bugzilla->dbh;
  208. my $table = $self->DB_TABLE;
  209. my $id_field = $self->ID_FIELD;
  210. $dbh->bz_start_transaction();
  211. my $old_self = $self->new($self->id);
  212. my %numeric = map { $_ => 1 } $self->NUMERIC_COLUMNS;
  213. my %date = map { $_ => 1 } $self->DATE_COLUMNS;
  214. my (@update_columns, @values, %changes);
  215. foreach my $column ($self->UPDATE_COLUMNS) {
  216. my ($old, $new) = ($old_self->{$column}, $self->{$column});
  217. # This has to be written this way in order to allow us to set a field
  218. # from undef or to undef, and avoid warnings about comparing an undef
  219. # with the "eq" operator.
  220. if (!defined $new || !defined $old) {
  221. next if !defined $new && !defined $old;
  222. }
  223. elsif ( ($numeric{$column} && $old == $new)
  224. || ($date{$column} && str2time($old) == str2time($new))
  225. || $old eq $new ) {
  226. next;
  227. }
  228. trick_taint($new) if defined $new;
  229. push(@values, $new);
  230. push(@update_columns, $column);
  231. # We don't use $new because we don't want to detaint this for
  232. # the caller.
  233. $changes{$column} = [$old, $self->{$column}];
  234. }
  235. my $columns = join(', ', map {"$_ = ?"} @update_columns);
  236. $dbh->do("UPDATE $table SET $columns WHERE $id_field = ?", undef,
  237. @values, $self->id) if @values;
  238. $dbh->bz_commit_transaction();
  239. return \%changes;
  240. }
  241. ###############################
  242. #### Subroutines ######
  243. ###############################
  244. sub create {
  245. my ($class, $params) = @_;
  246. my $dbh = Bugzilla->dbh;
  247. $dbh->bz_start_transaction();
  248. $class->check_required_create_fields($params);
  249. my $field_values = $class->run_create_validators($params);
  250. my $object = $class->insert_create_data($field_values);
  251. $dbh->bz_commit_transaction();
  252. return $object;
  253. }
  254. # Used to validate that a field name is in fact a valid column in the
  255. # current table before inserting it into SQL.
  256. sub _check_field {
  257. my ($invocant, $field, $function) = @_;
  258. my $class = ref($invocant) || $invocant;
  259. if (!Bugzilla->dbh->bz_column_info($class->DB_TABLE, $field)) {
  260. ThrowCodeError('param_invalid', { param => $field,
  261. function => "${class}::$function" });
  262. }
  263. }
  264. sub check_required_create_fields {
  265. my ($class, $params) = @_;
  266. foreach my $field ($class->REQUIRED_CREATE_FIELDS) {
  267. ThrowCodeError('param_required',
  268. { function => "${class}->create", param => $field })
  269. if !exists $params->{$field};
  270. }
  271. }
  272. sub run_create_validators {
  273. my ($class, $params) = @_;
  274. my $validators = $class->VALIDATORS;
  275. my %field_values;
  276. # We do the sort just to make sure that validation always
  277. # happens in a consistent order.
  278. foreach my $field (sort keys %$params) {
  279. my $value;
  280. if (exists $validators->{$field}) {
  281. my $validator = $validators->{$field};
  282. $value = $class->$validator($params->{$field}, $field);
  283. }
  284. else {
  285. $value = $params->{$field};
  286. }
  287. # We want people to be able to explicitly set fields to NULL,
  288. # and that means they can be set to undef.
  289. trick_taint($value) if defined $value && !ref($value);
  290. $field_values{$field} = $value;
  291. }
  292. return \%field_values;
  293. }
  294. sub insert_create_data {
  295. my ($class, $field_values) = @_;
  296. my $dbh = Bugzilla->dbh;
  297. my (@field_names, @values);
  298. while (my ($field, $value) = each %$field_values) {
  299. $class->_check_field($field, 'create');
  300. push(@field_names, $field);
  301. push(@values, $value);
  302. }
  303. my $qmarks = '?,' x @field_names;
  304. chop($qmarks);
  305. my $table = $class->DB_TABLE;
  306. $dbh->do("INSERT INTO $table (" . join(', ', @field_names)
  307. . ") VALUES ($qmarks)", undef, @values);
  308. my $id = $dbh->bz_last_key($table, $class->ID_FIELD);
  309. return $class->new($id);
  310. }
  311. sub get_all {
  312. my $class = shift;
  313. return @{$class->_do_list_select()};
  314. }
  315. ###############################
  316. #### Validators ######
  317. ###############################
  318. sub check_boolean { return $_[1] ? 1 : 0 }
  319. 1;
  320. __END__
  321. =head1 NAME
  322. Bugzilla::Object - A base class for objects in Bugzilla.
  323. =head1 SYNOPSIS
  324. my $object = new Bugzilla::Object(1);
  325. my $object = new Bugzilla::Object({name => 'TestProduct'});
  326. my $id = $object->id;
  327. my $name = $object->name;
  328. =head1 DESCRIPTION
  329. Bugzilla::Object is a base class for Bugzilla objects. You never actually
  330. create a Bugzilla::Object directly, you only make subclasses of it.
  331. Basically, Bugzilla::Object exists to allow developers to create objects
  332. more easily. All you have to do is define C<DB_TABLE>, C<DB_COLUMNS>,
  333. and sometimes C<LIST_ORDER> and you have a whole new object.
  334. You should also define accessors for any columns other than C<name>
  335. or C<id>.
  336. =head1 CONSTANTS
  337. Frequently, these will be the only things you have to define in your
  338. subclass in order to have a fully-functioning object. C<DB_TABLE>
  339. and C<DB_COLUMNS> are required.
  340. =over
  341. =item C<DB_TABLE>
  342. The name of the table that these objects are stored in. For example,
  343. for C<Bugzilla::Keyword> this would be C<keyworddefs>.
  344. =item C<DB_COLUMNS>
  345. The names of the columns that you want to read out of the database
  346. and into this object. This should be an array.
  347. =item C<NAME_FIELD>
  348. The name of the column that should be considered to be the unique
  349. "name" of this object. The 'name' is a B<string> that uniquely identifies
  350. this Object in the database. Defaults to 'name'. When you specify
  351. C<{name => $name}> to C<new()>, this is the column that will be
  352. matched against in the DB.
  353. =item C<ID_FIELD>
  354. The name of the column that represents the unique B<integer> ID
  355. of this object in the database. Defaults to 'id'.
  356. =item C<LIST_ORDER>
  357. The order that C<new_from_list> and C<get_all> should return objects
  358. in. This should be the name of a database column. Defaults to
  359. L</NAME_FIELD>.
  360. =item C<REQUIRED_CREATE_FIELDS>
  361. The list of fields that B<must> be specified when the user calls
  362. C<create()>. This should be an array.
  363. =item C<VALIDATORS>
  364. A hashref that points to a function that will validate each param to
  365. L</create>.
  366. Validators are called both by L</create> and L</set>. When
  367. they are called by L</create>, the first argument will be the name
  368. of the class (what we normally call C<$class>).
  369. When they are called by L</set>, the first argument will be
  370. a reference to the current object (what we normally call C<$self>).
  371. The second argument will be the value passed to L</create> or
  372. L</set>for that field.
  373. The third argument will be the name of the field being validated.
  374. This may be required by validators which validate several distinct fields.
  375. These functions should call L<Bugzilla::Error/ThrowUserError> if they fail.
  376. The validator must return the validated value.
  377. =item C<UPDATE_VALIDATORS>
  378. This is just like L</VALIDATORS>, but these validators are called only
  379. when updating an object, not when creating it. Any validator that appears
  380. here must not appear in L</VALIDATORS>.
  381. L<Bugzilla::Bug> has good examples in its code of when to use this.
  382. =item C<UPDATE_COLUMNS>
  383. A list of columns to update when L</update> is called.
  384. If a field can't be changed, it shouldn't be listed here. (For example,
  385. the L</ID_FIELD> usually can't be updated.)
  386. =item C<NUMERIC_COLUMNS>
  387. When L</update> is called, it compares each column in the object to its
  388. current value in the database. It only updates columns that have changed.
  389. Any column listed in NUMERIC_COLUMNS is treated as a number, not as a string,
  390. during these comparisons.
  391. =item C<DATE_COLUMNS>
  392. This is much like L</NUMERIC_COLUMNS>, except that it treats strings as
  393. dates when being compared. So, for example, C<2007-01-01> would be
  394. equal to C<2007-01-01 00:00:00>.
  395. =back
  396. =head1 METHODS
  397. =head2 Constructors
  398. =over
  399. =item C<new>
  400. =over
  401. =item B<Description>
  402. The constructor is used to load an existing object from the database,
  403. by id or by name.
  404. =item B<Params>
  405. If you pass an integer, the integer is the id of the object,
  406. from the database, that we want to read in. (id is defined
  407. as the value in the L</ID_FIELD> column).
  408. If you pass in a hashref, you can pass a C<name> key. The
  409. value of the C<name> key is the case-insensitive name of the object
  410. (from L</NAME_FIELD>) in the DB.
  411. B<Additional Parameters Available for Subclasses>
  412. If you are a subclass of C<Bugzilla::Object>, you can pass
  413. C<condition> and C<values> as hash keys, instead of the above.
  414. C<condition> is a set of SQL conditions for the WHERE clause, which contain
  415. placeholders.
  416. C<values> is a reference to an array. The array contains the values
  417. for each placeholder in C<condition>, in order.
  418. This is to allow subclasses to have complex parameters, and then to
  419. translate those parameters into C<condition> and C<values> when they
  420. call C<$self->SUPER::new> (which is this function, usually).
  421. If you try to call C<new> outside of a subclass with the C<condition>
  422. and C<values> parameters, Bugzilla will throw an error. These parameters
  423. are intended B<only> for use by subclasses.
  424. =item B<Returns>
  425. A fully-initialized object, or C<undef> if there is no object in the
  426. database matching the parameters you passed in.
  427. =back
  428. =item C<check>
  429. =over
  430. =item B<Description>
  431. Checks if there is an object in the database with the specified name, and
  432. throws an error if you specified an empty name, or if there is no object
  433. in the database with that name.
  434. =item B<Params>
  435. The parameters are the same as for L</new>, except that if you don't pass
  436. a hashref, the single argument is the I<name> of the object, not the id.
  437. =item B<Returns>
  438. A fully initialized object, guaranteed.
  439. =item B<Notes For Implementors>
  440. If you implement this in your subclass, make sure that you also update
  441. the C<object_name> block at the bottom of the F<global/user-error.html.tmpl>
  442. template.
  443. =back
  444. =item C<new_from_list(\@id_list)>
  445. Description: Creates an array of objects, given an array of ids.
  446. Params: \@id_list - A reference to an array of numbers, database ids.
  447. If any of these are not numeric, the function
  448. will throw an error. If any of these are not
  449. valid ids in the database, they will simply
  450. be skipped.
  451. Returns: A reference to an array of objects.
  452. =item C<match>
  453. =over
  454. =item B<Description>
  455. Gets a list of objects from the database based on certain criteria.
  456. Basically, a simple way of doing a sort of "SELECT" statement (like SQL)
  457. to get objects.
  458. All criteria are joined by C<AND>, so adding more criteria will give you
  459. a smaller set of results, not a larger set.
  460. =item B<Params>
  461. A hashref, where the keys are column names of the table, pointing to the
  462. value that you want to match against for that column.
  463. There are two special values, the constants C<NULL> and C<NOT_NULL>,
  464. which means "give me objects where this field is NULL or NOT NULL,
  465. respectively."
  466. If you don't specify any criteria, calling this function is the same
  467. as doing C<[$class-E<gt>get_all]>.
  468. =item B<Returns>
  469. An arrayref of objects, or an empty arrayref if there are no matches.
  470. =back
  471. =back
  472. =head2 Database Manipulation
  473. =over
  474. =item C<create>
  475. Description: Creates a new item in the database.
  476. Throws a User Error if any of the passed-in params
  477. are invalid.
  478. Params: C<$params> - hashref - A value to put in each database
  479. field for this object. Certain values must be set (the
  480. ones specified in L</REQUIRED_CREATE_FIELDS>), and
  481. the function will throw a Code Error if you don't set
  482. them.
  483. Returns: The Object just created in the database.
  484. Notes: In order for this function to work in your subclass,
  485. your subclass's L</ID_FIELD> must be of C<SERIAL>
  486. type in the database. Your subclass also must
  487. define L</REQUIRED_CREATE_FIELDS> and L</VALIDATORS>.
  488. Subclass Implementors: This function basically just
  489. calls L</check_required_create_fields>, then
  490. L</run_create_validators>, and then finally
  491. L</insert_create_data>. So if you have a complex system that
  492. you need to implement, you can do it by calling these
  493. three functions instead of C<SUPER::create>.
  494. =item C<check_required_create_fields>
  495. =over
  496. =item B<Description>
  497. Part of L</create>. Throws an error if any of the L</REQUIRED_CREATE_FIELDS>
  498. have not been specified in C<$params>
  499. =item B<Params>
  500. =over
  501. =item C<$params> - The same as C<$params> from L</create>.
  502. =back
  503. =item B<Returns> (nothing)
  504. =back
  505. =item C<run_create_validators>
  506. Description: Runs the validation of input parameters for L</create>.
  507. This subroutine exists so that it can be overridden
  508. by subclasses who need to do special validations
  509. of their input parameters. This method is B<only> called
  510. by L</create>.
  511. Params: The same as L</create>.
  512. Returns: A hash, in a similar format as C<$params>, except that
  513. these are the values to be inserted into the database,
  514. not the values that were input to L</create>.
  515. =item C<insert_create_data>
  516. Part of L</create>.
  517. Takes the return value from L</run_create_validators> and inserts the
  518. data into the database. Returns a newly created object.
  519. =item C<update>
  520. =over
  521. =item B<Description>
  522. Saves the values currently in this object to the database.
  523. Only the fields specified in L</UPDATE_COLUMNS> will be
  524. updated, and they will only be updated if their values have changed.
  525. =item B<Params> (none)
  526. =item B<Returns>
  527. A hashref showing what changed during the update. The keys are the column
  528. names from L</UPDATE_COLUMNS>. If a field was not changed, it will not be
  529. in the hash at all. If the field was changed, the key will point to an arrayref.
  530. The first item of the arrayref will be the old value, and the second item
  531. will be the new value.
  532. If there were no changes, we return a reference to an empty hash.
  533. =back
  534. =back
  535. =head2 Subclass Helpers
  536. These functions are intended only for use by subclasses. If
  537. you call them from anywhere else, they will throw a C<CodeError>.
  538. =over
  539. =item C<set>
  540. =over
  541. =item B<Description>
  542. Sets a certain hash member of this class to a certain value.
  543. Used for updating fields. Calls the validator for this field,
  544. if it exists. Subclasses should use this function
  545. to implement the various C<set_> mutators for their different
  546. fields.
  547. If your class defines a method called C<_set_global_validator>,
  548. C<set> will call it with C<($value, $field)> as arguments, after running
  549. the validator for this particular field. C<_set_global_validator> does not
  550. return anything.
  551. See L</VALIDATORS> for more information.
  552. =item B<Params>
  553. =over
  554. =item C<$field> - The name of the hash member to update. This should
  555. be the same as the name of the field in L</VALIDATORS>, if it exists there.
  556. =item C<$value> - The value that you're setting the field to.
  557. =back
  558. =item B<Returns> (nothing)
  559. =back
  560. =back
  561. =head2 Simple Validators
  562. You can use these in your subclass L</VALIDATORS> or L</UPDATE_VALIDATORS>.
  563. Note that you have to reference them like C<\&Bugzilla::Object::check_boolean>,
  564. you can't just write C<\&check_boolean>.
  565. =over
  566. =item C<check_boolean>
  567. Returns C<1> if the passed-in value is true, C<0> otherwise.
  568. =back
  569. =head1 CLASS FUNCTIONS
  570. =over
  571. =item C<get_all>
  572. Description: Returns all objects in this table from the database.
  573. Params: none.
  574. Returns: A list of objects, or an empty list if there are none.
  575. Notes: Note that you must call this as C<$class->get_all>. For
  576. example, C<Bugzilla::Keyword->get_all>.
  577. C<Bugzilla::Keyword::get_all> will not work.
  578. =back
  579. =cut