1: <?php
2:
3: namespace Alchemy\orm;
4: use Alchemy\core\query\Expression;
5: use Alchemy\core\query\Predicate;
6: use Alchemy\core\query\Query;
7: use Alchemy\core\query\TableRef;
8:
9:
10: 11: 12:
13: class ORMQuery extends Query {
14:
15: protected $joinedTables = array();
16:
17: public function __construct($type, TableRef $table) {
18: parent::__construct($type, $table);
19:
20: $this->joinedTables[$table->getID()] = $table;
21: if ($table instanceof ORMTableRef && $table->predicate()) {
22: $this->joins($table->predicate()->tables());
23: $this->where = $table->predicate();
24: }
25:
26: $this->columns($table->columns());
27: }
28:
29:
30: public function __set($name, $value) {
31: if ($value instanceof Expression) {
32: $this->joins($value->tables());
33: }
34:
35: parent::__set($name, $value);
36: }
37:
38:
39: public function join($table, Predicate $on = null, $direction = null, $type = null) {
40: if (array_key_exists($table->getID(), $this->joinedTables)) {
41: return $this;
42: }
43:
44: $this->joinedTables[$table->getID()] = $table;
45: if ($on) {
46: $this->joins($on->tables());
47: }
48:
49: return parent::join($table, $on, $direction, $type);
50: }
51:
52:
53: public function joins($args = false) {
54: if ($args === false) {
55: return $this->joins;
56: }
57:
58: foreach ($args as $table) {
59: $on = $table instanceof ORMTableRef ? $table->predicate() : null;
60: $this->join($table, $on);
61: }
62: }
63:
64:
65: public function where($expr = false) {
66: if ($expr === false) {
67: return $this->where;
68: }
69:
70: $this->where = Predicate::ALL($this->where, func_get_args());
71: $this->joins($this->where->tables());
72:
73: return $this;
74: }
75:
76:
77: public function with() {
78: $this->joins(func_get_args());
79:
80:
81: }
82:
83:
84: public function without() {
85:
86: }
87:
88: }