CFormModel.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * CFormModel class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CFormModel represents a data model that collects HTML form inputs.
  12. *
  13. * Unlike {@link CActiveRecord}, the data collected by CFormModel are stored
  14. * in memory only, instead of database.
  15. *
  16. * To collect user inputs, you may extend CFormModel and define the attributes
  17. * whose values are to be collected from user inputs. You may override
  18. * {@link rules()} to declare validation rules that should be applied to
  19. * the attributes.
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @package system.web
  23. * @since 1.0
  24. */
  25. class CFormModel extends CModel
  26. {
  27. private static $_names=array();
  28. /**
  29. * Constructor.
  30. * @param string $scenario name of the scenario that this model is used in.
  31. * See {@link CModel::scenario} on how scenario is used by models.
  32. * @see getScenario
  33. */
  34. public function __construct($scenario='')
  35. {
  36. $this->setScenario($scenario);
  37. $this->init();
  38. $this->attachBehaviors($this->behaviors());
  39. $this->afterConstruct();
  40. }
  41. /**
  42. * Initializes this model.
  43. * This method is invoked in the constructor right after {@link scenario} is set.
  44. * You may override this method to provide code that is needed to initialize the model (e.g. setting
  45. * initial property values.)
  46. */
  47. public function init()
  48. {
  49. }
  50. /**
  51. * Returns the list of attribute names.
  52. * By default, this method returns all public properties of the class.
  53. * You may override this method to change the default.
  54. * @return array list of attribute names. Defaults to all public properties of the class.
  55. */
  56. public function attributeNames()
  57. {
  58. $className=get_class($this);
  59. if(!isset(self::$_names[$className]))
  60. {
  61. $class=new ReflectionClass(get_class($this));
  62. $names=array();
  63. foreach($class->getProperties() as $property)
  64. {
  65. $name=$property->getName();
  66. if($property->isPublic() && !$property->isStatic())
  67. $names[]=$name;
  68. }
  69. return self::$_names[$className]=$names;
  70. }
  71. else
  72. return self::$_names[$className];
  73. }
  74. }