1
|
|
|
<?php |
2
|
|
|
namespace App\Model\Table; |
3
|
|
|
|
4
|
|
|
use ArrayObject; |
5
|
|
|
use Cake\Event\Event; |
6
|
|
|
use Cake\ORM\Entity; |
7
|
|
|
use Cake\ORM\Table; |
8
|
|
|
use Cake\Validation\Validator; |
9
|
|
|
|
10
|
|
|
class BlogArticlesCommentsTable extends Table |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Initialize method |
15
|
|
|
* |
16
|
|
|
* @param array $config The configuration for the Table. |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function initialize(array $config) |
21
|
|
|
{ |
22
|
|
|
$this->table('blog_articles_comments'); |
|
|
|
|
23
|
|
|
$this->displayField('comment'); |
|
|
|
|
24
|
|
|
$this->primaryKey('id'); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$this->addBehavior('Timestamp'); |
27
|
|
|
$this->addBehavior('CounterCache', [ |
28
|
|
|
'Users' => ['blog_articles_comment_count'], |
29
|
|
|
'BlogArticles' => ['comment_count'] |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$this->belongsTo('BlogArticles', [ |
33
|
|
|
'foreignKey' => 'article_id', |
34
|
|
|
]); |
35
|
|
|
$this->belongsTo('Users', [ |
36
|
|
|
'foreignKey' => 'user_id', |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create validation rules. |
42
|
|
|
* |
43
|
|
|
* @param \Cake\Validation\Validator $validator Instance of the validator. |
44
|
|
|
* |
45
|
|
|
* @return \Cake\Validation\Validator |
46
|
|
|
*/ |
47
|
|
|
public function validationCreate(Validator $validator) |
48
|
|
|
{ |
49
|
|
|
$validator |
50
|
|
|
->notEmpty('content') |
51
|
|
|
->add('content', [ |
52
|
|
|
'minLength' => [ |
53
|
|
|
'rule' => ['minLength', 5], |
54
|
|
|
'message' => __("Please, {0} characters minimum for your comment.", 5) |
55
|
|
|
] |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
return $validator; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* AfterSave callback. |
63
|
|
|
* |
64
|
|
|
* @param \Cake\Event\Event $event The afterSave event that was fired. |
65
|
|
|
* @param \Cake\ORM\Entity $entity The entity that was saved. |
66
|
|
|
* @param \ArrayObject $options The options passed to the callback. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function afterSave(Event $event, Entity $entity, ArrayObject $options) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if ($entity->isNew()) { |
73
|
|
|
$comment = new Event('Model.BlogArticlesComments.add', $this, [ |
74
|
|
|
'comment' => $entity |
75
|
|
|
]); |
76
|
|
|
$this->eventManager()->dispatch($comment); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.