S
S
Sergey Beloventsev2018-02-06 13:02:34
Yii
Sergey Beloventsev, 2018-02-06 13:02:34

Why does not save in mongo?

there is a Phone class, it organizes saving to the MongoBD database, this is how saving is organized

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;
    }
    ...
}

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;
    }
}

in general, in the debug-er I get like this
viber.phone.bulkWrite([{"type":"insert","document":[]}])

Tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2018-02-06
@Maksclub

Maybe the validation fails? Try to catch the validation error:

if(!$phone->validate())
{
        $errors = $model->errors;
        var_dump($errors);die;
}

D
davidnum95, 2018-02-06
@davidnum95

Did you forget the attributes method?
poke

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question