Associations

has_many($options = array (), $table)

7: 	function has_many($options = array (), $table) {
11:
16:
17: 		$association_table = $table;
18:
19: 		$inflector = new Inflector;
20: 		if (isset ($options['class_name'])) {
21: 			$table = $inflector->tableize($options['class_name']);
22: 		}
23:
25: 		$class = ucwords($inflector->singularize($inflector->camelize($table)));
26: 		$object = new $class;
27:
28: 		if (!isset ($options['foreign_key'])) {
29: 			$options['foreign_key'] = $inflector->singularize($this->table) . '_id';
30: 		}
31:
32: 		if (!isset ($options['conditions'])) {
33: 			$options['conditions'] = '';
34: 		} else {
35: 			$options['conditions'] = $options['conditions'] . ' and ';
36: 		}
37:
38: 		if (isset ($options['through'])) {
39: 			$options['joins'] = Associations :: associate_through($options, $table);
40: 			$options['as'] = $this->has_many[$options['through']]['as'];
41: 			$table = $options['through'];
42: 		}
43:
44: 		if (isset ($options['as'])) {
45: 			$options['conditions'] .= Associations :: associate_as($options, $table, $inflector->singularize(ucwords($this->table)));
46: 		} else {
47: 			$options['conditions'] .= $table . ".`" . $options['foreign_key'] . "` = " . $this->id;
48: 		}
49:
51: 		if (isset ($options['finder_sql'])) {
52: 			if (isset ($options['counter_sql'])) {
53: 				$options['finder_sql'] = preg_replace('*', 'COUNT(*)', $options['finder_sql']);
54: 			}
55: 			$this-> $association_table = $object->find_by_sql($options['finder_sql']);
56: 		} else {
57: 			$options['type'] = 'array';
58: 			$this-> $association_table = $object->find($options);
59: 		}
60:
61: 		return $this-> $association_table;
62:
63: 	}

File: lib/model/Associations.php, #line 7

has_and_belongs_to_many($options = array (), $table)

67: 	function has_and_belongs_to_many($options = array (), $table) {
68:
74:
77: 		$association_table = $table;
78:
79: 		$inflector = new Inflector;
80: 		if (isset ($options['class_name'])) {
81: 			$table = $inflector->tableize($options['class_name']);
82: 		}
83:
85: 		$class = ucwords($inflector->singularize($inflector->camelize($table)));
86: 		$object = new $class;
87:
88: 		if (!isset ($options['foreign_key'])) {
89: 			$options['foreign_key'] = $inflector->singularize($this->table) . '_id';
90: 		}
91:
92: 		if (!isset ($options['association_foreign_key'])) {
93: 			$options['association_foreign_key'] = $inflector->singularize($association_table) . '_id';
94: 		}
95:
96: 		if (!isset ($options['conditions'])) {
97: 			$options['conditions'] = '';
98: 		} else {
99: 			$options['conditions'] = $options['conditions'] . ' and ';
100: 		}
101:
102: 		if (!isset ($optionsp['jons'])) {
103: 			$options['joins'] = '';
104: 		}
105:
106: 		if (!isset ($options['join_table'])) {
107: 			$options['join_table'] = Associations :: order_table_association($this->table, $association_table); // users, projects => projects_users
108: 		}
109:
110: 		$options['joins'] .= "inner join " . $options['join_table'] . " on " . $table . ".`id` = " . $options['join_table'] . ".`" . $options['association_foreign_key'] . "`";
111: 		$options['conditions'] .= $options['join_table'] . ".`" . $options['foreign_key'] . "` = " . $this->id;
112: 		$options['type'] = 'array';
113:
115: 		if (isset ($options['finder_sql'])) {
116: 			$this-> $association_table = $object->find_by_sql($options['finder_sql']);
117: 		} else {
118: 			$options['type'] = 'array';
119: 			$this-> $association_table = $object->find($options);
120: 		}
121:
122: 		return $this-> $association_table;
123:
124: 	}

File: lib/model/Associations.php, #line 67

has_one($options, $table)

129: 	function has_one($options, $table) {
131:
134: 		$association_table = $table;
135:
136: 		$inflector = new Inflector;
137: 		if (isset ($options['class_name'])) {
138: 			$table = $inflector->tableize($options['class_name']);
140: 			$class = ucwords($inflector->singularize($inflector->camelize($table)));
141: 		} else {
142: 			$class = ucwords($table);
143: 			$table = $inflector->pluralize($table);
144: 		}
145:
146: 		$object = new $class;
147:
148: 		if (!isset ($options['foreign_key'])) {
149: 			$options['foreign_key'] = $inflector->singularize($this->table) . '_id';
150: 		}
151:
152: 		if (!isset ($options['conditions'])) {
153: 			$options['conditions'] = '';
154: 		} else {
155: 			$options['conditions'] .= ' and ';
156: 		}
157:
158: 		if (isset ($options['as'])) {
159: 			$options['conditions'] .= Associations :: associate_as($options, $table, $inflector->singularize(ucwords($this->table)));
160: 		} else {
161: 			$options['conditions'] .= $table . ".`" . $options['foreign_key'] . "` = " . $this->id;
162: 		}
163:
164: 		$options['limit'] = 1;
165: 		$this-> $association_table = $object->find($options);
166:
167: 		return $this-> $association_table;
168: 	}

File: lib/model/Associations.php, #line 129

belongs_to($options = array (), $table)

173: 	function belongs_to($options = array (), $table) {
176:
177:
178: 		$association_table = $table;
179: 		$inflector = new Inflector;
180:
181: 		if (isset ($options['polymorphic'])) {
182: 			$associations = $table;
183: 			$class = ucwords($this-> {$table . '_type' });
184: 			$table = strtolower($inflector->pluralize($class));
185: 			$options['foreign_key'] = $associations . '_id';
186: 		}
187: 		elseif (isset ($options['class_name'])) {
188: 			$table = $inflector->tableize($options['class_name']);
190: 			$class = ucwords($inflector->singularize($inflector->camelize($table)));
191: 		} else {
192: 			$class = ucwords($table); // User
193: 			$table = $inflector->pluralize($table);
194: 		}
195: 		$object = new $class;
196:
197: 		if (!isset ($options['foreign_key'])) {
198: 			$options['foreign_key'] = $inflector->singularize($table) . '_id';
199: 		}
200:
201: 		if (!isset ($options['conditions'])) {
202: 			$options['conditions'] = '';
203: 		} else {
204: 			$options['conditions'] .= ' and ';
205: 		}
206: 		if ($this-> $options['foreign_key'] > 0) {
207: 			$options['conditions'] .= $table . ".`id` = " . $this-> $options['foreign_key'];
208: 			$options['limit'] = 1;
209: 			return $this-> $association_table = $object->find($options);
210: 		} else {
211: 			return $this-> $association_table = $object->create();
212: 		}
213: 	}

File: lib/model/Associations.php, #line 173

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>