How to use $push / $pull



1.4k

pretty sure its

db connector -> collection -> update( //your array here include push pull string, seems like json string to me)

edited Jan '15

Thank for help.

In Module.php file ... ( namespace Modules\Backend; )

    $di->set('mongo', function() {
        $mongo = new \MongoClient("mongodb:/myusername:[email protected]:27017/mongodb");
        return $mongo->selectDB("mongodb"); //database name in MongoDB
    }, true);

In Controller/Categories.php

How do I use db connect in this file ?

I'm coding

echo '<pre>'; print_r($mongo); echo '</pre>'; exit();

It's error

" Undefined variable: mongo "

pretty sure its

db connector -> collection -> update( //your array here include push pull string, seems like json string to me)



1.4k
$di->get("mongo")->collection
$di->mongo->collection
\Phalcon\Di::getDefault()->mongo->collection
\Phalcon\Di::getDefault()->get("mongo")->collection

$this->di->mongo->collection
$this->di->get("mongo")->collection
$this->mongo->collection
$this->get("mongo")->collection
$this["mongo"]->collection

etc etc etc

depends on where you are,

I want to do "Navigation Menu" with "Sub Navigation Menu"

Now I have "Main Navigation"

Array ( [_id] => MongoId Object ( [$id] => 54b18a77d41a3e8b128b4567 )

[parent] => 
[name] => food recipe
[slug] => food recipe
[title] => food recipe
[keyword] => food recipe
[description] => food recipe
[order] => 0
[status] => Published
[created_at] => 2015-01-11 03:24:23
[updated_at] => 2015-01-23 15:42:03
[language] => en
[slogan] => 
[childs] =>

)

if I want insert "Sub Navigation" into "childs" How do I do?

This my code

    $category = Category::findById($id);

    $category->childs = array('$push' => array(
                                '_id' => new \MongoId(),
                                'title' => 'test'
                            )
                        );

    $category->save();

It's error

Please you help me I want example code .

Thank you.

```php $di->get("mongo")->collection $di->mongo->collection \Phalcon\Di::getDefault()->mongo->collection \Phalcon\Di::getDefault()->get("mongo")->collection

$this->di->mongo->collection $this->di->get("mongo")->collection $this->mongo->collection $this->get("mongo")->collection $this["mongo"]->collection ```

etc etc etc

depends on where you are,



1.4k

Not entirely sure if ODM has relationships, but if it does model relationships are like these

class Category
{
    public $id;
    public $parent;

    public function initialize()
    {
        $this->hasMany("id", "Category", "parent", ["alias" => "childs"]);
        $this->belongsTo("parent", "Category", "id", ["alias" => "parent"]);
    }
}

$category = new Category();
$subCategory = new Category();
$category->childs[] = $subCategory;

Ah!! Thank you.

I'll try it. ^^

Not entirely sure if ODM has relationships, but if it does model relationships are like these

```php class Category { public $id; public $parent;

public function initialize() { $this->hasMany("id", "Category", "parent", ["alias" => "childs"]); $this->belongsTo("parent", "Category", "id", ["alias" => "parent"]); } }

$category = new Category(); $subCategory = new Category(); $category->childs[] = $subCategory; ```