V
V
Valery Yumshanov2017-06-27 19:09:58
Yii
Valery Yumshanov, 2017-06-27 19:09:58

Adding document to MongoDB collection in Yii2 not working via Active Record?

I have advanced template. Connected MongoDB in the common/config config in the main-local.php file (no password or user, database name is mymongo, sale collection):
'components' => [
'mongodb' => [
'class' => '\yii\mongodb \Connection',
'dsn' => 'mongodb://localhost:27017/mymongo',
],
] The
fetch works, no problem there. But adding a new document does not work. Used the insert() and save() methods from Active Record. I update the browser, there are no errors, there are no new data in the database. Moreover, if you write requests in pure PHP (without using a framework), then the data is added perfectly!
model:

<?php
namespace frontend\models;

use yii\base\Model;
use yii\mongodb\ActiveRecord;
use yii\db\ActiveRecordInterface;

class Sale extends ActiveRecord implements ActiveRecordInterface
{
    /**
     * @return string the name of the index associated with this ActiveRecord class.
     */
    public static function collectionName()
    {
        return 'sale';
    }

    /**
     * @return array list of attribute names.
     */
    public function attributes()
    {
        return ['_id', 'ids', 'name', 'path', 'file'];
    }
}

?>

controller
<?php
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use frontend\models\ColorForm;
use yii\data\Pagination;
use frontend\models\Sale;
use yii\mongodb\ActiveRecord;
use yii\db\ActiveRecordInterface;

/**
 * Site controller
 */
class SiteController extends Controller
{

public function actionSale()
    {

$query = Sale::find()->orderBy(['ids' => SORT_DESC])->where(['>=', 'ids', 3]);

$customer = new Sale();

$customer->ids = 18;
$customer->name = 'Donna';
$customer->save();

$pagination = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $query->count(),
        ]);

        $sales = $query->offset($pagination->offset)->limit($pagination->limit)->all();

        return $this->render('sale', [
            'sales' => $sales,
            'pagination' => $pagination,
        ]);
    }

}

?>

view views/site/sale.php
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($sales as $sale): ?>
    <li>
        <?= Html::encode("{$sale->ids} ({$sale->name})") ?>:
        <?= $sale->path ?>
    </li>
<?php endforeach; ?>
</ul>


<?= LinkPager::widget(['pagination' => $pagination]) ?>

Did everything according to the documentation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Yumshanov, 2017-06-28
@scanderberg

I solved the problem myself. I cleared the cache and it worked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question