PHP code example of mongodb / transistor

1. Go to this page and download the library: Download mongodb/transistor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mongodb / transistor example snippets



class Person implements MongoDB\BSON\Persistable {
    use MongoDB\Transistor;
    protected $_id;
    protected $username;
    protected $email;
    protected $name;
    protected $addresses = array();
    protected $_lastModified;
    protected $_created;
}

class Address implements MongoDB\BSON\Persistable {
    use MongoDB\Transistor;
    protected $_id;
    protected $streetAddress;
    protected $city;
    protected $postalCode;
}


/* Construct a new person */
$person = new Person("bjori", "[email protected]", "Hannes Magnusson");

/* Insert it */
insert($person);

/* Find a person based on its username */
$person = findOne(array("username" => "bjori"));

/* Get an instance of the Person object again */
var_dump($person);


/* Continuing from previous example, $person is instanceof Person */
$person->setName("Dr. " . $person->getName());

/* Update the document */
update(array("username" => "bjori"), $person);

/* Retrieve it again */
$person = findOne(array("username" => "bjori"));

/* Get an instance of the Person object again */
var_dump($person);


/* Continuing from previous example, $person is instanceof Person */

/* Construct a new Address object */
$address = new Address("Manabraut 4", "Kopavogur", 200);
$person->addAddress($address);

/* Update the object with a new Address embedded object */
update(array("username" => "bjori"), $person);

$person = findOne(array("username" => "bjori"));
var_dump($person);