Welcome to the Cookbook

loading...

3.7.7 Callback Methods

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

If you want to sneak in some logic just before or after a CakePHP model operation, use model callbacks. These functions can be defined in model classes (including your AppModel) class. Be sure to note the expected return values for each of these special functions.

Please note that these callbacks are not called when dealing with associated models; callbacks are only executed for the main model of a query.

3.7.7.1 beforeFind

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

beforeFind(mixed $queryData)

Called before any find-related operation. The $queryData passed to this callback contains information about the current query: conditions, fields, etc.

If you do not wish the find operation to begin (possibly based on a decision relating to the $queryData options), return false. Otherwise, return the possibly modified $queryData, or anything you want to get passed to find and its counterparts.

You might use this callback to restrict find operations based on a user’s role, or make caching decisions based on the current load.

3.7.7.2 afterFind

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

afterFind(array $results, bool $primary)

Use this callback to modify results that have been returned from a find operation, or to perform any other post-find logic. The $results parameter passed to this callback contains the returned results from the model's find operation, i.e. something like:

$results = array(
  0 => array(
    'ModelName' => array(
      'field1' => 'value1',
      'field2' => 'value2',
    ),
  ),
);
  1. $results = array(
  2. 0 => array(
  3. 'ModelName' => array(
  4. 'field1' => 'value1',
  5. 'field2' => 'value2',
  6. ),
  7. ),
  8. );

The return value for this callback should be the (possibly modified) results for the find operation that triggered this callback.

The $primary parameter indicates whether or not the current model was the model that the query originated on or whether or not this model was queried as an association. If a model is queried as an assocation the format of $results can differ; instead of the result you would normally get from a find operation, you may get this:

$results = array(
  'field_1' => 'value1',
  'field_2' => 'value2'
);
  1. $results = array(
  2. 'field_1' => 'value1',
  3. 'field_2' => 'value2'
  4. );

Code expecting $primary to be true will probably get a "Cannot use string offset as an array" fatal error from PHP if a recursive find is used.

Below is an example of how afterfind can be used for date formating.

function afterFind($results) {
	foreach ($results as $key => $val) {
		if (isset($val['Event']['begindate'])) {
			$results[$key]['Event']['begindate'] = $this->dateFormatAfterFind($val['Event']['begindate']);
		}
	}
	return $results;
}

function dateFormatAfterFind($dateString) {
	return date('d-m-Y', strtotime($dateString));
}
  1. function afterFind($results) {
  2. foreach ($results as $key => $val) {
  3. if (isset($val['Event']['begindate'])) {
  4. $results[$key]['Event']['begindate'] = $this->dateFormatAfterFind($val['Event']['begindate']);
  5. }
  6. }
  7. return $results;
  8. }
  9. function dateFormatAfterFind($dateString) {
  10. return date('d-m-Y', strtotime($dateString));
  11. }

3.7.7.3 beforeValidate

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

beforeValidate()

Use this callback to modify model data before it is validated, or to modify validation rules if required. This function must also return true, otherwise the current save() execution will abort.

3.7.7.4 beforeSave

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

beforeSave()

Place any pre-save logic in this function. This function executes immediately after model data has been successfully validated, but just before the data is saved. This function should also return true if you want the save operation to continue.

This callback is especially handy for any data-massaging logic that needs to happen before your data is stored. If your storage engine needs dates in a specific format, access it at $this->data and modify it.

Below is an example of how beforeSave can be used for date conversion. The code in the example is used for an application with a begindate formatted like YYYY-MM-DD in the database and is displayed like DD-MM-YYYY in the application. Of course this can be changed very easily. Use the code below in the appropriate model.

function beforeSave() {
	if (!empty($this->data['Event']['begindate']) && !empty($this->data['Event']['enddate'])) {
    		$this->data['Event']['begindate'] = $this->dateFormatBeforeSave($this->data['Event']['begindate']);
    		$this->data['Event']['enddate'] = $this->dateFormatBeforeSave($this->data['Event']['enddate']);
	}
	return true;
}

function dateFormatBeforeSave($dateString) {
	return date('Y-m-d', strtotime($dateString)); // Direction is from 
}
  1. function beforeSave() {
  2. if (!empty($this->data['Event']['begindate']) && !empty($this->data['Event']['enddate'])) {
  3. $this->data['Event']['begindate'] = $this->dateFormatBeforeSave($this->data['Event']['begindate']);
  4. $this->data['Event']['enddate'] = $this->dateFormatBeforeSave($this->data['Event']['enddate']);
  5. }
  6. return true;
  7. }
  8. function dateFormatBeforeSave($dateString) {
  9. return date('Y-m-d', strtotime($dateString)); // Direction is from
  10. }

Be sure that beforeSave() returns true, or your save is going to fail.

3.7.7.5 afterSave

Der Originaltext dieses Abschnitts hat sich seit der letzen Übersetzung geändert. Bitte hilf dabei die Unterschiede zu lösen. Du kannst:

Mehr Informationen zu Übersetzungen

afterSave(boolean $created)

Wenn Befehle direkt nach der Speichern-Funktion ausgeführt werden sollen, dann schreibt man sie in diese callback-Funktion.

Der Wert von $created ist wahr, wenn ein neues Objekt erstellt wurde (anstelle einer Aktualisierung)

3.7.7.6 beforeDelete

beforeDelete(boolean $cascade)

Wenn Befehle direkt vor der Löschen-Funktion ausgeführt werden sollen, dann schreibt man sie in diese callback-Funktion. Diese Funktion muss true zurückgeben, damit das Löschen ausgeführt wird und false, wenn das Löschen abgebrochen werden soll.

Der Wert von $cascade ist true, wenn Datensätze, die von dem zu löschenden Datensatz abhängen, ebenfalls gelöscht werden.

3.7.7.7 afterDelete

afterDelete()

Wenn Befehle nach dem Löschen ausgeführt werden sollen, schreibt man sie in diese callback-Funktion.

3.7.7.8 onError

onError()

Wird aufgerufen, wenn irgendwelche Probleme auftauchen.