Kohana 3 ORM - Wie kann man sagen, wenn das aktuelle Modell gespeichert wird, ist neu?
-
27-09-2019 - |
Frage
In meinem ORM-Modell, würde Ich mag einige Standardwerte speichern, die auf der Grundlage anderen Werten berechnet werden. Das Beste, was ich tun konnte, ist:
function save(){
if( ! $this->loaded()){
// Set values here
}
parent::save();
}
Weiß jemand, ob es ein besserer / empfohlene Weg, dies zu tun, oder sollte dies für die meisten Fälle ausreichend sein? Danke:)
Lösung
Ihre Implementierung ist gut. Die einzige Verbesserung, die Sie tun können, ist nur ersetzen Ihre Bedingung:
if (!$this->_loaded) {
Andere Tipps
Es `s sehr gute Frage.
Unfortunnaly Weg nur möglich.
Unter ORM :: create () Funktion Weicht von ORM genannt :: save ().
public function create(Validation $validation = NULL)
{
if ($this->_loaded)
throw new Kohana_Exception('Cannot create :model model because it is already loaded.', array(':model' => $this->_object_name));
// Require model validation before saving
if ( ! $this->_valid OR $validation)
{
$this->check($validation);
}
$data = array();
foreach ($this->_changed as $column)
{
// Generate list of column => values
$data[$column] = $this->_object[$column];
}
if (is_array($this->_created_column))
{
// Fill the created column
$column = $this->_created_column['column'];
$format = $this->_created_column['format'];
$data[$column] = $this->_object[$column] = ($format === TRUE) ? time() : date($format);
}
$result = DB::insert($this->_table_name)
->columns(array_keys($data))
->values(array_values($data))
->execute($this->_db);
if ( ! array_key_exists($this->_primary_key, $data))
{
// Load the insert id as the primary key if it was left out
$this->_object[$this->_primary_key] = $this->_primary_key_value = $result[0];
}
else
{
$this->_primary_key_value = $this->_object[$this->_primary_key];
}
// Object is now loaded and saved
$this->_loaded = $this->_saved = TRUE;
// All changes have been saved
$this->_changed = array();
$this->_original_values = $this->_object;
return $this;
}
Wie Sie sehen können, gibt es keine defaut Werte ...
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow