J
J
Jamal Absalimov2019-05-19 10:48:38
Yii
Jamal Absalimov, 2019-05-19 10:48:38

Insert() ActiveRecord MongoDB not working. Why?

Connecting to mongo and fetching works, adding a document via migration works, but not via ActiveRecord.
Active Record Model

<?php

namespace common\models\activeRecord\searchPhone;

use Yii;

/**
 * This is the model class for collection "search_phone".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 * @property mixed $name
 * @property mixed $birth
 * @property mixed $full_name
 */
class SearchPhone extends \yii\mongodb\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function collectionName()
    {
        return ['mydb', 'search_phone'];

    }

    /**
     * {@inheritdoc}
     */
    public function attributes()
    {
        return [
            '_id',
            'phone',
            'name',
            'bitrh',
            'full_name',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'bitrh', 'full_name'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            '_id'       => 'ID',
            'name'      => 'Name',
            'bitrh'     => 'Bitrh',
            'full_name' => 'Full Name',
        ];
    }

}

How do I try to add a document
/**
     * @throws \Exception
     */
    public function getModelSearchPhone()
    {

        /**
         * @var $model \common\models\localRecord\searchPhone\SearchPhone
         */
        $model = new $this->search_phone();

        $model->insert(['name'=> 'alex','phone'=> '79898707217','birth'=> 3124523143,'full_name'=> 'alex alex']);

    }

Adds a document with only the object_id field, other fields are not added

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
Vlad Osadchyi, 2019-05-19
@VladOsadchyi

you don't have a phone field in your rules() model , so the insert request doesn't validate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question