These classes give you access to database-wide and individual Table object features respectively. The primary use of the schema system is for Fixtures. However, it can also be used in your application if required. The schema subsystem provides a simple TableSchema object to hold data about a table in a database. This object is returned by the schema reflection features:.
It helps to normalize and validate the data used to describe a table. For example, the following two forms are equivalent:. While equivalent, the 2nd form allows more detail and control. Columns are either added as constructor arguments, or via addColumn. Once fields are added information can be fetched using column or columns :.
Indexes are added using the addIndex. Please refer to the Changing Accessible Fields section for more information. By setting the guard option to false , you can ignore the accessible field list for a single call to set.
It is often necessary to know if an entity represents a row that is already in the database. In those situations use the isNew method:. If you are certain that an entity has already been persisted, you can use setNew :. While eager loading associations is generally the most efficient way to access your associations, there may be times when you need to lazily load associated data.
Before we get into how to lazy load associations, we should discuss the differences between eager loading and lazy loading associations:. Eager loading uses joins where possible to fetch data from the database in as few queries as possible. When a separate query is required, like in the case of a HasMany association, a single query is emitted to fetch all the associated data for the current set of objects. Lazy loading defers loading association data until it is absolutely required.
While this can save CPU time because possibly unused data is not hydrated into objects, it can result in many more queries being emitted to the database. We recommend the LazyLoad Plugin. You may find yourself needing the same logic in multiple entity classes. By convention traits in CakePHP are suffixed with Trait so they can be discernible from classes or interfaces. Traits are often a good complement to behaviors, allowing you to provide functionality for the table and entity objects.
For example if we had SoftDeletable plugin, it could provide a trait. CakePHP makes this simple:. Entities are recursively converted to JSON as well. This means that if you eager loaded entities and their associations CakePHP will correctly handle converting the associated data into the correct format. By default virtual fields are not exported when converting entities to arrays or JSON.
In order to expose virtual fields you need to make them visible. When defining your entity class you can provide a list of virtual field that should be exposed:.
This list can be modified at runtime using the setVirtual method:. There are often fields you do not want exported in JSON or array formats. For example it is often unwise to expose password hashes or account recovery questions. When defining an entity class, define which fields should be hidden:. This list can be modified at runtime using the setHidden method:. Holds model associations temporarily to allow for dynamic un binding.
Instance of the CakeEventManager this model is using to dispatch inner events. Container for the data that this model gets from persistent storage usually, a database.
List of valid finder method options, supplied as the first parameter to find. Automatically set during Model creation. List of table names included in the model description. Used for associations. Name of the validation string domain to use when translating validation errors. Helper method for saveAll and friends, to add foreign key to fieldlist. Protected helper method to create associated models of a given class. Cascades model deletes through associated hasMany and hasOne child records.
Passes query results through model and behavior afterFilter methods. Detects the previous field's value, then uses logic to find the 'wrapping' rows and return them. Normalize Xml::toArray to use in Model::save. Helper method for Model::updateCounterCache.
Checks the fields to be updated for. Called before each save operation, after validation. Return a non-true result to halt the save. Builds the query array that is used by the data source to generate the query to fetch the data. Deconstructs a complex data type array or object into a single field value.
Removes record for given ID. If no ID is given, the current ID is used. Returns true on success. Returns the contents of a single field given the supplied conditions, in the supplied order. Returns an associative array of field names and column types. Returns true if the supplied field exists in the model's database table.
Returns an array of fields that have failed validation. On the current model. Returns true if given field name is a foreign key in this model.
Saves a single record, as well as all its directly associated records. Saves the value of a single field to the database, based on the current model ID. Sets a custom table for your model class. Used by your controller to select a database table. Updates the counter cache of belongsTo associations after a save or delete operation. Validates a single record, as well as all its directly associated records. Binds the model's database table to the object.
Adding new find types Behaviors and find types can also define custom finder keys which are passed into find. If a single field is specified, 'id' is used for key and specified field is used for value. If three fields are specified, they are used in order for key, value and group.
Otherwise, first and second fields are used for key and value. Note: find count has its own return values. Returns integer Number of rows. Returns array Field types indexed by field name. Returns CakeEventManager. Returns mixed Last inserted ID. You can override this function to add you own listener callbacks Returns array. Normally called at the end of each call to Model::find Returns boolean Success. Using 'before' or 'after' will enable only those callbacks.
Options validate : Set to false to disable validation, true to validate each record before saving, 'first' to validate all records before any are saved default , or 'only' to only validate the records, but not save them.
It should be an associate array with model name as key and array of fields as value. Options validate : Set to false to disable validation, true to validate each record before saving, 'first' to validate all records before any are saved default , atomic : If true default , will attempt to save all records in a single transaction.
This should be an array indexed by association name. Options atomic : If true default , returns boolean. If false returns array. Wraps exit making testing easier. Each class can override this method as necessary. Returns string The name of this class. Holds the Behavior objects currently bound to this model. Back containable association array. Back inner association array. Back original association array.
Default list of association keys. The ID of the model record that was last inserted. Field-by-field table metadata.
0コメント