Код: Выделить всё
class Phone extends ActiveRecord
{
public $contact_collection_id;
public $phone;
public $clients_id;
public $username;
public static function collectionName()
{
return 'phone';
}
/**
* @param int $contact_collection_id
* @param int $phone
* @param int $clients_id
* @param string $username
*/
public static function createPhone(int $contact_collection_id,int $phone, int $clients_id,string $username){
$phones = new static();
$phones->contact_collection_id=$contact_collection_id;
$phones->phone=$phone;
$phones->clients_id=$clients_id;
$phones->username=$username;
return $phones;
}
...
}
Код: Выделить всё
public function actionCreate($id)
{
$form = new PhoneCreateForm();
if ($id) {
$form->contact_collection_id = Yii::$app->request->get('id');
}
$form->clients_id = Yii::$app->user->identity->id;
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$entities = $this->createService->create($form);
return $this->redirect(['view', 'id' => (string)$entities['_id']]);
} catch (RuntimeException $ex) {
Yii::$app->errorHandler->logException($ex);
Yii::$app->session->setFlash('error', $ex->getMessage());
}
} else {
return $this->render('create', [
'model' => $form,
]);
}
}
Код: Выделить всё
class PhoneFormCreateService
{
public function create(PhoneCreateForm $phoneCreateForm){
$array=['+','(',')','-'];
if(Phone::find()->where(['phone'=>$phoneCreateForm->phone,'contact_collection_id'=>$phoneCreateForm->contact_collection_id])->one())
throw new \RuntimeException('Phone already exist');
$phone=Phone::createPhone(
$phoneCreateForm->contact_collection_id,
(int)str_replace($array,'',$phoneCreateForm->phone),
$phoneCreateForm->clients_id,
$phoneCreateForm->username
);
if(!$phone->save())
throw new \RuntimeException(json_encode($phone->errors));
return $phone;
}
}
в общем итоге в debug-ере получаю вот так
Код: Выделить всё
viber.phone.bulkWrite([{"type":"insert","document":[]}])
Подскажите что я делаю не так ?