Bugzilla::Object - A base class for objects in Bugzilla.
my $object = new Bugzilla::Object(1); my $object = new Bugzilla::Object({name => 'TestProduct'}); my $id = $object->id; my $name = $object->name;
Bugzilla::Object is a base class for Bugzilla objects. You never actually create a Bugzilla::Object directly, you only make subclasses of it.
Basically, Bugzilla::Object exists to allow developers to create objects more easily. All you have to do is define DB_TABLE
, DB_COLUMNS
, and sometimes LIST_ORDER
and you have a whole new object.
You should also define accessors for any columns other than name
or id
.
Frequently, these will be the only things you have to define in your subclass in order to have a fully-functioning object. DB_TABLE
and DB_COLUMNS
are required.
DB_TABLE
The name of the table that these objects are stored in. For example, for Bugzilla::Keyword
this would be keyworddefs
.
DB_COLUMNS
The names of the columns that you want to read out of the database and into this object. This should be an array.
NAME_FIELD
The name of the column that should be considered to be the unique "name" of this object. The 'name' is a string that uniquely identifies this Object in the database. Defaults to 'name'. When you specify {name =
$name}> to new()
, this is the column that will be matched against in the DB.
ID_FIELD
The name of the column that represents the unique integer ID of this object in the database. Defaults to 'id'.
LIST_ORDER
The order that new_from_list
and get_all
should return objects in. This should be the name of a database column. Defaults to "NAME_FIELD".
REQUIRED_CREATE_FIELDS
The list of fields that must be specified when the user calls create()
. This should be an array.
VALIDATORS
A hashref that points to a function that will validate each param to "create".
Validators are called both by "create" and "set". When they are called by "create", the first argument will be the name of the class (what we normally call $class
).
When they are called by "set", the first argument will be a reference to the current object (what we normally call $self
).
The second argument will be the value passed to "create" or "set"for that field.
The third argument will be the name of the field being validated. This may be required by validators which validate several distinct fields.
These functions should call "ThrowUserError" in Bugzilla::Error if they fail.
The validator must return the validated value.
UPDATE_VALIDATORS
This is just like "VALIDATORS", but these validators are called only when updating an object, not when creating it. Any validator that appears here must not appear in "VALIDATORS".
Bugzilla::Bug has good examples in its code of when to use this.
UPDATE_COLUMNS
A list of columns to update when "update" is called. If a field can't be changed, it shouldn't be listed here. (For example, the "ID_FIELD" usually can't be updated.)
NUMERIC_COLUMNS
When "update" is called, it compares each column in the object to its current value in the database. It only updates columns that have changed.
Any column listed in NUMERIC_COLUMNS is treated as a number, not as a string, during these comparisons.
DATE_COLUMNS
This is much like "NUMERIC_COLUMNS", except that it treats strings as dates when being compared. So, for example, 2007-01-01
would be equal to 2007-01-01 00:00:00
.
new
The constructor is used to load an existing object from the database, by id or by name.
If you pass an integer, the integer is the id of the object, from the database, that we want to read in. (id is defined as the value in the "ID_FIELD" column).
If you pass in a hashref, you can pass a name
key. The value of the name
key is the case-insensitive name of the object (from "NAME_FIELD") in the DB. You can also pass in an id
key which will be interpreted as the id of the object you want (overriding the name
key).
Additional Parameters Available for Subclasses
If you are a subclass of Bugzilla::Object
, you can pass condition
and values
as hash keys, instead of the above.
condition
is a set of SQL conditions for the WHERE clause, which contain placeholders.
values
is a reference to an array. The array contains the values for each placeholder in condition
, in order.
This is to allow subclasses to have complex parameters, and then to translate those parameters into condition
and values
when they call $self-
SUPER::new> (which is this function, usually).
If you try to call new
outside of a subclass with the condition
and values
parameters, Bugzilla will throw an error. These parameters are intended only for use by subclasses.
A fully-initialized object, or undef
if there is no object in the database matching the parameters you passed in.
check
Checks if there is an object in the database with the specified name, and throws an error if you specified an empty name, or if there is no object in the database with that name.
The parameters are the same as for "new", except that if you don't pass a hashref, the single argument is the name of the object, not the id.
A fully initialized object, guaranteed.
If you implement this in your subclass, make sure that you also update the object_name
block at the bottom of the global/user-error.html.tmpl template.
new_from_list(\@id_list)
Description: Creates an array of objects, given an array of ids. Params: \@id_list - A reference to an array of numbers, database ids. If any of these are not numeric, the function will throw an error. If any of these are not valid ids in the database, they will simply be skipped. Returns: A reference to an array of objects.
match
Gets a list of objects from the database based on certain criteria.
Basically, a simple way of doing a sort of "SELECT" statement (like SQL) to get objects.
All criteria are joined by AND
, so adding more criteria will give you a smaller set of results, not a larger set.
A hashref, where the keys are column names of the table, pointing to the value that you want to match against for that column.
There are two special values, the constants NULL
and NOT_NULL
, which means "give me objects where this field is NULL or NOT NULL, respectively."
In addition to the column keys, there are a few special keys that can be used to rig the underlying database queries. These are LIMIT
, OFFSET
, and WHERE
.
The value for the LIMIT
key is expected to be an integer defining the number of objects to return, while the value for OFFSET
defines the position, relative to the number of objects the query would normally return, at which to begin the result set. If OFFSET
is defined without a corresponding LIMIT
it is silently ignored.
The WHERE
key provides a mechanism for adding arbitrary WHERE clauses to the underlying query. Its value is expected to a hash reference whose keys are the columns, operators and placeholders, and the values are the placeholders' bind value. For example:
WHERE => { 'some_column >= ?' => $some_value }
would constrain the query to only those objects in the table whose 'some_column' column has a value greater than or equal to $some_value.
If you don't specify any criteria, calling this function is the same as doing [$class->get_all]
.
An arrayref of objects, or an empty arrayref if there are no matches.
create
Description: Creates a new item in the database. Throws a User Error if any of the passed-in params are invalid.
Params: $params
- hashref - A value to put in each database field for this object. Certain values must be set (the ones specified in "REQUIRED_CREATE_FIELDS"), and the function will throw a Code Error if you don't set them.
Returns: The Object just created in the database.
Notes: In order for this function to work in your subclass, your subclass's "ID_FIELD" must be of SERIAL
type in the database. Your subclass also must define "REQUIRED_CREATE_FIELDS" and "VALIDATORS".
Subclass Implementors: This function basically just calls L</check_required_create_fields>, then L</run_create_validators>, and then finally L</insert_create_data>. So if you have a complex system that you need to implement, you can do it by calling these three functions instead of C<SUPER::create>.
check_required_create_fields
Part of "create". Throws an error if any of the "REQUIRED_CREATE_FIELDS" have not been specified in $params
$params
- The same as $params
from "create".run_create_validators
Description: Runs the validation of input parameters for "create". This subroutine exists so that it can be overridden by subclasses who need to do special validations of their input parameters. This method is only called by "create".
Params: The same as "create".
Returns: A hash, in a similar format as $params
, except that these are the values to be inserted into the database, not the values that were input to "create".
insert_create_data
Part of "create".
Takes the return value from "run_create_validators" and inserts the data into the database. Returns a newly created object.
update
Saves the values currently in this object to the database. Only the fields specified in "UPDATE_COLUMNS" will be updated, and they will only be updated if their values have changed.
In scalar context:
A hashref showing what changed during the update. The keys are the column names from "UPDATE_COLUMNS". If a field was not changed, it will not be in the hash at all. If the field was changed, the key will point to an arrayref. The first item of the arrayref will be the old value, and the second item will be the new value.
If there were no changes, we return a reference to an empty hash.
In array context:
Returns a list, where the first item is the above hashref. The second item is the object as it was in the database before update() was called. (This is mostly useful to subclasses of Bugzilla::Object
that are implementing update
.)
remove_from_db
Removes this object from the database. Will throw an error if you can't remove it for some reason. The object will then be destroyed, as it is not safe to use the object after it has been removed from the database.
These are used for updating the values in objects, before calling update
.
set
Sets a certain hash member of this class to a certain value. Used for updating fields. Calls the validator for this field, if it exists. Subclasses should use this function to implement the various set_
mutators for their different fields.
If your class defines a method called _set_global_validator
, set
will call it with ($value, $field)
as arguments, after running the validator for this particular field. _set_global_validator
does not return anything.
See "VALIDATORS" for more information.
NOTE: This function is intended only for use by subclasses. If you call it from anywhere else, it will throw a CodeError
.
$field
- The name of the hash member to update. This should be the same as the name of the field in "VALIDATORS", if it exists there.$value
- The value that you're setting the field to.set_all
This is a convenience function which is simpler than calling many different set_
functions in a row. You pass a hashref of parameters and it calls set_$key($value)
for every item in the hashref.
Takes a hashref of the fields that need to be set, pointing to the value that should be passed to the set_
function that is called.
You can use these in your subclass "VALIDATORS" or "UPDATE_VALIDATORS". Note that you have to reference them like \&Bugzilla::Object::check_boolean
, you can't just write \&check_boolean
.
check_boolean
Returns 1
if the passed-in value is true, 0
otherwise.
get_all
Description: Returns all objects in this table from the database. Params: none. Returns: A list of objects, or an empty list if there are none. Notes: Note that you must call this as C<$class->get_all>. For example, C<Bugzilla::Keyword->get_all>. C<Bugzilla::Keyword::get_all> will not work.