/** * Save in database */ public function save() { if (method_exists($this, 'customSave')) { $this->customSave(); } else { if ($this->id) { $this->updateRecord($this->toDBData(), 'id'); } else { $this->insertRecord($this->toDBData()); $this->id = (int) DBI::lastInsertId(); } } $a = get_called_class(); self::$objectCache[get_called_class()][$this->pad_id . '_' . $this->guest_id] = $this; }
/** * Save in database */ public function customSave() { if ($this->id) { // Cannot happened } else { $guest = $this->getByEmail($this->email); if (!$guest) { $this->insertRecord($this->toDBData()); $this->id = (int) DBI::lastInsertId(); } else { $this->id = $guest->id; } } // Cache self::$objectCache[get_class()][$this->id] = $this; }
/** * Save in database */ public function customSave() { //TODO : manage guests // // TODO: Manage date if ($this->id) { // Get new guests lists (this) $cPad = NotesFactory::build(PadComponent::getClass(), $this->id); // If guest list differs if ($this->guests !== $cPad->guests) { foreach (PadGuestComponent::getByPadID($this->id) as $padGuest) { $padGuest->delete(); } foreach ($this->guests as $guest) { $cGuest = GuestComponent::getByEmail($guest->email); // If guest not found in DB, creating it if (!$cGuest) { $cGuest = NotesFactory::build(GuestComponent::getClass(), $guest); $cGuest->save(); } // Create the PadGuest link $padGuest = NotesFactory::build(PadGuestComponent::getClass(), array('pad_id' => $this->id, 'guest_id' => $cGuest->id)); $padGuest->save(); } } $this->updateRecord($this->toDBData(), 'id'); } else { // Insert record $this->insertRecord($this->toDBData()); $this->id = (int) DBI::lastInsertId(); if (count($this->guests) > 0) { foreach ($this->guests as $tmpGuest) { if (!$tmpGuest->id) { $tmpGuest->save(); } $joint = NotesFactory::build(PadGuestComponent::getClass(), array('pad_id' => $this->id, 'guest_id' => $tmpGuest->id)); $joint->save(); } } } self::$objectCache[get_class()][$this->id] = $this; }
/** * Insert object as new record * * @param array $data database compliant data such as returned by toDBData */ public static function insertRecord($data) { $event = new Event('dbobject_insert', static::getClassName(), $data); $table = static::getDBTable(); $datamap = static::getDataMap(); return $event->trigger(function ($class, $data) use($table, $datamap) { // Remove autoinc keys foreach ($datamap as $field_name => $dfn) { if (array_key_exists('autoinc', $dfn) && $dfn['autoinc']) { if (array_key_exists($field_name, $data)) { unset($data[$field_name]); } } } // Insert data $values = array(); foreach ($data as $field_name => $value) { $values[':' . $field_name] = $value; } $s = DBI::prepare('INSERT INTO ' . $table . '(' . implode(', ', array_keys($data)) . ') VALUES(:' . implode(', :', array_keys($data)) . ')'); $s->execute($values); // Get primary key(s) back $pks = array(); foreach ($datamap as $field_name => $dfn) { if (array_key_exists('autoinc', $dfn) && $dfn['autoinc']) { $pks[$field_name] = DBI::lastInsertId($table . '_' . $field_name . '_seq'); } } return $pks; }); }
/** * Insert object as new record * * @param array $data database compliant data such as returned by toDBData */ public static function insertRecord($data) { $table = static::getDBTable(); // Remove autoinc keys foreach (static::$dataMap as $field_name => $dfn) { if (array_key_exists('autoinc', $dfn) && $dfn['autoinc']) { if (array_key_exists($field_name, $data)) { unset($data[$field_name]); } } } // Insert data $values = array(); foreach ($data as $field_name => $value) { $values[':' . $field_name] = $value; } $s = DBI::prepare('INSERT INTO ' . $table . '(' . implode(', ', array_keys($data)) . ') VALUES(:' . implode(', :', array_keys($data)) . ')'); $s->execute($values); // Get primary key(s) back $pks = array(); foreach (static::$dataMap as $field_name => $dfn) { if (array_key_exists('autoinc', $dfn) && $dfn['autoinc']) { $pks[$field_name] = DBI::lastInsertId($table . '_' . $field_name . '_seq'); } } return $pks; }
/** * Save in database */ public function customSave() { if ($this->id) { $this->updateRecord($this->toDBData(), 'id'); } else { $this->insertRecord($this->toDBData()); $this->id = (int) DBI::lastInsertId(); } self::$objectCache[get_class()][$this->id] = $this; }