[ Index ]
 

Code source de PHPonTrax 2.6.6-svn

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/vendor/trax/ -> active_record.php (sommaire)

File containing the ActiveRecord class (PHP 5)

Copyright: (c) 2005 John Peterson
Version: $Id: active_record.php 263 2006-09-04 04:50:57Z john $
Poids: 2805 lignes (108 kb)
Inclus ou requis: 1 fois
Référencé: 0 fois
Nécessite: 0 fichiers

Définit 1 class

ActiveRecord:: (85 méthodes):
  __construct()
  __get()
  __set()
  __call()
  find_all_habtm()
  find_all_has_many()
  find_one_has_one()
  find_one_belongs_to()
  aggregate_all()
  get_join_table_name()
  is_new_record()
  column_for_attribute()
  column_type()
  column_attribute_exists()
  send()
  begin()
  commit()
  rollback()
  query()
  find_by()
  find_all()
  find()
  find_first()
  find_by_sql()
  reload()
  load()
  create()
  update()
  update_all()
  save_without_validation()
  save()
  add_record_or_update_record()
  add_record()
  update_record()
  get_composite_object()
  get_association_type()
  save_associations()
  save_association()
  delete()
  delete_all()
  set_habtm_attributes()
  update_habtm_records()
  add_habtm_records()
  delete_habtm_records()
  delete_all_habtm_records()
  check_datetime()
  update_attributes()
  update_composite_attributes()
  get_attributes()
  quoted_attributes()
  get_inserts()
  get_primary_key_conditions()
  get_updates_sql()
  set_table_name_using_class_name()
  get_class_name()
  set_content_columns()
  get_insert_id()
  establish_connection()
  is_error()
  raise()
  add_error()
  get_errors()
  get_errors_as_string()
  attribute_is_string()
  is_composite()
  valid()
  validate_model_attributes()
  validate()
  validate_on_create()
  validate_on_update()
  before_validation()
  after_validation()
  before_validation_on_create()
  after_validation_on_create()
  before_validation_on_update()
  after_validation_on_update()
  before_save()
  after_save()
  before_create()
  after_create()
  before_update()
  after_update()
  before_delete()
  after_delete()
  log_query()


Classe: ActiveRecord  - X-Ref

Base class for the ActiveRecord design pattern

<p>Each subclass of this class is associated with a database table
in the Model section of the Model-View-Controller architecture.
By convention, the name of each subclass is the CamelCase singular
form of the table name, which is in the lower_case_underscore
plural notation.  For example,
a table named "order_details" would be associated with a subclass
of ActiveRecord named "OrderDetail", and a table named "people"
would be associated with subclass "Person".  See the tutorial
{@tutorial PHPonTrax/naming.pkg}</p>

<p>For a discussion of the ActiveRecord design pattern, see
"Patterns of Enterprise
Application Architecture" by Martin Fowler, pp. 160-164.</p>

<p>Unit tester: {@link ActiveRecordTest}</p>

__construct($attributes = null)   X-Ref
Construct an ActiveRecord object

<ol>
<li>Establish a connection to the database</li>
<li>Find the name of the table associated with this object</li>
<li>Read description of this table from the database</li>
<li>Optionally apply update information to column attributes</li>
</ol>
param: string[] $attributes Updates to column attributes

__get($key)   X-Ref
Override get() if they do $model->some_association->field_name
dynamically load the requested contents from the database.


__set($key, $value)   X-Ref
Store column value or description of the table format

If called with key 'table_name', $value is stored as the
description of the table format in $content_columns.
Any other key causes an object variable with the same name to
be created and stored into.  If the value of $key matches the
name of a column in content_columns, the corresponding object
variable becomes the content of the column in this row.

__call($method_name, $parameters)   X-Ref
Override call() to dynamically call the database associations


find_all_habtm($other_table_name, $parameters = null)   X-Ref
Find all records using a "has_and_belongs_to_many" relationship
(many-to-many with a join table in between).  Note that you can also
specify an optional "paging limit" by setting the corresponding "limit"
instance variable.  For example, if you want to return 10 movies from the
5th movie on, you could set $this->movies_limit = "10, 5"

Parameters: $this_table_name:  The name of the database table that has the
one row you are interested in.  E.g. genres
$other_table_name: The name of the database table that has the
many rows you are interested in.  E.g. movies
Returns: An array of ActiveRecord objects. (e.g. Movie objects)

find_all_has_many($other_table_name, $parameters = null)   X-Ref
Find all records using a "has_many" relationship (one-to-many)

Parameters: $other_table_name: The name of the other table that contains
many rows relating to this object's id.
Returns: An array of ActiveRecord objects. (e.g. Contact objects)

find_one_has_one($other_object_name, $parameters = null)   X-Ref
Find all records using a "has_one" relationship (one-to-one)
(the foreign key being in the other table)
Parameters: $other_table_name: The name of the other table that contains
many rows relating to this object's id.
Returns: An array of ActiveRecord objects. (e.g. Contact objects)


find_one_belongs_to($other_object_name, $parameters = null)   X-Ref
Find all records using a "belongs_to" relationship (one-to-one)
(the foreign key being in the table itself)
Parameters: $other_object_name: The singularized version of a table name.
E.g. If the Contact class belongs_to the
Customer class, then $other_object_name
will be "customer".


aggregate_all($aggregate_type, $parameters = null)   X-Ref
Implement *_all() functions (SQL aggregate functions)

Apply one of the SQL aggregate functions to a column of the
table associated with this object.  The SQL aggregate
functions are AVG, COUNT, MAX, MIN and SUM.  Not all DBMS's
implement all of these functions.
param: string $agrregrate_type SQL aggregate function to
param: string[] $parameters  Conditions to apply to the

get_join_table_name($first_table, $second_table)   X-Ref
Returns a the name of the join table that would be used for the two
tables.  The join table name is decided from the alphabetical order
of the two tables.  e.g. "genres_movies" because "g" comes before "m"

Parameters: $first_table, $second_table: the names of two database tables,
e.g. "movies" and "genres"

is_new_record()   X-Ref
Test whether this object represents a new record

return: boolean Whether this object represents a new record

column_for_attribute($attribute)   X-Ref
get the attributes for a specific column.


column_type($attribute)   X-Ref
get the columns  data type.


column_attribute_exists($attribute)   X-Ref
Check whether a column exists in the associated table

When called, {@link $content_columns} lists the columns in
the table described by this object.
param: string Name of the column
return: boolean true=>the column exists; false=>it doesn't

send($column)   X-Ref
Get contents of one column of record selected by id and table

When called, {@link $id} identifies one record in the table
identified by {@link $table}.  Fetch from the database the
contents of column $column of this record.
param: string Name of column to retrieve

begin()   X-Ref
Only used if you want to do transactions and your db supports transactions


commit()   X-Ref
Only used if you want to do transactions and your db supports transactions


rollback()   X-Ref
Only used if you want to do transactions and your db supports transactions


query($sql)   X-Ref
Perform an SQL query and return the results

param: string $sql  SQL for the query command
return: $mdb2->query {@link http://pear.php.net/manual/en/package.database.mdb2.intro-query.php}

find_by($method_name, $parameters, $find_type = null)   X-Ref
Implement find_by_*() and =_* methods

Converts a method name beginning 'find_by_' or 'find_all_by_'
into a query for rows matching the rest of the method name and
the arguments to the function.  The part of the method name
after '_by' is parsed for columns and logical relationships
(AND and OR) to match.  For example, the call
find_by_fname('Ben')
is converted to
SELECT * ... WHERE fname='Ben'
and the call
find_by_fname_and_lname('Ben','Dover')
is converted to
SELECT * ... WHERE fname='Ben' AND lname='Dover'


find_all($conditions = null, $order = null, $limit = null, $joins = null)   X-Ref
Return rows selected by $conditions

If no rows match, an empty array is returned.
param: string  SQL to use in the query.  If
param: string  Argument to "ORDER BY" in query.
param: integer[] Page, rows per page???
param: string ???
return: object[] Array of objects of the same class as this

find($id, $order = null, $limit = null, $joins = null)   X-Ref
Find row(s) with specified value(s)

Find all the rows in the table which match the argument $id.
Return zero or more objects of the same class as this
class representing the rows that matched the argument.
param: mixed[] $id  If $id is an array then a query will be
param: string $order Argument to "ORDER BY" in query.
param: integer[] $limit Page, rows per page???
param: string $joins ???
return: mixed Results of query.  If $id was a scalar then the

find_first($conditions = null, $order = null, $limit = null, $joins = null)   X-Ref
Return first row selected by $conditions

If no rows match, null is returned.
param: string $conditions SQL to use in the query.  If
param: string $order Argument to "ORDER BY" in query.
param: integer[] $limit Page, rows per page??? @todo Document this parameter
param: string $joins ??? @todo Document this parameter
return: mixed An object of the same class as this class and

find_by_sql($sql)   X-Ref
Return all the rows selected by the SQL argument

If no rows match, an empty array is returned.
param: string $sql SQL to use in the query.

reload($conditions = null)   X-Ref
Reloads the attributes of this object from the database.


load($conditions = null)   X-Ref
Loads into current object values from the database.


create($attributes, $dont_validate = false)   X-Ref


update($id, $attributes, $dont_validate = false)   X-Ref
Finds the record from the passed id, instantly saves it with the passed attributes
(if the validation permits it). Returns true on success and false on error.


update_all($updates, $conditions = null)   X-Ref
Updates all records with the SET-part of an SQL update statement in updates and
returns an integer with the number of rows updates. A subset of the records can
be selected by specifying conditions.
Example:
$model->update_all("category = 'cooldude', approved = 1", "author = 'John'");


save_without_validation($attributes = null)   X-Ref
Save without valdiating anything.


save($attributes = null, $dont_validate = false)   X-Ref
Create or update a row in the table with specified attributes

param: string[] $attributes List of name => value pairs giving
param: boolean $dont_validate true => Don't call validation
return: boolean

add_record_or_update_record()   X-Ref
Create or update a row in the table

If this object represents a new row in the table, insert it.
Otherwise, update the exiting row.  before_?() and after_?()
routines will be called depending on whether the row is new.
return: boolean

add_record()   X-Ref
Insert a new row in the table associated with this object

Build an SQL INSERT statement getting the table name from
{@link $table_name}, the column names from {@link
$content_columns} and the values from object variables.
Send the insert to the RDBMS.
return: boolean

update_record()   X-Ref
Update the row in the table described by this object

The primary key attributes must exist and have appropriate
non-null values.  If a column is listed in {@link
$content_columns} but no attribute of that name exists, the
column will be set to the null string ''.
return: boolean

get_composite_object($name)   X-Ref
Loads the model values into composite object


get_association_type($association_name)   X-Ref
returns the association type if defined in child class or null

return: mixed Association type, one of the following:

save_associations()   X-Ref
Saves any associations objects assigned to this instance


save_association($object, $type)   X-Ref
save the association to the database


delete($id = null)   X-Ref
Deletes the record with the given $id or if you have done a
$model = $model->find($id), then $model->delete() it will delete
the record it just loaded from the find() without passing anything
to delete(). If an array of ids is provided, all ids in array are deleted.


delete_all($conditions = null)   X-Ref
Delete from table all rows that match argument

Delete the row(s), if any, matching the argument.
param: string $conditions SQL argument to "WHERE" describing
return: boolean

set_habtm_attributes($attributes)   X-Ref


update_habtm_records($this_foreign_value)   X-Ref


add_habtm_records($this_foreign_value)   X-Ref


delete_habtm_records($this_foreign_value)   X-Ref


delete_all_habtm_records($other_table_name, $this_foreign_value)   X-Ref
Pas de description

check_datetime($field, $value)   X-Ref
Apply automatic timestamp updates

If automatic timestamps are in effect (as indicated by
{@link $auto_timestamps} == true) and the column named in the
$field argument is of type "timestamp" and matches one of the
names in {@link auto_create_timestamps} or {@link
auto_update_timestamps}(as selected by {@link $new_record}),
then return the current date and  time as a string formatted
to insert in the database.  Otherwise return $value.
param: string $field Name of a column in the table
param: mixed $value Value to return if $field is not an
return: mixed Current date and time or $value

update_attributes($attributes)   X-Ref
Update object attributes from list in argument

The elements of $attributes are parsed and assigned to
attributes of the ActiveRecord object.  Date/time fields are
treated according to the
{@tutorial PHPonTrax/naming.pkg#naming.naming_forms}.
param: string[] $attributes List of name => value pairs giving

update_composite_attributes()   X-Ref
If a composite object was specified via $composed_of, then its values
mapped to the model will overwrite the models values.


get_attributes()   X-Ref
Return pairs of column-name:column-value

Return the contents of the object as an array of elements
where the key is the column name and the value is the column
value.  Relies on a previous call to
{@link set_content_columns()} for information about the format
of a row in the table.

quoted_attributes($attributes = null)   X-Ref
Return pairs of column-name:quoted-column-value

Return pairs of column-name:quoted-column-value where the key
is the column name and the value is the column value with
automatic timestamp updating applied and characters special to
SQL quoted.

If $attributes is null or omitted, return all columns as
currently stored in {@link content_columns()}.  Otherwise,
return the name:value pairs in $attributes.
param: string[] $attributes Name:value pairs to return.
return: string[]

get_inserts()   X-Ref
Return column values for SQL insert statement

Return an array containing the column names and values of this
object, filtering out the primary keys, which are not set.


get_primary_key_conditions()   X-Ref
Return argument for a "WHERE" clause specifying this row

Returns a string which specifies the column(s) and value(s)
which describe the primary key of this row of the associated
table.  The primary key must be one or more attributes of the
object and must be listed in {@link $content_columns} as
columns in the row.

Example: if $primary_keys = array("id", "ssn") and column "id"
has value "5" and column "ssn" has value "123-45-6789" then
the string "id = '5' AND ssn = '123-45-6789'" would be returned.
return: string Column name = 'value' [ AND name = 'value']...

get_updates_sql()   X-Ref
Return column values of object formatted for SQL update statement

Return a string containing the column names and values of this
object in a format ready to be inserted in a SQL UPDATE
statement.  Automatic update has been applied to timestamps if
enabled and characters special to SQL have been quoted.
return: string Column name = 'value', ... for all attributes

set_table_name_using_class_name()   X-Ref
Set {@link $table_name} from the class name of this object

By convention, the name of the database table represented by
this object is derived from the name of the class.

get_class_name()   X-Ref
Get class name of child object

this will return the manually set name or get_class($this)
return: string child class name

set_content_columns($table_name)   X-Ref
Populate object with information about the table it represents

Call {@link
http://pear.php.net/manual/en/package.database.db.db-common.tableinfo.php
DB_common::tableInfo()} to get a description of the table and
store it in {@link $content_columns}.  Add a more human
friendly name to the element for each column.
param: string $table_name  Name of table to get information about

get_insert_id()   X-Ref
Returns the autogenerated id from the last insert query


establish_connection()   X-Ref
Open a database connection if one is not currently open

The name of the database normally comes from
$database_settings which is set in {@link
environment.php} by reading file config/database.ini. The
database name may be overridden by assigning a different name
to {@link $database_name}.

If there is a connection now open, as indicated by the saved
value of a MDB2 object in $active_connections[$connection_name], and
{@link force_reconnect} is not true, then set the database
fetch mode and return.

If there is no connection, open one and save a reference to
it in $active_connections[$connection_name].


is_error($obj)   X-Ref
Test whether argument is a PEAR Error object or a MDB2 Error object.

param: object $obj Object to test
return: boolean  Whether object is one of these two errors

raise($message)   X-Ref
Throw an exception describing an error in this object


add_error($error, $key = null)   X-Ref
Add or overwrite description of an error to the list of errors

param: string $error Error message text
param: string $key Key to associate with the error (in the

get_errors($return_string = false, $seperator = "<br>")   X-Ref
Return description of non-fatal errors

param: boolean $return_string
param: string $seperator  String to concatenate between error
return: mixed Error description(s), if any

get_errors_as_string($seperator = "<br>")   X-Ref
Return errors as a string.

Concatenate all error descriptions into a stringusing
$seperator between elements and return the string.
param: string $seperator  String to concatenate between error
return: string Concatenated error description(s), if any

attribute_is_string($attribute, $column = null)   X-Ref
Determine if passed in attribute (table column) is a string

param: string $attribute Name of the table column

is_composite($name)   X-Ref
Determine if passed in name is a composite class or not

param: string $name Name of the composed_of mapping

valid()   X-Ref
Runs validation routines for update or create

return: boolean

validate_model_attributes()   X-Ref
Call every method named "validate_*()" where * is a column name

Find and call every method named "validate_something()" where
"something" is the name of a column.  The "validate_something()"
functions are expected to return an array whose first element
is true or false (indicating whether or not the validation
succeeded), and whose second element is the error message to
display if the first element is false.

return: boolean

validate()   X-Ref
Overwrite this method for validation checks on all saves and
use $this->errors[] = "My error message."; or
for invalid attributes $this->errors['attribute'] = "Attribute is invalid.";


validate_on_create()   X-Ref
Override this method for validation checks used only on creation.


validate_on_update()   X-Ref
Override this method for validation checks used only on updates.


before_validation()   X-Ref
Is called before validate().


after_validation()   X-Ref
Is called after validate().


before_validation_on_create()   X-Ref
Is called before validate() on new objects that haven't been saved yet (no record exists).


after_validation_on_create()   X-Ref
Is called after validate() on new objects that haven't been saved yet (no record exists).


before_validation_on_update()   X-Ref
Is called before validate() on existing objects that has a record.


after_validation_on_update()   X-Ref
Is called after validate() on existing objects that has a record.


before_save()   X-Ref
Is called before save() (regardless of whether its a create or update save)


after_save()   X-Ref
Is called after save (regardless of whether its a create or update save).


before_create()   X-Ref
Is called before save() on new objects that havent been saved yet (no record exists).


after_create()   X-Ref
Is called after save() on new objects that havent been saved yet (no record exists).


before_update()   X-Ref
Is called before save() on existing objects that has a record.


after_update()   X-Ref
Is called after save() on existing objects that has a record.


before_delete()   X-Ref
Is called before delete().


after_delete()   X-Ref
Is called after delete().


log_query($query)   X-Ref
Log SQL query in development mode

If running in development mode, log the query to self::$query_log
param: string SQL to be logged



Généré le : Sun Feb 25 20:04:38 2007 par Balluche grâce à PHPXref 0.7