Search code examples
phparraysmongodbyii2yii-extensions

mongodb + yii2: How can i push an array/object to mongodb?


I am playing around with yii2 and mongodb and i am stuck at a one point. I have created the CRUD functionality using mongodb GII. Now i am making modification to put the objects of array in my database record.

On creation of a customer i create an object of mileage and push it like this.

$model = new Customer();
if ($model->load(Yii::$app->request->post())) {
    $mileage = new mileage(2, 'British Airways', 'Usman', 10000);
    $model->mileage = array($mileage);
    $model->save();
    return $this->redirect(['view', 'id' => (string) $model->_id]);
}

mileage.php

class Mileage {
    public $_id;
    public $sm_order_id;
    public $program;
    public $customer;
    public $miles;

    public function __construct($sm_order_id, $program, $customer, $miles) {
        $this->_id = new \MongoId();
        $this->sm_order_id = $sm_order_id;
        $this->program = $program;
        $this->customer = $customer;
        $this->miles = $miles;
    }
}

and i get the data in the following format in database:

array (
  '_id' => 
  MongoId::__set_state(array(
     '$id' => '569cd9ed9eb8954c2000002c',
  )),
  'name' => 'Alan',
  'email' => 'alan@abc.com',
  'address' => 'New York',
  'mileage' => 
  array (
    0 => 
    array (
      '_id' => 
      MongoId::__set_state(array(
         '$id' => '569cd9ed9eb8954c2000002b',
      )),
      'sm_order_id' => 2,
      'program' => 'British Airways',
      'customer' => 'Wake',
      'miles' => 10000,
    ),
  ),
)

Now i want a mechanism so i can add more array/objects in mileage node. I have searched a lot but didn't find anything. Same format of the database is mentioned below:

array (
  '_id' => 
  MongoId::__set_state(array(
     '$id' => '569cd9ed9eb8954c2000002c',
  )),
  'name' => 'Alan',
  'email' => 'alan@abc.com',
  'address' => 'New York',
  'mileage' => 
  array (
    0 => 
    array (
      '_id' => 
      MongoId::__set_state(array(
         '$id' => '569cd9ed9eb8954c2000002b',
      )),
      'sm_order_id' => 2,
      'program' => 'British Airways',
      'customer' => 'Wake',
      'miles' => 5000,
    ),
    1 => 
    array (
      '_id' => 
      MongoId::__set_state(array(
         '$id' => '569cd9ed9eb8954c2000002c',
      )),
      'sm_order_id' => 7,
      'program' => 'Qatar Airways',
      'customer' => 'Tony',
      'miles' => 2500,
    ),
  ),
  2 =>
  array (
  .........
  .........
  ),
)

Solution

  • Ok I have found out the solution long time ago and i thought i should share it here. So i am pushing the 1st object in mileage node like that:

    $model = Customer::findOne($customerId);
    if ($model->load(Yii::$app->request->post())) {
        $mileage = new mileage(2, 'British Airways', 'Usman', 10000);
        $model->mileage = array($mileage);
        $model->save();
        return $this->redirect(['view', 'id' => (string) $model->_id]);
    }
    

    So to add another object in mileage node, what we need to do is we need to get the customer object from database and we have to check if there is already an object added in the mileage node and then i have to get that object and push the newly created mileage array/object in it. modified code is below:

    $model = Customer::findOne($customerId);
    if ($model->load(Yii::$app->request->post())) {
        $mileage = new mileage(2, 'British Airways', 'Kamran', 9000);
        if (empty($model->mileage) || count($model->mileage) == 0) {
            $model->mileage = array($mileage);
        } else {
            $allUsedMiles = $model->mileage;
            array_push($allUsedMiles, $mileage);
            $model->mileage = $allUsedMiles;
        }
        $model->save();
        return $this->redirect(['view', 'id' => (string) $model->_id]);
    }
    

    I hope this will help someone in the future ;) Thanks,