Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

line ending fix

  • Loading branch information...
commit e42e863af09c721924d527cf044a71975445512a 1 parent 63ceb24
@mstdokumaci authored
Showing with 647 additions and 647 deletions.
  1. +412 −412 lib/adb.php
  2. +235 −235 lib/item.php
View
824 lib/adb.php
@@ -1,413 +1,413 @@
-<?php
-
- require_once('cache.php');
- require_once('db.php');
- require_once('ddl.php');
- require_once('item.php');
-
- class ADB {
- private static $instance;
- public $ROW, $COUNT, $LIST;
- public $db, $cache;
- private $DM;
-
- function __construct ($DM) {
- $this->ROW=$this->LIST=$this->COUNT=array();
- $this->db=DB::get_instance();
- $this->cache=CACHE::get_instance();
-
- $cached_dm=$this->cache->get('__DATA_MODEL__');
- $cached_hash=$this->cache->get('__DATA_MODEL_HASH__');
- if ($cached_dm!==false && $cached_hash!==false && $cached_hash==md5(serialize($DM))) {
- $this->DM=$cached_dm;
- return;
- }
-
- $this->DM=$DM;
-
- foreach ($DM as $name=>$item) {
- $item=$this->DM[$name]+$this->get_initial_item();
- $item['conf']+=$this->get_initial_config();
-
- foreach ($item['fields'] as $f_name=>$field) {
- $field+=$this->get_initial_field_data($field);
- $item['fields'][$f_name]=$field;
- }
-
- foreach ($item['has_many'] as $local_name=>$has_many) {
- $has_many['local_name']=is_string($local_name) ? $local_name : $has_many['type'];
- if (!(isset($has_many['foreign_name']))) $has_many['foreign_name']=$name;
- $this->DM[$has_many['type']]['fields'][$has_many['foreign_name']]=array(
- 'type'=>'numeric',
- 'len'=>$item['conf']['len'],
- 'foreign'=>array('type'=>$name, 'field'=>$has_many['local_name']),
- 'index'=>true
- ) + $this->get_initial_field_data(array('type'=>'numeric'));
- $item['has_many'][$local_name]=$has_many;
- }
-
- foreach ($item['many_to_many'] as $local_name=>$m2m) {
- if (isset($this->DM[$name]['many_to_many'][$local_name]['done'])) continue;
- $m2m['local_name']=is_string($local_name) ? $local_name : $m2m['type'];
- if (!isset($m2m['foreign_name'])) $m2m['foreign_name']=$name;
- if (!isset($m2m['relation_name'])) {
- $relation_name=array($m2m['foreign_name'], $m2m['local_name']);
- sort($relation_name);
- $m2m['relation_name']=implode('_', $relation_name);
- }
- $this->DM[$m2m['type']]['many_to_many'][$m2m['foreign_name']]=array(
- 'done'=>true,
- 'type'=>$name,
- 'local_name'=>$m2m['foreign_name'],
- 'foreign_name'=>$m2m['local_name'],
- 'relation_name'=>$m2m['relation_name']
- );
- $item['many_to_many'][$local_name]=$m2m;
- }
-
- $this->DM[$name]=$item;
- }
-
- $this->cache->set('__DATA_MODEL_HASH__', md5(serialize($DM)));
- $this->cache->set('__DATA_MODEL__', $this->DM);
- }
-
- static function init ($DM) {
- self::$instance=new ADB ($DM);
- }
-
- static function get_instance () {
- if (!isset(self::$instance))
- throw new Exception('You have to initialize this class before using');
- return self::$instance;
- }
-
- function create_tables () {
- $this->cache->flush();
- $ddl=new DDL($this->DM);
- $ddl->create_tables();
- }
-
- function load ($name, $id) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- if (isset($this->ROW[$name][$id]))
- return new ITEM($name, $this->DM[$name], $id, $this->ROW[$name][$id]);
- else
- return new ITEM($name, $this->DM[$name], $id);
- }
-
- function create ($name, $data) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- $item_model=$this->DM[$name];
-
- $insert=$foreigns=array();
- foreach ($data as $k=>$v) {
- if (!isset($item_model['fields'][$k])) {continue;}
- $field=$item_model['fields'][$k];
- if (isset($field['filter']) && function_exists($field['filter'])) {$v=eval('return ' . $field['filter'] . '($v);');}
- $insert[$k]=$v;
- if ($field['foreign']!==false) {
- $field['foreign']['id']=$v;
- $foreigns[]=$field['foreign'];
- }
- }
- $insert['create_date']=$_SERVER['REQUEST_TIME'];
-
- $id=$this->db->insert($name, $insert);
-
- foreach ($foreigns as $foreign) {
- $foreign_item=$this->load($foreign['type'], intval($foreign['id']));
- $foreign_item->add_relation($foreign['field'], $id);
- }
-
- unset($this->LIST[$name], $this->COUNT[$name]);
-
- return $id;
- }
-
- function delete ($name, $id, $delete_belongings=false) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- $item_model=$this->DM[$name];
-
- $item=$this->load($name, $id);
-
- $this->cache->delete('item_' . $name . '_' . $id);
-
- $this->db->delete($name, "id='" . $id . "'");
-
- foreach (array_filter($item_model['fields'], function ($el) {return $el['foreign']!==false;}) as $k=>$f) {
- $foreign_item=$this->load($f['foreign']['type'], intval($item[$k]));
- $foreign_item->delete_relation($f['foreign']['field'], $id);
- }
- foreach ($item_model['has_many'] as $has_many) {
- foreach ($item[$has_many['local_name']] as $foreign_id) {
- if ($delete_belongings) {
- $this->delete($has_many['type'], $foreign_id, $delete_belongings);
- } else {
- $foreign_item=$this->load($has_many['type'], $foreign_id);
- $foreign_item[$has_many['foreign_name']]=0;
- }
- }
- }
- foreach ($item_model['many_to_many'] as $m2m) {
- foreach ($item[$m2m['local_name']] as $foreign_id) {
- $this->unrelate($name, $m2m['local_name'], $id, $foreign_id);
- }
- }
- foreach ($item_model['self_ref'] as $self_ref) {
- foreach ($item[$self_ref] as $self_ref_id) {
- $this->self_unrelate($name, $self_ref, $id, $self_ref_id);
- }
- }
-
- unset($this->LIST[$name], $this->COUNT[$name]);
-
- unset($this->ITEM[$name][$id]);
- unset($this->ROW[$name][$id]);
-
- $item->delete();
- }
-
- function relate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- $m2m=array_shift(array_filter($this->DM[$name]['many_to_many'], function ($m2m) use ($local_name) {return $m2m['local_name']==$local_name;}));
- if (empty($m2m)) return $this->self_relate($name, $local_name, $id1, $id2);
-
- $item1=$this->load($name, $id1);
- $item2=$this->load($m2m['type'], $id2);
-
- $list=$item1[$local_name];
- if (in_array($id2, $list)) {return;}
-
- $insert=array($m2m['foreign_name']=>$id1, $local_name=>$id2);
- $this->db->insert($m2m['relation_name'], $insert);
-
- $item1->add_relation($local_name, $id2);
- $item2->add_relation($m2m['foreign_name'], $id1);
- }
-
- function unrelate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- $m2m=array_shift(array_filter($this->DM[$name]['many_to_many'], function ($m2m) use ($local_name) {return $m2m['local_name']==$local_name;}));
- if (empty($m2m)) return $this->self_unrelate($name, $local_name, $id1, $id2);
-
- $item1=$this->load($name, $id1);
- $item2=$this->load($m2m['type'], $id2);
-
- $condition="`" . $m2m['foreign_name'] . "`='" . $id1 . "' AND `" . $local_name . "`='" . $id2 . "'";
- $this->db->delete($m2m['relation_name'], $condition);
-
- $item1->delete_relation($local_name, $id2);
- $item2->delete_relation($m2m['foreign_name'], $id1);
- }
-
- private function self_relate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- if (!(in_array($local_name, $this->DM[$name]['self_ref'])))
- throw new Exception('No defined relation to relate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
-
- $item1=$this->load($name, $id1);
- $item2=$this->load($name, $id2);
-
- $list=$item1[$local_name];
- if (in_array($id2, $list)) {return;}
-
- $insert=array($name . '1'=>$id1, $name . '2'=>$id2);
- $this->db->insert($local_name, $insert);
-
- $item1->add_relation($local_name, $id2);
- $item2->add_relation($local_name, $id1);
- }
-
- private function self_unrelate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- if (!(in_array($local_name, $this->DM[$name]['self_ref'])))
- throw new Exception('No defined relation to unrelate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
-
- $condition="(`" . $name . "1`='" . $id1 . "' AND `" . $name . "2`='" . $id2 . "') OR (`" . $name . "1`='" . $id2 . "' AND `" . $name . "2`='" . $id1 . "')";
- $this->db->delete($local_name, $condition);
-
- $item1=$this->load($name, $id1);
- $item2=$this->load($name, $id2);
-
- $item1->delete_relation($local_name, $id2);
- $item2->delete_relation($local_name, $id1);
- }
-
- function find_unique ($name, $field, $value) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
-
- if (!isset($this->DM[$name]['fields'][$field]))
- throw new Exception('No field as ' . $field . ' found for item ' . $name);
-
- $sql="SELECT * FROM `" . $name . "` WHERE `" . $field . "`='" . $this->db->escape($value) . "'";
- $result=$this->db->select($sql);
- if (count($result)) {
- $this->ROW[$name][$result[0]['id']]=$result[0];
- return $this->load($name, $result[0]['id']);
- }
-
- return false;
- }
-
- function count ($name, $condition=false, $order=false) {
- if (isset($this->COUNT[$name][$condition])) return $this->COUNT[$name][$condition];
-
- $sql=$this->prepare_select($name, $condition, $order);
-
- $this->COUNT[$name][$condition]=$this->db->count($sql);
-
- return $this->COUNT[$name][$condition];
- }
-
- function id_list ($name, $condition=false, $order=false, $limit=false) {
- if (isset($this->LIST[$name][$condition][$order][$limit])) return $this->LIST[$name][$condition][$order][$limit];
-
- $sql=$this->prepare_select($name, $condition, $order);
-
- if ($limit!==false) {$sql.=" LIMIT " . $limit;}
-
- $return=$this->LIST[$name][$condition][$order][$limit]=array();
-
- foreach ($this->db->select($sql) as $row) {
- $this->ROW[$name][intval($row['id'])]=$row;
- $return[]=intval($row['id']);
- }
-
- $this->LIST[$name][$condition][$order][$limit]=$return;
-
- return $return;
- }
-
- function count_join ($name, $table, $condition, $order) {
- if (isset($this->COUNT[$name][$table][$condition])) return $this->COUNT[$name][$table][$condition];
-
- $sql=$this->prepare_join_select($name, $table, $condition, $order);
-
- $this->COUNT[$name][$table][$condition]=$this->db->count($sql);
-
- return $this->COUNT[$name][$table][$condition];
- }
-
- function id_list_join ($name, $table, $condition=false, $order=false, $limit=false) {
- if (isset($this->LIST[$name][$table][$condition][$order][$limit])) return $this->LIST[$name][$table][$condition][$order][$limit];
-
- $sql=$this->prepare_join_select($name, $table, $condition, $order);
-
- if ($limit!==false) {$sql.=" LIMIT " . $limit;}
-
- $return=$this->LIST[$name][$table][$condition][$order][$limit]=array();
-
- foreach ($this->db->select($sql) as $row) {
- $this->ROW[$name][intval($row['id'])]=$row;
- $return[]=intval($row['id']);
- }
-
- $this->LIST[$name][$table][$condition][$order][$limit]=$return;
-
- return $return;
- }
-
- private function prepare_select ($name, $condition=false, $order=false) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
- $item_model=$this->DM[$name];
-
- $sql='SELECT * FROM `' . $name . '`';
- if ($condition!==false) $sql.=' WHERE ' . $condition;
-
- if ($order!==false) $sql.=" ORDER BY " . $this->prepare_order($name, $order);
-
- return $sql;
- }
-
- private function prepare_join_select ($name, $table, $condition=false, $order=false) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
- $item_model=$this->DM[$name];
-
- $sql='SELECT `' . $name . '`.* FROM `' . $name . '`, `' . $table . '`';
- if ($condition!==false) $sql.=' WHERE ' . $condition;
-
- if ($order!==false) $sql.=" ORDER BY " . $this->prepare_order($name, $order);
-
- return $sql;
- }
-
- private function prepare_order ($name, $order) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
- $item_model=$this->DM[$name];
-
- foreach (explode(',', $order) as $p) {
- $p=trim(array_shift(explode(' ', trim($p))));
- if (isset($item_model['fields'][$p])) {continue;}
- foreach ($item_model['has_many'] as $has_many) {
- if ($p!=$has_many['local_name']) {continue;}
- $order=strtr($order, array($p=>"(SELECT COUNT(id) FROM `". $has_many['type'] . "` WHERE `" . $has_many['foreign_name'] . "`=" . $name . ".id)"));
- continue 2;
- }
- foreach ($item_model['many_to_many'] as $m2m) {
- if ($p!=$m2m['local_name']) {continue;}
- $order=strtr($order, array($p=>"(SELECT COUNT(*) FROM `". $m2m['relation_name'] . "` WHERE `" . $m2m['foreign_name'] . "`=" . $name . ".id)"));
- continue 2;
- }
- foreach ($item_model['self_ref'] as $self_ref) {
- if ($p!=$self_ref) {continue;}
- $order=strtr($order, array($p=>"(SELECT COUNT(*) FROM `". $self_ref . "` WHERE `" . $name . "1`=" . $name . ".id OR `" . $name . "2`=" . $name . ".id)"));
- continue 2;
- }
- }
-
- return $order;
- }
-
- private function get_initial_item () {
- static $list=array(
- 'conf'=>array(),
- 'has_many'=>array(),
- 'many_to_many'=>array(),
- 'self_ref'=>array(),
- 'fields'=>array()
- );
- return $list;
- }
-
- private function get_initial_config () {
- static $list=array(
- 'len'=>5,
- 'ttl'=>3600,
- );
- return $list;
- }
-
- private function get_initial_field_data ($field) {
- static $list=array(
- 'type'=>'text',
- 'unique'=>false,
- 'index'=>false,
- 'foreign'=>false
- );
-
- static $list_for_type=array(
- 'text'=>array(
- 'len'=>100
- ),
- 'numeric'=>array(
- 'len'=>4,
- 'decimal'=>0,
- 'signed'=>false,
- ),
- 'pass'=>array(
- 'len'=>40,
- 'filter'=>'sha1'
- ),
- );
- $field=$field+$list;
- return $field + $list_for_type[$field['type']];
- }
+<?php
+
+ require_once('cache.php');
+ require_once('db.php');
+ require_once('ddl.php');
+ require_once('item.php');
+
+ class ADB {
+ private static $instance;
+ public $ROW, $COUNT, $LIST;
+ public $db, $cache;
+ private $DM;
+
+ function __construct ($DM) {
+ $this->ROW=$this->LIST=$this->COUNT=array();
+ $this->db=DB::get_instance();
+ $this->cache=CACHE::get_instance();
+
+ $cached_dm=$this->cache->get('__DATA_MODEL__');
+ $cached_hash=$this->cache->get('__DATA_MODEL_HASH__');
+ if ($cached_dm!==false && $cached_hash!==false && $cached_hash==md5(serialize($DM))) {
+ $this->DM=$cached_dm;
+ return;
+ }
+
+ $this->DM=$DM;
+
+ foreach ($DM as $name=>$item) {
+ $item=$this->DM[$name]+$this->get_initial_item();
+ $item['conf']+=$this->get_initial_config();
+
+ foreach ($item['fields'] as $f_name=>$field) {
+ $field+=$this->get_initial_field_data($field);
+ $item['fields'][$f_name]=$field;
+ }
+
+ foreach ($item['has_many'] as $local_name=>$has_many) {
+ $has_many['local_name']=is_string($local_name) ? $local_name : $has_many['type'];
+ if (!(isset($has_many['foreign_name']))) $has_many['foreign_name']=$name;
+ $this->DM[$has_many['type']]['fields'][$has_many['foreign_name']]=array(
+ 'type'=>'numeric',
+ 'len'=>$item['conf']['len'],
+ 'foreign'=>array('type'=>$name, 'field'=>$has_many['local_name']),
+ 'index'=>true
+ ) + $this->get_initial_field_data(array('type'=>'numeric'));
+ $item['has_many'][$local_name]=$has_many;
+ }
+
+ foreach ($item['many_to_many'] as $local_name=>$m2m) {
+ if (isset($this->DM[$name]['many_to_many'][$local_name]['done'])) continue;
+ $m2m['local_name']=is_string($local_name) ? $local_name : $m2m['type'];
+ if (!isset($m2m['foreign_name'])) $m2m['foreign_name']=$name;
+ if (!isset($m2m['relation_name'])) {
+ $relation_name=array($m2m['foreign_name'], $m2m['local_name']);
+ sort($relation_name);
+ $m2m['relation_name']=implode('_', $relation_name);
+ }
+ $this->DM[$m2m['type']]['many_to_many'][$m2m['foreign_name']]=array(
+ 'done'=>true,
+ 'type'=>$name,
+ 'local_name'=>$m2m['foreign_name'],
+ 'foreign_name'=>$m2m['local_name'],
+ 'relation_name'=>$m2m['relation_name']
+ );
+ $item['many_to_many'][$local_name]=$m2m;
+ }
+
+ $this->DM[$name]=$item;
+ }
+
+ $this->cache->set('__DATA_MODEL_HASH__', md5(serialize($DM)));
+ $this->cache->set('__DATA_MODEL__', $this->DM);
+ }
+
+ static function init ($DM) {
+ self::$instance=new ADB ($DM);
+ }
+
+ static function get_instance () {
+ if (!isset(self::$instance))
+ throw new Exception('You have to initialize this class before using');
+ return self::$instance;
+ }
+
+ function create_tables () {
+ $this->cache->flush();
+ $ddl=new DDL($this->DM);
+ $ddl->create_tables();
+ }
+
+ function load ($name, $id) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ if (isset($this->ROW[$name][$id]))
+ return new ITEM($name, $this->DM[$name], $id, $this->ROW[$name][$id]);
+ else
+ return new ITEM($name, $this->DM[$name], $id);
+ }
+
+ function create ($name, $data) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ $item_model=$this->DM[$name];
+
+ $insert=$foreigns=array();
+ foreach ($data as $k=>$v) {
+ if (!isset($item_model['fields'][$k])) {continue;}
+ $field=$item_model['fields'][$k];
+ if (isset($field['filter']) && function_exists($field['filter'])) {$v=eval('return ' . $field['filter'] . '($v);');}
+ $insert[$k]=$v;
+ if ($field['foreign']!==false) {
+ $field['foreign']['id']=$v;
+ $foreigns[]=$field['foreign'];
+ }
+ }
+ $insert['create_date']=$_SERVER['REQUEST_TIME'];
+
+ $id=$this->db->insert($name, $insert);
+
+ foreach ($foreigns as $foreign) {
+ $foreign_item=$this->load($foreign['type'], intval($foreign['id']));
+ $foreign_item->add_relation($foreign['field'], $id);
+ }
+
+ unset($this->LIST[$name], $this->COUNT[$name]);
+
+ return $id;
+ }
+
+ function delete ($name, $id, $delete_belongings=false) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ $item_model=$this->DM[$name];
+
+ $item=$this->load($name, $id);
+
+ $this->cache->delete('item_' . $name . '_' . $id);
+
+ $this->db->delete($name, "id='" . $id . "'");
+
+ foreach (array_filter($item_model['fields'], function ($el) {return $el['foreign']!==false;}) as $k=>$f) {
+ $foreign_item=$this->load($f['foreign']['type'], intval($item[$k]));
+ $foreign_item->delete_relation($f['foreign']['field'], $id);
+ }
+ foreach ($item_model['has_many'] as $has_many) {
+ foreach ($item[$has_many['local_name']] as $foreign_id) {
+ if ($delete_belongings) {
+ $this->delete($has_many['type'], $foreign_id, $delete_belongings);
+ } else {
+ $foreign_item=$this->load($has_many['type'], $foreign_id);
+ $foreign_item[$has_many['foreign_name']]=0;
+ }
+ }
+ }
+ foreach ($item_model['many_to_many'] as $m2m) {
+ foreach ($item[$m2m['local_name']] as $foreign_id) {
+ $this->unrelate($name, $m2m['local_name'], $id, $foreign_id);
+ }
+ }
+ foreach ($item_model['self_ref'] as $self_ref) {
+ foreach ($item[$self_ref] as $self_ref_id) {
+ $this->self_unrelate($name, $self_ref, $id, $self_ref_id);
+ }
+ }
+
+ unset($this->LIST[$name], $this->COUNT[$name]);
+
+ unset($this->ITEM[$name][$id]);
+ unset($this->ROW[$name][$id]);
+
+ $item->delete();
+ }
+
+ function relate ($name, $local_name, $id1, $id2) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ $m2m=array_shift(array_filter($this->DM[$name]['many_to_many'], function ($m2m) use ($local_name) {return $m2m['local_name']==$local_name;}));
+ if (empty($m2m)) return $this->self_relate($name, $local_name, $id1, $id2);
+
+ $item1=$this->load($name, $id1);
+ $item2=$this->load($m2m['type'], $id2);
+
+ $list=$item1[$local_name];
+ if (in_array($id2, $list)) {return;}
+
+ $insert=array($m2m['foreign_name']=>$id1, $local_name=>$id2);
+ $this->db->insert($m2m['relation_name'], $insert);
+
+ $item1->add_relation($local_name, $id2);
+ $item2->add_relation($m2m['foreign_name'], $id1);
+ }
+
+ function unrelate ($name, $local_name, $id1, $id2) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ $m2m=array_shift(array_filter($this->DM[$name]['many_to_many'], function ($m2m) use ($local_name) {return $m2m['local_name']==$local_name;}));
+ if (empty($m2m)) return $this->self_unrelate($name, $local_name, $id1, $id2);
+
+ $item1=$this->load($name, $id1);
+ $item2=$this->load($m2m['type'], $id2);
+
+ $condition="`" . $m2m['foreign_name'] . "`='" . $id1 . "' AND `" . $local_name . "`='" . $id2 . "'";
+ $this->db->delete($m2m['relation_name'], $condition);
+
+ $item1->delete_relation($local_name, $id2);
+ $item2->delete_relation($m2m['foreign_name'], $id1);
+ }
+
+ private function self_relate ($name, $local_name, $id1, $id2) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ if (!(in_array($local_name, $this->DM[$name]['self_ref'])))
+ throw new Exception('No defined relation to relate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
+
+ $item1=$this->load($name, $id1);
+ $item2=$this->load($name, $id2);
+
+ $list=$item1[$local_name];
+ if (in_array($id2, $list)) {return;}
+
+ $insert=array($name . '1'=>$id1, $name . '2'=>$id2);
+ $this->db->insert($local_name, $insert);
+
+ $item1->add_relation($local_name, $id2);
+ $item2->add_relation($local_name, $id1);
+ }
+
+ private function self_unrelate ($name, $local_name, $id1, $id2) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ if (!(in_array($local_name, $this->DM[$name]['self_ref'])))
+ throw new Exception('No defined relation to unrelate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
+
+ $condition="(`" . $name . "1`='" . $id1 . "' AND `" . $name . "2`='" . $id2 . "') OR (`" . $name . "1`='" . $id2 . "' AND `" . $name . "2`='" . $id1 . "')";
+ $this->db->delete($local_name, $condition);
+
+ $item1=$this->load($name, $id1);
+ $item2=$this->load($name, $id2);
+
+ $item1->delete_relation($local_name, $id2);
+ $item2->delete_relation($local_name, $id1);
+ }
+
+ function find_unique ($name, $field, $value) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+
+ if (!isset($this->DM[$name]['fields'][$field]))
+ throw new Exception('No field as ' . $field . ' found for item ' . $name);
+
+ $sql="SELECT * FROM `" . $name . "` WHERE `" . $field . "`='" . $this->db->escape($value) . "'";
+ $result=$this->db->select($sql);
+ if (count($result)) {
+ $this->ROW[$name][$result[0]['id']]=$result[0];
+ return $this->load($name, $result[0]['id']);
+ }
+
+ return false;
+ }
+
+ function count ($name, $condition=false, $order=false) {
+ if (isset($this->COUNT[$name][$condition])) return $this->COUNT[$name][$condition];
+
+ $sql=$this->prepare_select($name, $condition, $order);
+
+ $this->COUNT[$name][$condition]=$this->db->count($sql);
+
+ return $this->COUNT[$name][$condition];
+ }
+
+ function id_list ($name, $condition=false, $order=false, $limit=false) {
+ if (isset($this->LIST[$name][$condition][$order][$limit])) return $this->LIST[$name][$condition][$order][$limit];
+
+ $sql=$this->prepare_select($name, $condition, $order);
+
+ if ($limit!==false) {$sql.=" LIMIT " . $limit;}
+
+ $return=$this->LIST[$name][$condition][$order][$limit]=array();
+
+ foreach ($this->db->select($sql) as $row) {
+ $this->ROW[$name][intval($row['id'])]=$row;
+ $return[]=intval($row['id']);
+ }
+
+ $this->LIST[$name][$condition][$order][$limit]=$return;
+
+ return $return;
+ }
+
+ function count_join ($name, $table, $condition, $order) {
+ if (isset($this->COUNT[$name][$table][$condition])) return $this->COUNT[$name][$table][$condition];
+
+ $sql=$this->prepare_join_select($name, $table, $condition, $order);
+
+ $this->COUNT[$name][$table][$condition]=$this->db->count($sql);
+
+ return $this->COUNT[$name][$table][$condition];
+ }
+
+ function id_list_join ($name, $table, $condition=false, $order=false, $limit=false) {
+ if (isset($this->LIST[$name][$table][$condition][$order][$limit])) return $this->LIST[$name][$table][$condition][$order][$limit];
+
+ $sql=$this->prepare_join_select($name, $table, $condition, $order);
+
+ if ($limit!==false) {$sql.=" LIMIT " . $limit;}
+
+ $return=$this->LIST[$name][$table][$condition][$order][$limit]=array();
+
+ foreach ($this->db->select($sql) as $row) {
+ $this->ROW[$name][intval($row['id'])]=$row;
+ $return[]=intval($row['id']);
+ }
+
+ $this->LIST[$name][$table][$condition][$order][$limit]=$return;
+
+ return $return;
+ }
+
+ private function prepare_select ($name, $condition=false, $order=false) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ $item_model=$this->DM[$name];
+
+ $sql='SELECT * FROM `' . $name . '`';
+ if ($condition!==false) $sql.=' WHERE ' . $condition;
+
+ if ($order!==false) $sql.=" ORDER BY " . $this->prepare_order($name, $order);
+
+ return $sql;
+ }
+
+ private function prepare_join_select ($name, $table, $condition=false, $order=false) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ $item_model=$this->DM[$name];
+
+ $sql='SELECT `' . $name . '`.* FROM `' . $name . '`, `' . $table . '`';
+ if ($condition!==false) $sql.=' WHERE ' . $condition;
+
+ if ($order!==false) $sql.=" ORDER BY " . $this->prepare_order($name, $order);
+
+ return $sql;
+ }
+
+ private function prepare_order ($name, $order) {
+ if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ $item_model=$this->DM[$name];
+
+ foreach (explode(',', $order) as $p) {
+ $p=trim(array_shift(explode(' ', trim($p))));
+ if (isset($item_model['fields'][$p])) {continue;}
+ foreach ($item_model['has_many'] as $has_many) {
+ if ($p!=$has_many['local_name']) {continue;}
+ $order=strtr($order, array($p=>"(SELECT COUNT(id) FROM `". $has_many['type'] . "` WHERE `" . $has_many['foreign_name'] . "`=" . $name . ".id)"));
+ continue 2;
+ }
+ foreach ($item_model['many_to_many'] as $m2m) {
+ if ($p!=$m2m['local_name']) {continue;}
+ $order=strtr($order, array($p=>"(SELECT COUNT(*) FROM `". $m2m['relation_name'] . "` WHERE `" . $m2m['foreign_name'] . "`=" . $name . ".id)"));
+ continue 2;
+ }
+ foreach ($item_model['self_ref'] as $self_ref) {
+ if ($p!=$self_ref) {continue;}
+ $order=strtr($order, array($p=>"(SELECT COUNT(*) FROM `". $self_ref . "` WHERE `" . $name . "1`=" . $name . ".id OR `" . $name . "2`=" . $name . ".id)"));
+ continue 2;
+ }
+ }
+
+ return $order;
+ }
+
+ private function get_initial_item () {
+ static $list=array(
+ 'conf'=>array(),
+ 'has_many'=>array(),
+ 'many_to_many'=>array(),
+ 'self_ref'=>array(),
+ 'fields'=>array()
+ );
+ return $list;
+ }
+
+ private function get_initial_config () {
+ static $list=array(
+ 'len'=>5,
+ 'ttl'=>3600,
+ );
+ return $list;
+ }
+
+ private function get_initial_field_data ($field) {
+ static $list=array(
+ 'type'=>'text',
+ 'unique'=>false,
+ 'index'=>false,
+ 'foreign'=>false
+ );
+
+ static $list_for_type=array(
+ 'text'=>array(
+ 'len'=>100
+ ),
+ 'numeric'=>array(
+ 'len'=>4,
+ 'decimal'=>0,
+ 'signed'=>false,
+ ),
+ 'pass'=>array(
+ 'len'=>40,
+ 'filter'=>'sha1'
+ ),
+ );
+ $field=$field+$list;
+ return $field + $list_for_type[$field['type']];
+ }
}
View
470 lib/item.php
@@ -1,236 +1,236 @@
-<?php
-
- class ITEM implements arrayaccess {
- private static $ITEM;
- private $name, $model, $id, $data;
- private $db, $adb, $cache;
-
- function __construct ($name, $model, $id, $row=false) {
- $this->db=DB::get_instance();
- $this->adb=ADB::get_instance();
- $this->cache=CACHE::get_instance();
-
- $this->name=$name;
- $this->model=$model;
- $this->id=$id;
-
- if (isset(self::$ITEM[$name][$id])) {
- $this->data=self::$ITEM[$name][$id];
- return;
- }
-
- if ($data=$this->cache->get('item_' . $this->name . '_' . $this->id)) {
- $this->data=$data;
- return;
- }
-
- if ($row!==false) {
- $this->data=$row;
- } else {
- $sql="SELECT * FROM `" . $this->name . "` WHERE id='" . $this->id . "'";
- $result=$this->db->select($sql);
- if (!count($result))
- throw new Exception('No ' . $this->name . ' found with id ' . $this->id);
-
- $this->data=$result[0];
- }
-
- foreach ($this->model['has_many'] as $has_many)
- $this->data[$has_many['local_name']]=$this->adb->id_list($has_many['type'], $has_many['foreign_name'] . "='" . $this->id . "'");
-
- foreach ($this->model['many_to_many'] as $m2m) {
- $sql="SELECT `" . $m2m['local_name'] . "` FROM `" . $m2m['relation_name'] . "` WHERE `" . $m2m['foreign_name'] . "`='" . $id . "'";
- $this->data[$m2m['local_name']]=array();
- foreach ($this->db->select($sql) as $row) {
- $this->data[$m2m['local_name']][]=intval($row[$m2m['local_name']]);
- }
- }
- foreach ($this->model['self_ref'] as $self_ref) {
- $sql="(SELECT `" . $this->name . "1` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "2`='" . $this->id . "')";
- $sql.="UNION (SELECT `" . $this->name . "2` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "1`='" . $this->id . "')";
- $this->data[$self_ref]=array();
- foreach ($this->db->select($sql) as $row) {
- $this->data[$self_ref][]=intval($row['id']);
- }
- }
-
- $this->save();
- }
-
-
- /** OFFSET FONKSİYONLARI **/
-
- function offsetexists ($field) {return isset($this->data[$field]);}
-
- function offsetget ($field) {
- if (!isset($this->data[$field]))
- throw new Exception('No field found as ' . $field);
-
- return $this->data[$field];
- }
-
- function offsetset ($field, $value) {
- if (!(isset($this->model['fields'][$field])))
- throw new Exception('No field found as ' . $field);
-
- $field_model=$this->model['fields'][$field];
- if (isset($field_model['filter']) && function_exists($field_model['filter'])) {$value=eval('return ' . $field_model['filter'] . '($value);');}
-
- $update[$field]=$value;
- $update['update_date']=$_SERVER['REQUEST_TIME'];
-
- $this->db->update($this->name, $update, "id='" . $this->id . "'");
-
- if ($field_model['foreign']!==false) {
- $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($this->data[$field]));
- $foreign_item->delete_relation($field_model['foreign']['field'], $this->id);
-
- if ($value!=0) {
- $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($value));
- $foreign_item->add_relation($field_model['foreign']['field'], $this->id);
- }
- }
- $this->data[$field]=$value;
- $this->data['update_date']=$_SERVER['REQUEST_TIME'];
-
- unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
- $this->save();
- }
-
- function offsetunset ($field) {
- throw new Exception('Field unset is not allowed.');
- }
-
- function add_relation ($field, $id) {
- $id=intval($id);
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- if (in_array($id, $this->data[$field])) return false;
-
- $this->data[$field][]=$id;
- $this->save();
- }
-
- function delete_relation ($field, $id) {
- $id=intval($id);
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- if (!in_array($id, $this->data[$field])) return false;
-
- unset($this->data[$field][array_search($id, $this->data[$field])]);
- $this->save();
- }
-
- function update ($data) {
- $update=$foreigns=array();
-
- foreach ($data as $k=>$v) {
- if (!isset($this->model['fields'][$k])) {continue;}
-
- $field=$item_model['fields'][$k];
- if (isset($field['filter']) && function_exists($field['filter'])) {$v=eval('return ' . $field['filter'] . '($v);');}
-
- $update[$k]=$v;
-
- if ($field['foreign']!==false) {
- $field['foreign']['id']=$v;
- $foreigns[$k]=$field['foreign'];
- }
- }
- $update['update_date']=$_SERVER['REQUEST_TIME'];
-
- $this->db->update($this->name, $update, "id='" . $this->id . "'");
-
- foreach ($foreigns as $k=>$foreign) {
- $foreign_item=$this->load($foreign['type'], intval($this->data[$k]));
- $foreign_item->delete_relation($foreign['field'], $this->id);
-
- $foreign_item=$this->load($foreign['type'], intval($foreign['id']));
- $foreign_item->add_relation($foreign['field'], $this->id);
- }
-
- $this->data=$update + $this->data;
-
- unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
- $this->save();
- }
-
- function count ($field, $condition=false, $order=false) {
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- $sql=array();
-
- foreach ($this->model['has_many'] as $has_many) {
- if ($field!=$has_many['local_name']) {continue;}
- $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
- return $this->adb->count($has_many['type'], implode(' AND ', $sql), $order);
- }
-
- foreach ($this->model['many_to_many'] as $m2m) {
- if ($field!=$m2m['local_name']) {continue;}
-
- $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
- $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->count_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order);
- }
-
- foreach ($this->model['self_ref'] as $self_ref) {
- if ($field!=$self_ref) {continue;}
-
- $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
- $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->count_join($this->name, $self_ref, implode(' AND ', $sql), $order);
- }
- }
-
- function id_list ($field, $condition=false, $order=false, $limit=false) {
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- $sql=array();
-
- foreach ($this->model['has_many'] as $has_many) {
- if ($field!=$has_many['local_name']) {continue;}
- $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
- return $this->adb->id_list($has_many['type'], implode(' AND ', $sql), $order, $limit);
- }
-
- foreach ($this->model['many_to_many'] as $m2m) {
- if ($field!=$m2m['local_name']) {continue;}
-
- $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
- $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->id_list_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order, $limit);
- }
-
- foreach ($this->model['self_ref'] as $self_ref) {
- if ($field!=$self_ref) {continue;}
-
- $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
- $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->id_list_join($this->name, $self_ref, implode(' AND ', $sql), $order, $limit);
- }
- }
-
- private function save () {
- $this->cache->set('item_' . $this->name . '_' . $this->id, $this->data,$this->model['conf']['ttl']);
- self::$ITEM[$this->name][$this->id]=$this->data;
- }
-
- public function delete () {
- unset(self::$ITEM[$this->name][$this->id]);
- }
+<?php
+
+ class ITEM implements arrayaccess {
+ private static $ITEM;
+ private $name, $model, $id, $data;
+ private $db, $adb, $cache;
+
+ function __construct ($name, $model, $id, $row=false) {
+ $this->db=DB::get_instance();
+ $this->adb=ADB::get_instance();
+ $this->cache=CACHE::get_instance();
+
+ $this->name=$name;
+ $this->model=$model;
+ $this->id=$id;
+
+ if (isset(self::$ITEM[$name][$id])) {
+ $this->data=self::$ITEM[$name][$id];
+ return;
+ }
+
+ if ($data=$this->cache->get('item_' . $this->name . '_' . $this->id)) {
+ $this->data=$data;
+ return;
+ }
+
+ if ($row!==false) {
+ $this->data=$row;
+ } else {
+ $sql="SELECT * FROM `" . $this->name . "` WHERE id='" . $this->id . "'";
+ $result=$this->db->select($sql);
+ if (!count($result))
+ throw new Exception('No ' . $this->name . ' found with id ' . $this->id);
+
+ $this->data=$result[0];
+ }
+
+ foreach ($this->model['has_many'] as $has_many)
+ $this->data[$has_many['local_name']]=$this->adb->id_list($has_many['type'], $has_many['foreign_name'] . "='" . $this->id . "'");
+
+ foreach ($this->model['many_to_many'] as $m2m) {
+ $sql="SELECT `" . $m2m['local_name'] . "` FROM `" . $m2m['relation_name'] . "` WHERE `" . $m2m['foreign_name'] . "`='" . $id . "'";
+ $this->data[$m2m['local_name']]=array();
+ foreach ($this->db->select($sql) as $row) {
+ $this->data[$m2m['local_name']][]=intval($row[$m2m['local_name']]);
+ }
+ }
+ foreach ($this->model['self_ref'] as $self_ref) {
+ $sql="(SELECT `" . $this->name . "1` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "2`='" . $this->id . "')";
+ $sql.="UNION (SELECT `" . $this->name . "2` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "1`='" . $this->id . "')";
+ $this->data[$self_ref]=array();
+ foreach ($this->db->select($sql) as $row) {
+ $this->data[$self_ref][]=intval($row['id']);
+ }
+ }
+
+ $this->save();
+ }
+
+
+ /** OFFSET FONKSİYONLARI **/
+
+ function offsetexists ($field) {return isset($this->data[$field]);}
+
+ function offsetget ($field) {
+ if (!isset($this->data[$field]))
+ throw new Exception('No field found as ' . $field);
+
+ return $this->data[$field];
+ }
+
+ function offsetset ($field, $value) {
+ if (!(isset($this->model['fields'][$field])))
+ throw new Exception('No field found as ' . $field);
+
+ $field_model=$this->model['fields'][$field];
+ if (isset($field_model['filter']) && function_exists($field_model['filter'])) {$value=eval('return ' . $field_model['filter'] . '($value);');}
+
+ $update[$field]=$value;
+ $update['update_date']=$_SERVER['REQUEST_TIME'];
+
+ $this->db->update($this->name, $update, "id='" . $this->id . "'");
+
+ if ($field_model['foreign']!==false) {
+ $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($this->data[$field]));
+ $foreign_item->delete_relation($field_model['foreign']['field'], $this->id);
+
+ if ($value!=0) {
+ $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($value));
+ $foreign_item->add_relation($field_model['foreign']['field'], $this->id);
+ }
+ }
+ $this->data[$field]=$value;
+ $this->data['update_date']=$_SERVER['REQUEST_TIME'];
+
+ unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
+ $this->save();
+ }
+
+ function offsetunset ($field) {
+ throw new Exception('Field unset is not allowed.');
+ }
+
+ function add_relation ($field, $id) {
+ $id=intval($id);
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new Exception($field . ' is not a relation field');
+
+ if (in_array($id, $this->data[$field])) return false;
+
+ $this->data[$field][]=$id;
+ $this->save();
+ }
+
+ function delete_relation ($field, $id) {
+ $id=intval($id);
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new Exception($field . ' is not a relation field');
+
+ if (!in_array($id, $this->data[$field])) return false;
+
+ unset($this->data[$field][array_search($id, $this->data[$field])]);
+ $this->save();
+ }
+
+ function update ($data) {
+ $update=$foreigns=array();
+
+ foreach ($data as $k=>$v) {
+ if (!isset($this->model['fields'][$k])) {continue;}
+
+ $field=$item_model['fields'][$k];
+ if (isset($field['filter']) && function_exists($field['filter'])) {$v=eval('return ' . $field['filter'] . '($v);');}
+
+ $update[$k]=$v;
+
+ if ($field['foreign']!==false) {
+ $field['foreign']['id']=$v;
+ $foreigns[$k]=$field['foreign'];
+ }
+ }
+ $update['update_date']=$_SERVER['REQUEST_TIME'];
+
+ $this->db->update($this->name, $update, "id='" . $this->id . "'");
+
+ foreach ($foreigns as $k=>$foreign) {
+ $foreign_item=$this->load($foreign['type'], intval($this->data[$k]));
+ $foreign_item->delete_relation($foreign['field'], $this->id);
+
+ $foreign_item=$this->load($foreign['type'], intval($foreign['id']));
+ $foreign_item->add_relation($foreign['field'], $this->id);
+ }
+
+ $this->data=$update + $this->data;
+
+ unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
+ $this->save();
+ }
+
+ function count ($field, $condition=false, $order=false) {
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new Exception($field . ' is not a relation field');
+
+ $sql=array();
+
+ foreach ($this->model['has_many'] as $has_many) {
+ if ($field!=$has_many['local_name']) {continue;}
+ $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+ return $this->adb->count($has_many['type'], implode(' AND ', $sql), $order);
+ }
+
+ foreach ($this->model['many_to_many'] as $m2m) {
+ if ($field!=$m2m['local_name']) {continue;}
+
+ $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
+ $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->count_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order);
+ }
+
+ foreach ($this->model['self_ref'] as $self_ref) {
+ if ($field!=$self_ref) {continue;}
+
+ $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
+ $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->count_join($this->name, $self_ref, implode(' AND ', $sql), $order);
+ }
+ }
+
+ function id_list ($field, $condition=false, $order=false, $limit=false) {
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new Exception($field . ' is not a relation field');
+
+ $sql=array();
+
+ foreach ($this->model['has_many'] as $has_many) {
+ if ($field!=$has_many['local_name']) {continue;}
+ $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+ return $this->adb->id_list($has_many['type'], implode(' AND ', $sql), $order, $limit);
+ }
+
+ foreach ($this->model['many_to_many'] as $m2m) {
+ if ($field!=$m2m['local_name']) {continue;}
+
+ $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
+ $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->id_list_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order, $limit);
+ }
+
+ foreach ($this->model['self_ref'] as $self_ref) {
+ if ($field!=$self_ref) {continue;}
+
+ $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
+ $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->id_list_join($this->name, $self_ref, implode(' AND ', $sql), $order, $limit);
+ }
+ }
+
+ private function save () {
+ $this->cache->set('item_' . $this->name . '_' . $this->id, $this->data,$this->model['conf']['ttl']);
+ self::$ITEM[$this->name][$this->id]=$this->data;
+ }
+
+ public function delete () {
+ unset(self::$ITEM[$this->name][$this->id]);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.