/** * Get notified of an event * * @param Model $instance * @param string $event */ public static function orm_notify(Model $instance, $event) { if (!array_key_exists($event, static::$events)) { return; } $event_type = static::$events[$event]; $properties = $instance->properties(); foreach ($properties as $p => $settings) { if (empty($settings['data_type']) || in_array($p, $instance->primary_key())) { continue; } if ($instance->{$p} === null) { if (array_key_exists('null', $settings) and $settings['null'] === false) { throw new InvalidContentType('The property "' . $p . '" cannot be NULL.'); } continue; } foreach (static::$type_methods as $match => $method) { if (is_array($method)) { $method = !empty($method[$event_type]) ? $method[$event_type] : false; } if ($method === false) { continue; } if ($method and preg_match($match, $settings['data_type']) > 0) { $instance->{$p} = call_user_func($method, $instance->{$p}, $settings); continue; } } } }
/** * AutoGenerate the on for the specified relationship. * * @return string * @author Justin Palmer **/ private function autoGenerateOn($name) { $options = $this->relationships->get($name); $ret = ''; switch ($options->type) { case 'has-one': $ret = $this->model->alias() . "." . $this->model->primary_key() . " = " . $options->alias . "." . $options->foreign_key; break; case 'has-many': $ret = $options->table . "." . $options->foreign_key . " = ?"; break; } return $ret; }
/** * Get notified of an event * * @param Model $instance * @param string $event */ public static function orm_notify(Model $instance, $event) { // if we don't serve this event, bail out immediately if (array_key_exists($event, static::$events)) { // get the event type of the event that triggered us $event_type = static::$events[$event]; // fetch the model's properties $properties = $instance->properties(); // and check if we need to do any datatype conversions foreach ($properties as $p => $settings) { // the property is part of the primary key, skip it if (in_array($p, $instance->primary_key())) { continue; } $instance->{$p} = static::typecast($p, $instance->{$p}, $settings, $event_type); } } }
/** * Save the model record * * @return void * @author Justin Palmer **/ public function saveNow() { $database_name = $this->model->database_name(); $table_name = $this->model->table_name(); $primary_key_name = $this->model->primary_key(); $id = $primary_key_name; //var_dump($this->model->$id); if ($this->model->{$id} === null) { try { $this->model->created_at = new Expression('NOW()'); } catch (NoColumnInTableException $e) { } //print 'insert' . '<br/>'; $props = $this->model->props()->export(); $columns = $this->getInsertColumnNames($props); $marks = $this->getValues($props); $this->Statement = $this->prepare(sprintf("INSERT INTO `{$database_name}`.`{$table_name}` (%s) values (%s)", $columns, $marks)); $params = array_values($this->model->props()->export()); if ($this->Statement->execute($params)) { $ret = true; $this->model->{$primary_key_name} = $this->lastInsertId(); } else { $ret = (object) $this->Statement->errorInfo(); } return $ret; } else { try { $this->model->updated_at = new Expression('NOW()'); } catch (NoColumnInTableException $e) { } //print 'update' . '<br/>'; $id = $this->model->{$primary_key_name}; $this->model->removeProperty($primary_key_name); //Get the props before setting the primary key for the UpdateSet method $props = $this->model->props()->export(); $query = "UPDATE `{$database_name}`.`{$table_name}` SET %s WHERE `{$primary_key_name}` = ?"; $this->Statement = $this->prepare(sprintf($query, $this->getUpdateSet($props))); $this->model->{$primary_key_name} = $id; return $this->Statement->execute($this->getUpdateValues()) ? true : false; } }
/** * Get notified of an event * * @param Model $instance * @param string $event */ public static function orm_notify(Model $instance, $event) { if (!array_key_exists($event, static::$events)) { return; } $event_type = static::$events[$event]; $properties = $instance->properties(); foreach ($properties as $p => $settings) { if (empty($settings['type']) || in_array($p, $instance->primary_key())) { continue; } if ($instance->{$p} === null) { continue; } foreach (static::$type_methods as $match => $method) { if (is_array($method)) { $method = !empty($method[$event_type]) ? $method[$event_type] : false; } if ($method and preg_match($match, $settings['type']) > 0) { $instance->{$p} = call_user_func($method, $instance->{$p}, $settings['type']); } } } }
/** * Returns an array of the primary keys that are not related to temporal * timestamp information. */ public static function getNonTimestampPks() { $timestamp_start_name = static::temporal_property('start_column'); $timestamp_end_name = static::temporal_property('end_column'); $pks = array(); foreach (parent::primary_key() as $key) { if ($key != $timestamp_start_name && $key != $timestamp_end_name) { $pks[] = $key; } } return $pks; }