|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CommentCreateForm.php |
|
4
|
|
|
* @author Revin Roman |
|
5
|
|
|
* @link https://rmrevin.ru |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace rmrevin\yii\module\Comments\forms; |
|
9
|
|
|
|
|
10
|
|
|
use rmrevin\yii\module\Comments; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CommentCreateForm |
|
14
|
|
|
* @package rmrevin\yii\module\Comments\forms |
|
15
|
|
|
*/ |
|
16
|
|
|
class CommentCreateForm extends \yii\base\Model |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
public $id; |
|
20
|
|
|
public $entity; |
|
21
|
|
|
public $from; |
|
22
|
|
|
public $text; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Comments\models\Comment */ |
|
25
|
|
|
public $Comment; |
|
26
|
|
|
|
|
27
|
|
|
public function init() |
|
28
|
|
|
{ |
|
29
|
|
|
$Comment = $this->Comment; |
|
30
|
|
|
|
|
31
|
|
|
if (false === $this->Comment->isNewRecord) { |
|
32
|
|
|
$this->id = $Comment->id; |
|
33
|
|
|
$this->entity = $Comment->entity; |
|
34
|
|
|
$this->from = $Comment->from; |
|
35
|
|
|
$this->text = $Comment->text; |
|
36
|
|
|
} elseif (!\Yii::$app->getUser()->getIsGuest()) { |
|
|
|
|
|
|
37
|
|
|
$User = \Yii::$app->getUser()->getIdentity(); |
|
38
|
|
|
|
|
39
|
|
|
$this->from = $User instanceof Comments\interfaces\CommentatorInterface |
|
40
|
|
|
? $User->getCommentatorName() |
|
41
|
|
|
: null; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public function rules() |
|
49
|
|
|
{ |
|
50
|
|
|
$CommentModelClassName = Comments\Module::instance()->model('comment'); |
|
51
|
|
|
|
|
52
|
|
|
return [ |
|
53
|
|
|
[['entity', 'text'], 'required'], |
|
54
|
|
|
[['entity', 'from', 'text'], 'string'], |
|
55
|
|
|
[['id'], 'integer'], |
|
56
|
|
|
[['id'], 'exist', 'targetClass' => $CommentModelClassName, 'targetAttribute' => 'id'], |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function attributeLabels() |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'entity' => \Yii::t('app', 'Entity'), |
|
67
|
|
|
'from' => \Yii::t('app', 'Your name'), |
|
68
|
|
|
'text' => \Yii::t('app', 'Text'), |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return bool |
|
74
|
|
|
* @throws \yii\web\NotFoundHttpException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function save() |
|
77
|
|
|
{ |
|
78
|
|
|
$Comment = $this->Comment; |
|
79
|
|
|
|
|
80
|
|
|
$CommentModelClassName = Comments\Module::instance()->model('comment'); |
|
81
|
|
|
|
|
82
|
|
|
if (empty($this->id)) { |
|
83
|
|
|
$Comment = \Yii::createObject($CommentModelClassName); |
|
84
|
|
|
} elseif ($this->id > 0 && $Comment->id !== $this->id) { |
|
85
|
|
|
/** @var Comments\models\Comment $CommentModel */ |
|
86
|
|
|
$CommentModel = \Yii::createObject($CommentModelClassName); |
|
87
|
|
|
$Comment = $CommentModel::find() |
|
88
|
|
|
->byId($this->id) |
|
89
|
|
|
->one(); |
|
90
|
|
|
|
|
91
|
|
|
if (!($Comment instanceof Comments\models\Comment)) { |
|
92
|
|
|
throw new \yii\web\NotFoundHttpException; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$Comment->entity = $this->entity; |
|
97
|
|
|
$Comment->from = $this->from; |
|
98
|
|
|
$Comment->text = $this->text; |
|
99
|
|
|
|
|
100
|
|
|
$result = $Comment->save(); |
|
101
|
|
|
|
|
102
|
|
|
if ($Comment->hasErrors()) { |
|
103
|
|
|
foreach ($Comment->getErrors() as $attribute => $messages) { |
|
104
|
|
|
foreach ($messages as $mes) { |
|
105
|
|
|
$this->addError($attribute, $mes); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->Comment = $Comment; |
|
111
|
|
|
|
|
112
|
|
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: