EMongoExistValidator.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * EExistValidator validates that the attribute value exists in a table.
  4. *
  5. * This validator is often used to verify that a foreign key contains a value
  6. * that can be found in the foreign table.
  7. *
  8. * When using the {@link message} property to define a custom error message, the message
  9. * may contain additional placeholders that will be replaced with the actual content. In addition
  10. * to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
  11. * CExistValidator allows for the following placeholders to be specified:
  12. * <ul>
  13. * <li>{value}: replaced with value of the attribute.</li>
  14. * </ul>
  15. *
  16. * @see CExistValidator
  17. */
  18. class EMongoExistValidator extends CValidator
  19. {
  20. /**
  21. * @var boolean whether the comparison is case sensitive. Defaults to true.
  22. * Note, by setting it to false, you are assuming the attribute type is string.
  23. */
  24. public $caseSensitive = true;
  25. /**
  26. * @var string the ActiveRecord class name that should be used to
  27. * look for the attribute value being validated. Defaults to null,
  28. * meaning using the ActiveRecord class of the attribute being validated.
  29. * You may use path alias to reference a class name here.
  30. * @see attributeName
  31. */
  32. public $className;
  33. /**
  34. * @var string the ActiveRecord class attribute name that should be
  35. * used to look for the attribute value being validated. Defaults to null,
  36. * meaning using the name of the attribute being validated.
  37. * @see className
  38. */
  39. public $attributeName;
  40. /**
  41. * @var mixed additional query criteria. Either an array or CDbCriteria.
  42. * This will be combined with the condition that checks if the attribute
  43. * value exists in the corresponding table column.
  44. * This array will be used to instantiate a {@link CDbCriteria} object.
  45. */
  46. public $criteria = array();
  47. /**
  48. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  49. * meaning that if the attribute is empty, it is considered valid.
  50. */
  51. public $allowEmpty = true;
  52. public $mongoId = false;
  53. /**
  54. * Validates the attribute of the object.
  55. * If there is any error, the error message is added to the object.
  56. * @param CModel $object the object being validated
  57. * @param string $attribute the attribute being validated
  58. */
  59. protected function validateAttribute($object, $attribute)
  60. {
  61. $value = $object->$attribute;
  62. if($this->allowEmpty && $this->isEmpty($value)){
  63. return;
  64. }
  65. $className = $this->className === null ? get_class($object) : Yii::import($this->className);
  66. $attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
  67. $finder = EMongoDocument::model($className);
  68. $criteria = array($attributeName => $this->mongoId ? new MongoId($value) : $value);
  69. if(!$finder->exists($criteria)){
  70. $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" is invalid.');
  71. $this->addError($object, $attribute, $message, array('{value}' => CHtml::encode($value)));
  72. }
  73. }
  74. }