Advertisement
ricardo99

Untitled

Dec 16th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <?php
  2. namespace App\Model\Table;
  3.  
  4. use Cake\Core\Exception\Exception;
  5. use Cake\ORM\RulesChecker;
  6. use Cake\ORM\Table;
  7. use Cake\Validation\Validator;
  8.  
  9. /**
  10. * ProductFeatures Model
  11. *
  12. * @property \Cake\ORM\Association\BelongsTo $Products
  13. */
  14. class ProductFeaturesTable extends Table
  15. {
  16.  
  17. /**
  18. * Initialize method
  19. *
  20. * @param array $config The configuration for the Table.
  21. * @return void
  22. */
  23. public function initialize(array $config)
  24. {
  25. parent::initialize($config);
  26.  
  27. $this->table('product_features');
  28. $this->displayField('id');
  29. $this->primaryKey('id');
  30. $this->addBehavior('Timestamp');
  31. $this->belongsTo('Products', [
  32. 'foreignKey' => 'product_id',
  33. 'joinType' => 'INNER'
  34. ]);
  35. $this->belongsTo('Features', [
  36. 'foreignKey' => 'feature_intern_code',
  37. 'joinType' => 'INNER'
  38. ]);
  39. }
  40.  
  41. /**
  42. * Default validation rules.
  43. *
  44. * @param \Cake\Validation\Validator $validator Validator instance.
  45. * @return \Cake\Validation\Validator
  46. */
  47. public function validationDefault(Validator $validator)
  48. {
  49. $validator
  50. ->add('id', 'valid', ['rule' => 'numeric'])
  51. ->allowEmpty('id', 'create');
  52.  
  53. $validator
  54. ->requirePresence('feature_value', 'create')
  55. ->notEmpty('feature_value');
  56.  
  57. $validator
  58. ->requirePresence('feature_intern_code', 'create')
  59. ->notEmpty('feature_intern_code');
  60.  
  61. return $validator;
  62. }
  63.  
  64. /**
  65. * Returns a rules checker object that will be used for validating
  66. * application integrity.
  67. *
  68. * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
  69. * @return \Cake\ORM\RulesChecker
  70. */
  71. public function buildRules(RulesChecker $rules)
  72. {
  73. $rules->add($rules->existsIn(['product_id'], 'Products'));
  74. return $rules;
  75. }
  76.  
  77. public function setProductFeaturesEntities($productFeatures)
  78. {
  79. try {
  80. foreach ($productFeatures as $productFeature) {
  81. $this->save($productFeature);
  82. }
  83. return true;
  84. }catch (Exception $e){
  85. throw $e;
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement