Subversion Repositories PHPX

Rev

Rev 27 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27 Rev 29
1
<?php
1
<?php
2
2
3
require_once 'lib/Model.php';
3
require_once __DIR__ . '/../Model.php';
4
4
5
abstract class Adapter
5
abstract class Adapter
6
{
6
{
7
  /**
7
  /**
8
   * Database used by the adapter
8
   * Database used by the adapter
9
   * @var Database
9
   * @var Database
10
   */
10
   */
11
  protected $_database = null;
11
  protected $_database = null;
12
12
13
  /**
13
  /**
14
   * Constructs the adapter, associating a {@link Database} with it
14
   * Constructs the adapter, associating a {@link Database} with it
15
   * @param Database $database
15
   * @param Database $database
16
   */
16
   */
17
  /* Singleton */
17
  /* Singleton */
18
  protected function __construct(Database $database)
18
  protected function __construct(Database $database)
19
  {
19
  {
20
    $this->_database = $database;
20
    $this->_database = $database;
21
  }
21
  }
22
 
22
 
23
  /**
23
  /**
24
  * Selects data from one or more tables
24
  * Selects data from one or more tables
25
  *
25
  *
26
  * @return array
26
  * @return array
27
  * @see Database::select()
27
  * @see Database::select()
28
  */
28
  */
29
  public function select($table, $columns = null, $where = null, $order = null, $limit = null)
29
  public function select($table, $columns = null, $where = null, $order = null, $limit = null)
30
  {
30
  {
31
    return $this->_database->select($table, $columns, $where, $order, $limit);
31
    return $this->_database->select($table, $columns, $where, $order, $limit);
32
  }
32
  }
33
 
33
 
34
  /**
34
  /**
35
   * Finds all records matching the set properties of a model object
35
   * Finds all records matching the set properties of a model object
36
   *
36
   *
37
   * @param Model $object
37
   * @param Model $object
38
   * @return array[Model]
38
   * @return array[Model]
39
   */
39
   */
40
  public function findAll(Model $object, $order = null, $limit = null)
40
  public function findAll(Model $object, $order = null, $limit = null)
41
  {
41
  {
42
    $properties = $object->getPropertyVars();
42
    $properties = $object->getPropertyVars();
43
    $where = array();
43
    $where = array();
44
   
44
   
45
    foreach ($properties as $property)
45
    foreach ($properties as $property)
46
    {
46
    {
47
      if (!is_null($object->$property))
47
      if (!is_null($object->$property))
48
      {
48
      {
49
        $where[$property] = $object->$property;
49
        $where[$property] = $object->$property;
50
      }
50
      }
51
    }
51
    }
52
   
52
   
53
    $class = get_class($object);
53
    $class = get_class($object);
54
    $query_result = $this->select($class::persistentTable, null, $where, $order, $limit);
54
    $query_result = $this->select($class::persistentTable, null, $where, $order, $limit);
55
   
55
   
56
    $num_results = count($query_result);
56
    $num_results = count($query_result);
57
    if ($num_results === 0)
57
    if ($num_results === 0)
58
    {
58
    {
59
      return null;
59
      return null;
60
    }
60
    }
61
61
62
    $result = array();
62
    $result = array();
63
   
63
   
64
    foreach ($query_result as $row)
64
    foreach ($query_result as $row)
65
    {
65
    {
66
      $result[] = new $class($row);
66
      $result[] = new $class($row);
67
    }
67
    }
68
68
69
    return $result;
69
    return $result;
70
  }
70
  }
71
 
71
 
72
  /**
72
  /**
73
   * Finds the record for a model object by its primary key
73
   * Finds the record for a model object by its primary key
74
   *
74
   *
75
   * @param Model $object
75
   * @param Model $object
76
   * @return Model
76
   * @return Model
77
   *   The filled object if the primary key value could be found,
77
   *   The filled object if the primary key value could be found,
78
   *   <code>null</code> otherwise
78
   *   <code>null</code> otherwise
79
   */
79
   */
80
  public function find(Model $object)
80
  public function find(Model $object)
81
  {
81
  {
82
    $class = get_class($object);
82
    $class = get_class($object);
83
    $primaryKey = $class::persistentPrimaryKey;
83
    $primaryKey = $class::persistentPrimaryKey;
84
   
84
   
85
    if (is_array($primaryKey))
85
    if (is_array($primaryKey))
86
    {
86
    {
87
      /* TODO */
87
      /* TODO */
88
    }
88
    }
89
89
90
    $result = $this->select($class::persistentTable, null, array($primaryKey => $object->$primaryKey));
90
    $result = $this->select($class::persistentTable, null, array($primaryKey => $object->$primaryKey));
91
    if (0 == count($result))
91
    if (0 == count($result))
92
    {
92
    {
93
      return null;
93
      return null;
94
    }
94
    }
95
   
95
   
96
    $row = $result[0];
96
    $row = $result[0];
97
    $object->map($row);
97
    $object->map($row);
98
   
98
   
99
    return $object;
99
    return $object;
100
  }
100
  }
101
}
101
}