Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
throw new UnexpectedValueException(sprintf('Column %s not found for alias %s', $field, $alias));
}
}
foreach (array_keys($this->relations) as $one) {
$this->relations[$one] = null;
}
$this->additional_data = [];
}
/**
* checks if at least one field was modified since last restore
*
* @return boolean
*/
public function isDirty()
{
foreach (array_keys($this->db_fields) as $field) {
if ($this->isFieldDirty($field)) {
return true;
}
}
return false;
}
/**
* checks if given field was modified since last restore
*
* @param string $field
* @return boolean
*/
public function isFieldDirty($field)
{
$field = strtolower($field);
if ($this->content[$field] === null || $this->content_db[$field] === null) {
return $this->content[$field] !== $this->content_db[$field];
} else if ($this->content[$field] instanceof I18NString || $this->content_db[$field] instanceof I18NString) {
return $this->content[$field] != $this->content_db[$field];
} else {
return (string)$this->content[$field] !== (string)$this->content_db[$field];
}
}
/**
* reverts value of given field to last restored value
*
* @param string $field
* @return mixed the restored value
*/
public function revertValue($field)
{
$field = strtolower($field);
return ($this->content[$field] = $this->content_db[$field]);
}
/**
* returns unmodified value of given field
*
* @param string $field
* @throws InvalidArgumentException
* @return mixed
*/
public function getPristineValue($field)
{
$field = strtolower($field);
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
if (array_key_exists($field, $this->content_db)) {
return $this->content_db[$field];
} else {
throw new InvalidArgumentException(get_class($this) . '::'. $field . ' not found.');
}
}
/**
* intitalize a relationship and get related record(s)
*
* @param string $relation name of relation
* @throws InvalidArgumentException if the relation does not exists
* @return void
*/
public function initRelation($relation)
{
if (!array_key_exists($relation, $this->relations)) {
throw new InvalidArgumentException('Unknown relation: ' . $relation);
}
if ($this->relations[$relation] === null) {
$options = $this->getRelationOptions($relation);
$to_call = [$options['class_name'], $options['assoc_func']];
if (!is_callable($to_call)) {
throw new RuntimeException('assoc_func: ' . join('::', $to_call) . ' is not callable.' );
}
$params = $options['assoc_func_params_func'];
if ($options['type'] === 'has_many') {
$records = function($record) use ($to_call, $params, $options) {$p = (array)$params($record); return call_user_func_array($to_call, array_merge(count($p) ? $p : [null], [$options['order_by']]));};
$this->relations[$relation] = new SimpleORMapCollection($records, $options, $this);
} elseif ($options['type'] === 'has_and_belongs_to_many') {
$records = function($record) use ($to_call, $params, $options) {$p = (array)$params($record); return call_user_func_array($to_call, array_merge(count($p) ? $p : [null], [$options]));};
$this->relations[$relation] = new SimpleORMapCollection($records, $options, $this);
} else {
$p = (array)$params($this);
$records = call_user_func_array($to_call, count($p) ? $p : [null]);
$result = is_array($records) ? $records[0] : $records;
$this->relations[$relation] = $result;
}
}
}
/**
* clear data for a relationship
*
* @param string $relation name of relation
* @throws InvalidArgumentException if teh relation does not exists
*/
public function resetRelation($relation)
{
if (!array_key_exists($relation, $this->relations)) {
throw new InvalidArgumentException('Unknown relation: ' . $relation);
}
$this->relations[$relation] = null;
}
/**
* invoke registered callbacks for given type
* if one callback returns false the following will not
* be invoked
*
* @param string $type type of callback
* @return bool return value from last callback
*/
protected function applyCallbacks($type)
{
$ok = true;
foreach ($this->registered_callbacks[$type] as $cb) {
if ($cb instanceof Closure) {
$function = $cb;
$params = [$this, $type, $cb];
} else {
$function = [$this, $cb];
$params = [$type];
};
$ok = call_user_func_array($function, $params);
if ($ok === false) {
break;
}
}
return $ok;
}
/**
* register given callback for one or many possible callback types
* callback param could be a closure or method name of current class
*
* @param string|array $types types to register callback for
* @param mixed $cb callback
* @throws InvalidArgumentException if the callback type is not known
* @return number of registered callbacks
*/
protected function registerCallback($types, $cb)
{
$types = is_array($types) ? $types : words($types);
foreach ($types as $type) {
if (isset($this->registered_callbacks[$type])) {
$this->registered_callbacks[$type][] = $cb;
$reg++;
} else {
throw new InvalidArgumentException('Unknown callback type: ' . $type);
}
}
return $reg;
}
/**
* unregister given callback for one or many possible callback types
*
* @param string|array $types types to unregister callback for
* @param mixed $cb
* @throws InvalidArgumentException if the callback type is not known
* @return number of unregistered callbacks
*/
protected function unregisterCallback($types, $cb)
{
$types = is_array($types) ? $types : words($types);
foreach ($types as $type) {
if (isset($this->registered_callbacks[$type])) {
$found = array_search($cb, $this->registered_callbacks[$type], true);
if ($found !== false) {
$unreg++;
unset($this->registered_callbacks[$type][$found]);
}
} else {
throw new InvalidArgumentException('Unknown callback type: ' . $type);
}
}
return $unreg;
}
/**
* default callback for tables with auto_increment primary key
*
* @param string $type callback type
* @return boolean
*/
protected function cbAutoIncrementColumn($type)
{
if ($type == 'after_create' && !$this->getId()) {
$this->setId(DBManager::get()->lastInsertId());
}
if ($type == 'before_store' && $this->isNew() && $this->getId() === null) {
$this->setId(0);
}
return true;
}
/**
* default callback for tables without auto_increment
*/
protected function cbAutoKeyCreation()
{
if ($this->isNew() && $this->getId() === null) {
$this->setId($this->getNewId());
}
}
/**
* default callback used to map specific callbacks to NotificationCenter
*
* @param string $cb_type callback type
* @return boolean
*/
protected function cbNotificationMapper($cb_type)
{
if (isset($this->notification_map[$cb_type])) {
try {
foreach(words($this->notification_map[$cb_type]) as $notification) {
NotificationCenter::postNotification($notification, $this);
}
} catch (NotificationVetoException $e) {
return false;
}
}
}
/**
* default callback used to map specific callbacks to NotificationCenter
*
* @param string $cb_type callback type
* @return boolean
*/
protected function cbAfterInitialize($cb_type)
{
foreach (array_keys($this->db_fields) as $field) {
if (is_object($this->content[$field])) {
$this->content_db[$field] = clone $this->content[$field];
} else {
$this->content_db[$field] = $this->content[$field];
}
}
}
/**
* default setter used to proxy serialized fields with
* ArrayObjects
*
* @param string $field column name
* @param mixed $value value
* @return mixed
*/
protected function setSerializedValue($field, $value)
{
$object_type = $this->serialized_fields[$field];
if (is_null($value) || $value instanceof $object_type) {
$this->content[$field] = $value;
} else {
$this->content[$field] = new $object_type($value);
}
return $this->content[$field];
}
/**
* default setter used to proxy I18N fields with
* I18NString
*
* @param string $field column name
* @param mixed $value value
* @return mixed
*/
protected function setI18nValue($field, $value)
{
$meta = ['object_id' => $this->getId(),
'table' => $this->db_table,
'field' => $field];
if ($value instanceof I18NString) {
$value->setMetadata($meta);
$this->content[$field] = $value;
} else {
$this->content[$field] = new I18NString($value, null, $meta);
}
return $this->content[$field];
}
/**
* default callback for tables with I18N fields
* @param $type
* @return bool
*/
protected function cbI18N($type)
{
if ($type == 'before_store') {
$i18ncontent = [];
foreach (array_keys($this->i18n_fields) as $field) {
if ($this->content[$field] instanceof I18NString) {
$i18ncontent[$field] = $this->content[$field];
$this->content[$field] = $this->content[$field]->original();
$this->content_db[$field] = $this->content_db[$field]->original();
}
}
if (count($i18ncontent)) {
$after_store = function($that, $type, $myself) use ($i18ncontent) {
foreach ($i18ncontent as $field => $one) {
$meta = ['object_id' => $this->getId(),
'table' => $this->db_table,
'field' => $field];
$one->setMetadata($meta);
$one->storeTranslations();
if (!$this->content[$field] instanceof I18NString) {
$this->content[$field] = $one;
$this->content_db[$field] = clone $one;
}
}
$this->unregisterCallback('after_store', $myself);
};
$this->registerCallback('after_store', $after_store);
}
}
if ($type == 'after_delete') {
foreach (array_keys($this->i18n_fields) as $field) {
if ($this->content[$field] instanceof I18NString) {
$this->content[$field]->removeTranslations();
}
}
}
return true;
}
/**
* Cleans up this object. This essentially reset all relations of
* this object and marks them as unused so that the garbage collector may
* remove the objects.
*
* Use this function when you ran into memory problems and need to free
* some memory;
*/
public function cleanup()
{
foreach ($this->relations as $relation => $object) {
if ($object instanceof SimpleORMap || $object instanceof SimpleORMapCollection) {
$this->relations[$relation]->cleanup();
}
$this->resetRelation($relation);
}
}
}