a tiny mvc framework for php using php-activerecord
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

GH-39: removing checks for static::s|getters - we only check for methods prefixed with s|get_ on a model

+101 -114
+83 -96
lib/php-activerecord/lib/Model.php
··· 229 229 static $delegate = array(); 230 230 231 231 /** 232 - * Define customer setters methods for the model. 232 + * Constructs a model. 233 233 * 234 - * You can also use this to define custom setters for attributes as well. 234 + * When a user instantiates a new object (e.g.: it was not ActiveRecord that instantiated via a find) 235 + * then @var $attributes will be mapped according to the schema's defaults. Otherwise, the given 236 + * $attributes will be mapped via set_attributes_via_mass_assignment. 235 237 * 236 238 * <code> 237 - * class User extends ActiveRecord\Model { 238 - * static $setters = array('password','more','even_more'); 239 - * 240 - * # now to define the setter methods. Note you must 241 - * # prepend set_ to your method name: 242 - * function set_password($plaintext) { 243 - * $this->encrypted_password = md5($plaintext); 244 - * } 245 - * } 246 - * 247 - * $user = new User(); 248 - * $user->password = 'plaintext'; # will call $user->set_password('plaintext') 239 + * new Person(array('first_name' => 'Tito', 'last_name' => 'the Grief')); 249 240 * </code> 250 241 * 251 - * If you define a custom setter with the same name as an attribute then you 252 - * will need to use assign_attribute() to assign the value to the attribute. 253 - * This is necessary due to the way __set() works. 254 - * 255 - * For example, assume 'name' is a field on the table and we're defining a 256 - * custom setter for 'name': 257 - * 258 - * <code> 259 - * class User extends ActiveRecord\Model { 260 - * static $setters = array('name'); 261 - * 262 - * # INCORRECT way to do it 263 - * # function set_name($name) { 264 - * # $this->name = strtoupper($name); 265 - * # } 266 - * 267 - * function set_name($name) { 268 - * $this->assign_attribute('name',strtoupper($name)); 269 - * } 270 - * } 271 - * 272 - * $user = new User(); 273 - * $user->name = 'bob'; 274 - * echo $user->name; # => BOB 275 - * </code> 276 - * 277 - * @var array 242 + * @param array $attributes Hash containing names and values to mass assign to the model 243 + * @param boolean $guard_attributes Set to true to guard attributes 244 + * @param boolean $instantiating_via_find Set to true if this model is being created from a find call 245 + * @param boolean $new_record Set to true if this should be considered a new record 246 + * @return Model 278 247 */ 279 - static $setters = array(); 248 + public function __construct(array $attributes=array(), $guard_attributes=true, $instantiating_via_find=false, $new_record=true) 249 + { 250 + $this->__new_record = $new_record; 251 + 252 + // initialize attributes applying defaults 253 + if (!$instantiating_via_find) 254 + { 255 + foreach (static::table()->columns as $name => $meta) 256 + $this->attributes[$meta->inflected_name] = $meta->default; 257 + } 258 + 259 + $this->set_attributes_via_mass_assignment($attributes, $guard_attributes); 260 + 261 + // since all attribute assignment now goes thru assign_attributes() we want to reset 262 + // dirty if instantiating via find since nothing is really dirty when doing that 263 + if ($instantiating_via_find) 264 + $this->__dirty = array(); 265 + 266 + $this->invoke_callback('after_construct',false); 267 + } 280 268 281 269 /** 282 - * Define customer getter methods for the model. 270 + * Magic method which delegates to read_attribute(). This handles firing off getter methods, 271 + * as they are not checked/invoked inside of read_attribute(). This circumvents the problem with 272 + * a getter being accessed with the same name as an actual attribute. 273 + * 274 + * You can also define customer getter methods for the model. 283 275 * 276 + * EXAMPLE: 284 277 * <code> 285 278 * class User extends ActiveRecord\Model { 286 - * static $getters = array('middle_initial','more','even_more'); 287 279 * 288 - * # now to define the getter method. Note you must 280 + * # define custom getter methods. Note you must 289 281 * # prepend get_ to your method name: 290 282 * function get_middle_initial() { 291 283 * return $this->middle_name{0}; ··· 305 297 * 306 298 * <code> 307 299 * class User extends ActiveRecord\Model { 308 - * static $getters = array('name'); 309 300 * 310 301 * # INCORRECT way to do it 311 302 * # function get_name() { ··· 322 313 * echo $user->name; # => BOB 323 314 * </code> 324 315 * 325 - * @var array 326 - */ 327 - static $getters = array(); 328 - 329 - /** 330 - * Constructs a model. 331 - * 332 - * When a user instantiates a new object (e.g.: it was not ActiveRecord that instantiated via a find) 333 - * then @var $attributes will be mapped according to the schema's defaults. Otherwise, the given 334 - * $attributes will be mapped via set_attributes_via_mass_assignment. 335 - * 336 - * <code> 337 - * new Person(array('first_name' => 'Tito', 'last_name' => 'the Grief')); 338 - * </code> 339 - * 340 - * @param array $attributes Hash containing names and values to mass assign to the model 341 - * @param boolean $guard_attributes Set to true to guard attributes 342 - * @param boolean $instantiating_via_find Set to true if this model is being created from a find call 343 - * @param boolean $new_record Set to true if this should be considered a new record 344 - * @return Model 345 - */ 346 - public function __construct(array $attributes=array(), $guard_attributes=true, $instantiating_via_find=false, $new_record=true) 347 - { 348 - $this->__new_record = $new_record; 349 - 350 - // initialize attributes applying defaults 351 - if (!$instantiating_via_find) 352 - { 353 - foreach (static::table()->columns as $name => $meta) 354 - $this->attributes[$meta->inflected_name] = $meta->default; 355 - } 356 - 357 - $this->set_attributes_via_mass_assignment($attributes, $guard_attributes); 358 - 359 - // since all attribute assignment now goes thru assign_attributes() we want to reset 360 - // dirty if instantiating via find since nothing is really dirty when doing that 361 - if ($instantiating_via_find) 362 - $this->__dirty = array(); 363 - 364 - $this->invoke_callback('after_construct',false); 365 - } 366 - 367 - /** 368 - * Magic method which delegates to read_attribute(). This handles firing off getter methods, 369 - * as they are not checked/invoked inside of read_attribute(). This circumvents the problem with 370 - * a getter being accessed with the same name as an actual attribute. 371 316 * 372 317 * @see read_attribute() 373 318 * @param string $name Name of an attribute ··· 376 321 public function &__get($name) 377 322 { 378 323 // check for getter 379 - if (in_array("get_$name",static::$getters)) 324 + if (method_exists($this, "get_$name")) 380 325 { 381 326 $name = "get_$name"; 382 327 $value = $this->$name(); ··· 398 343 } 399 344 400 345 /** 401 - * Magic allows un-defined attributes to set via $attributes 346 + * Magic allows un-defined attributes to set via $attributes. 347 + * 348 + * You can also define customer setter methods for the model. 349 + * 350 + * EXAMPLE: 351 + * <code> 352 + * class User extends ActiveRecord\Model { 353 + * 354 + * # define custom setter methods. Note you must 355 + * # prepend set_ to your method name: 356 + * function set_password($plaintext) { 357 + * $this->encrypted_password = md5($plaintext); 358 + * } 359 + * } 360 + * 361 + * $user = new User(); 362 + * $user->password = 'plaintext'; # will call $user->set_password('plaintext') 363 + * </code> 364 + * 365 + * If you define a custom setter with the same name as an attribute then you 366 + * will need to use assign_attribute() to assign the value to the attribute. 367 + * This is necessary due to the way __set() works. 368 + * 369 + * For example, assume 'name' is a field on the table and we're defining a 370 + * custom setter for 'name': 371 + * 372 + * <code> 373 + * class User extends ActiveRecord\Model { 374 + * 375 + * # INCORRECT way to do it 376 + * # function set_name($name) { 377 + * # $this->name = strtoupper($name); 378 + * # } 379 + * 380 + * function set_name($name) { 381 + * $this->assign_attribute('name',strtoupper($name)); 382 + * } 383 + * } 384 + * 385 + * $user = new User(); 386 + * $user->name = 'bob'; 387 + * echo $user->name; # => BOB 388 + * </code> 402 389 * 403 390 * @throws {@link UndefinedPropertyException} if $name does not exist 404 391 * @param string $name Name of attribute, relationship or other to set ··· 410 397 if (array_key_exists($name, static::$alias_attribute)) 411 398 $name = static::$alias_attribute[$name]; 412 399 413 - elseif (in_array("set_$name",static::$setters)) 400 + elseif (method_exists($this,"set_$name")) 414 401 { 415 402 $name = "set_$name"; 416 403 return $this->$name($value); ··· 1250 1237 $method = str_replace($association_name, 'association', $method); 1251 1238 $table = static::table(); 1252 1239 1253 - if (($association = $table->get_relationship($association_name)) || 1240 + if (($association = $table->get_relationship($association_name)) || 1254 1241 ($association = $table->get_relationship(($association_name = Utils::pluralize($association_name))))) 1255 1242 { 1256 1243 // access association to ensure that the relationship has been loaded
+6 -12
lib/php-activerecord/lib/Table.php
··· 538 538 } 539 539 540 540 /** 541 - * Builds the getters/setters array by prepending get_/set_ to the method names. 541 + * DEPRECATED: Model.php now checks for get|set_ methods via method_exists so there is no need for declaring static g|setters. 542 542 */ 543 543 private function set_setters_and_getters() 544 544 { 545 - $build = array('setters', 'getters'); 546 - 547 - foreach ($build as $type) 548 - { 549 - $methods = array(); 550 - $prefix = substr($type,0,3) . "_"; 551 - 552 - foreach ($this->class->getStaticPropertyValue($type,array()) as $method) 553 - $methods[] = (substr($method,0,4) != $prefix ? "{$prefix}$method" : $method); 545 + $getters = $this->class->getStaticPropertyValue('getters', array()); 546 + $setters = $this->class->getStaticPropertyValue('setters', array()); 554 547 555 - $this->class->setStaticPropertyValue($type,$methods); 556 - } 548 + if (!empty($getters) || !empty($setters)) 549 + error_log('DEPRECATION WARNING: static::$getters and static::$setters is deprecated and will be removed in a future version. Please define your setters and getters by declaring methods in your model prefixed with get_ or set_. See 550 + http://www.phpactiverecord.org/projects/main/wiki/Utilities#attribute-setters and http://www.phpactiverecord.org/projects/main/wiki/Utilities#attribute-getters on how to make use of this option.'); 557 551 } 558 552 }; 559 553 ?>
+2 -3
lib/php-activerecord/test/ActiveRecordTest.php
··· 383 383 384 384 public function test_setter_with_same_name_as_an_attribute() 385 385 { 386 - Author::$setters[] = 'name'; 387 386 $author = new Author(); 388 387 $author->name = 'bob'; 389 388 $this->assert_equals('BOB',$author->name); ··· 397 396 398 397 public function test_getter_with_same_name_as_an_attribute() 399 398 { 400 - Book::$getters[] = 'name'; 399 + Book::$use_custom_get_name_getter = true; 401 400 $book = new Book; 402 401 $book->name = 'bob'; 403 402 $this->assert_equals('BOB', $book->name); 404 - Book::$getters = array(); 403 + Book::$use_custom_get_name_getter = false; 405 404 } 406 405 407 406 public function test_setting_invalid_date_should_set_date_to_null()
-1
lib/php-activerecord/test/models/Author.php
··· 9 9 array('awesome_person', 'foreign_key' => 'author_id', 'primary_key' => 'author_id'), 10 10 array('parent_author', 'class_name' => 'Author', 'foreign_key' => 'parent_author_id')); 11 11 static $belongs_to = array(); 12 - static $setters = array('password'); 13 12 14 13 public function set_password($plaintext) 15 14 {
+10 -2
lib/php-activerecord/test/models/Book.php
··· 3 3 { 4 4 static $belongs_to = array('author'); 5 5 static $has_one = array(); 6 - static $getters = array('upper_name'); 6 + static $use_custom_get_name_getter = false; 7 7 8 8 public function upper_name() 9 9 { ··· 17 17 18 18 public function get_name() 19 19 { 20 - return strtoupper($this->read_attribute('name')); 20 + if (self::$use_custom_get_name_getter) 21 + return strtoupper($this->read_attribute('name')); 22 + else 23 + return $this->read_attribute('name'); 21 24 } 22 25 23 26 public function get_upper_name() 24 27 { 25 28 return strtoupper($this->name); 29 + } 30 + 31 + public function get_lower_name() 32 + { 33 + return strtolower($this->name); 26 34 } 27 35 }; 28 36 ?>