CoughObject is the foundation for which all other "Model" / "ORM" classes extend. There will usually be one class extending CoughObject for each table in the database that an ORM is needed for.
It might be wise to add your own AppModel / AppCoughObject class that extends CoughObject and have all your classes extend that one; this way you can add custom functionality to Cough without modifying the Cough source.
Located in /cough/CoughObject.class.php (line 14)
An array of all the loaded collections in form [collectionName] => [CoughCollection]
An array of derived field definitions
Format of "derived_field_name" => attributes
An array of derived fields (read-only, as in not saved back to the database).
Format of "derived_field_name" => value
An array of all the columns in the database, including the primary key column and name columns.
Format of "field_name" => attributes
An array of all the currently initialized or set fields.
Format of "field_name" => value
Stores whether or not the object has been deleted from the database.
Save will do nothing if an object has been deleted...
Stores whether or not the object is new (i.e. not in database yet).
Save will perform an INSERT if $isNew is true, otherwise it will perform an UPDATE as long as hasKeyId() also returns true.
Note that isNew() = !isInflated(). That is, any time an object is inflated, it is considered to be synced with the database, and therefore not new.
An array of fields that have been modified.
An array of all the objects and their attributes.
The information is used by CoughObject to instantiate and load the objects.
Format of [objectName] => [array of attributes]
TODO: Document that array of attributes. For now just look at the one of the generated class's defineObjects().
An array of all the loaded objects in form [objectName] => [CoughObject]
Keep track of whether or not data has been validated.
Stores validation errors set by `validateData` function.
Format of "field_name" => "Error Text"
Builds a basic SELECT table.* FROM db.table query object (making it easy to inject joins, where criteria, order by, group by, etc.)
Constructs a new object from a single id (for single key PKs) or a hash of [field_name] => [field_value].
The key is used to pull data from the database, and, if no data is found, null is returned. You can use this function with any unique keys or the primary key as long as a hash is used. If the primary key is a single field, you may pass its value in directly without using a hash.
Constructs a new object from custom SQL.
Helper method for generating SELECT criteria for other join tables.
For example, you might need the following SQL:
But, rather than hand coding all the fields (which might change) on the manufacturer join, you can use getFieldAliases():
Returns a sub array of the given array containing all elements that have keys starting with the specified prefix. The resulting array's keys have the prefix removed.
Returns a sub array of the given array containing all elements that have keys starting with the specified prefix. The resulting array's keys have the prefix removed.
The array passed by reference has all the items that were added to the returned array removed.
Unsets all keys on the given array that start with the specified prefix.
Construct an empty CoughObject or an object with pre-initialized data.
Clear validation status.
Override in sub-class to define derived fields the object may possess.
Override in sub-class to define fields the object possesses, including $pkFieldNames.
Override in sub-class to define objects the object possesses.
Returns object to it's own state, except what you want to keep. By default it empties everything; overridden version only empties yours.
Deletes the record from the database, if hasKeyId returns true.
Override this for special delete functionality. For example, if an object should never be deleted, but instead just retired, then override this to look like:
$this->setIsRetired(true); $this->save();
Then just make sure the load queries include is_retired = 0 in the WHERE clause so that the item is not pulled from the database (i.e. it appears deleted, but the data is still there for historical reference/backup.)
Usually, the coder will be the only one to call delete, but Cough will call it on join objects when removing objects in a many-to-many collection.
Do the actual data validation. Override in sub classes.
Called at the end of __construct(). Override for special construction behavior that is dependent on the object's state.
Returns the specified derived field name.
Returns the current value of the requested field name.
Returns the object's fields as an array of [key] => [value] pairs.
Only the fields that directly correspond to columns in the database are returned. This means no:
For example, you can store the raw data in a session and reconstruct the object with it later, so you don't waste space in the session, and you don't have to re-pull the data from the database:
// E.g. save user data when they log in. $_SESSION['User'] = $user->getFields();
// Then reconstruct the object later: $user = new User($_SESSION['User']);
Gets fields by going through their getter methods, so that polymorphism is not broken (you can override getFieldName(), and that is the value you'll get).
Get all non-primary key related fields and their values.
Returns the fields that should be used for inserting. This logic is separate from the insert method to make it easy to override the behavior.
Returns the current value of the object's primary key id.
If the key is multi-key, this returns a unique string identifying itself (a concatenation of the fields making up the PK).
Get a list of all of this object's modified values.
Returns the primary key as an array of [field_name] => field_value pairs
Returns the fields that should be used for updating. This logic is separate from the update method to make it easy to override the behavior.
Returns the validation errors set by `validateData()`.
Returns true if all the key fields that make up the primary key are set to non-null values.
Whether or not there is at least one modified field.
Inflate/Invigorate the object with data.
This helper method for inflate() handles inflation of related objects.
Sets the cough object's basic identity:
Inserts a new row to the database and sets the object's key id with the returned database insert id.
If the object has a multi-key PK, then the key is not set after insert.
By default, only values that have been modified are used in the INSERT statement, leaving it up to the database to set default values for the other fields. To change this, override the getInsertFields() method.
Invalidates a field with an optional message.
Returns true if the data is valid (i.e. no validation errors set), otherwise it returns false.
Specifies whether or not the object has been deleted from the database.
This will compare two CoughObjects and return true if they are of the same type and have the same field values (excluding the primary key).
Feel free to override this in sub classes for customized comparison.
Whether or not the specified field has been modified.
Returns true if the field is valid (i.e. no validation errors set), otherwise it returns false.
Returns whether or not the object is inflated (i.e. pulled from persistent storage)
This method returns the opposite of isNew()
The reason this method isn't called `isLoaded()` is because it's possible for an object to become inflated from data that was not loaded from a database / persistent storage. It's meant to answer the question, "Can I call the getters on this object and expect to get meaningful values?"
Returns whether or not the object is new (i.e. not in persistent storage)
This method returns the opposite of isInflated()
This method's job is to notify related collections (if any) of the key change.
For example, if the schema is "order has many order lines" then on the Order object the `notifyChildrenOfKeyChange` function might change the order_id on all the order_line entities. The code might look like:
foreach ($this->getOrderLine_Collection() as $orderLine) { $orderLine->setOrderId($key['order_id']); }
Clear the list of modified fields and other modified flags.
Creates a new entry if needed, otherwise it updates an existing one.
During an update, it only updates the modified fields. During an insert, it only inserts modified fields (leaving defaults up to the database server).
Saves all the loaded collections (if it wasn't loaded there would be nothing to save)
Saves all the loaded objects (if it wasn't loaded there would be nothing to save)
Sets a read-only field; It's usually a derived field from a complex SQL query such as when overriding the getLoadSql() function.
Sets the current value of $fieldName to $value.
Sets the current value of the all the object's defined fields equal to the values passed in the $fields associative array.
Note: if a field passed in the fields associative array isn't in the class's defined fields, it will NOT BE SET.
Sets fields in the given hash, but only if they new values are different from the existing values
Sets fields by going through their setter methods, so that polymorphism is not broken (you can override setFieldName($val), and that is the method that will be called).
Sets the object's primary key id to the passed value.
If key is multi-key:
* Call this with an array of [field_name] => [field_value] pairs to set all values uniquely.
* Call this with a non-array value to set all keys to the same value.
Add a field to the list of modified fields
Indicates whether or not a save should insert a new record or update an existing one.
Updates the database with modified values, if any.
Validates data stored in the model. It is called automatically by `save`, which will return false if this function sets any errors.
If you pass it nothing, it will validate with the current data stored in the object.
Clone only the non-primary key fields.
Usage: $newProduct = clone $product;
Documentation generated on Fri, 19 Dec 2008 11:03:50 -0600 by phpDocumentor 1.4.0