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 {
143: 			$class = $inflector->camelize($table);
144: 			$table = $inflector->pluralize($table);
145: 		}
146:
147: 		$object = new $class;
148:
149: 		if (!isset ($options['foreign_key'])) {
150: 			$options['foreign_key'] = $inflector->singularize($this->table) . '_id';
151: 		}
152:
153: 		if (!isset ($options['conditions'])) {
154: 			$options['conditions'] = '';
155: 		} else {
156: 			$options['conditions'] .= ' and ';
157: 		}
158:
159: 		if (isset ($options['as'])) {
160: 			$options['conditions'] .= Associations :: associate_as($options, $table, $inflector->singularize(ucwords($this->table)));
161: 		} else {
162: 			$options['conditions'] .= $table . ".`" . $options['foreign_key'] . "` = " . $this->id;
163: 		}
164:
165: 		$options['limit'] = 1;
166: 		$this-> $association_table = $object->find($options);
167:
168: 		return $this-> $association_table;
169: 	}

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

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

174: 	function belongs_to($options = array (), $table) {
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 {
193: 			$class = $inflector->camelize($table);
194: 			$table = $inflector->pluralize($table);
195: 		}
196: 		$object = new $class;
197:
198: 		if (!isset ($options['foreign_key'])) {
199: 			$options['foreign_key'] = $inflector->singularize($table) . '_id';
200: 		}
201:
202: 		if (!isset ($options['conditions'])) {
203: 			$options['conditions'] = '';
204: 		} else {
205: 			$options['conditions'] .= ' and ';
206: 		}
207:
208: 		if ($this-> $options['foreign_key'] > 0) {
209: 			$options['conditions'] .= $table . ".`id` = " . $this-> $options['foreign_key'];
210: 			$options['limit'] = 1;
211: 			return $this-> $association_table = $object->find($options);
212: 		} else {
213: 			return $this-> $association_table = $object->create();
214: 		}
215: 	}

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

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>