Completed
Push — master ( 6f92e6...cbba0d )
by Fèvre
9s
created

PollsTable::validationDefault()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 1
1
<?php
2
namespace App\Model\Table;
3
4
use Cake\ORM\Query;
5
use Cake\ORM\RulesChecker;
6
use Cake\ORM\Table;
7
use Cake\Validation\Validator;
8
9
/**
10
 * Polls Model
11
 *
12
 * @property \Cake\ORM\Association\BelongsTo $Users
13
 * @property \Cake\ORM\Association\BelongsTo $BlogArticles
14
 * @property \Cake\ORM\Association\HasMany $PollsAnswers
15
 * @property \Cake\ORM\Association\BelongsToMany $PollsUsers
16
 */
17
class PollsTable extends Table
18
{
19
20
    /**
21
     * Initialize method
22
     *
23
     * @param array $config The configuration for the Table.
24
     *
25
     * @return void
26
     */
27
    public function initialize(array $config)
28
    {
29
        parent::initialize($config);
30
31
        $this->table('polls');
32
        $this->displayField('name');
33
        $this->primaryKey('id');
34
35
        $this->addBehavior('Timestamp');
36
37
        $this->belongsTo('Users', [
38
            'foreignKey' => 'user_id'
39
        ]);
40
        $this->belongsTo('BlogArticles', [
41
            'foreignKey' => 'article_id'
42
        ]);
43
        $this->hasMany('PollsAnswers', [
44
            'foreignKey' => 'poll_id',
45
            'dependent' => true
46
        ]);
47
        $this->hasMany('PollsUsers', [
48
            'foreignKey' => 'poll_id',
49
            'dependent' => true
50
        ]);
51
    }
52
53
    /**
54
     * Default validation rules.
55
     *
56
     * @param \Cake\Validation\Validator $validator Validator instance.
57
     *
58
     * @return \Cake\Validation\Validator
59
     */
60
    public function validationDefault(Validator $validator)
61
    {
62
        $validator
63
            ->integer('id')
64
            ->allowEmpty('id', 'create');
65
66
        $validator
67
            ->requirePresence('name', 'create')
68
            ->notEmpty('name');
69
70
        $validator
71
            ->boolean('is_display')
72
            ->requirePresence('is_display', 'create')
73
            ->notEmpty('is_display');
74
75
        $validator
76
            ->integer('user_count')
77
            ->requirePresence('user_count', 'create')
78
            ->notEmpty('user_count');
79
80
        $validator
81
            ->boolean('is_timed')
82
            ->requirePresence('is_timed', 'create')
83
            ->notEmpty('is_timed');
84
85
        $validator
86
            ->dateTime('end_date')
87
            ->allowEmpty('end_date');
88
89
        return $validator;
90
    }
91
92
    /**
93
     * Returns a rules checker object that will be used for validating
94
     * application integrity.
95
     *
96
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
97
     *
98
     * @return \Cake\ORM\RulesChecker
99
     */
100
    public function buildRules(RulesChecker $rules)
101
    {
102
        $rules->add($rules->existsIn(['user_id'], 'Users'));
103
104
        return $rules;
105
    }
106
}
107