Esempio n. 1
0
 /**
  * @param HHPnet\Core\Domain\Users\User $user
  *
  * @return bool
  */
 public function save(User $user)
 {
     $user_data = iterator_to_array($user);
     $user_data['_id'] = $user_data['id'];
     unset($user_data['id']);
     $result = $this->collection->updateOne(['_id' => $user_data['_id']], $user_data, ['upsert' => true]);
     if (1 !== $result->getUpsertedCount()) {
         throw new DomainException('User data could not be saved into database');
     }
     return $user;
 }
Esempio n. 2
0
 /**
  * @param Album $album
  *
  * @return HHPnet\Core\Domain\Albums\Album
  */
 public function save(Album $album)
 {
     $album_data = iterator_to_array($album);
     $album_data['_id'] = $album_data['id'];
     unset($album_data['id']);
     $result = $this->collection->updateOne(['_id' => $album_data['_id']], $album_data, ['upsert' => true]);
     if (1 !== $result->getUpsertedCount()) {
         throw new DomainException('Album data could not be saved into database');
     }
     return $album;
 }
Esempio n. 3
0
$collection = new MongoDB\Collection($manager, "phplib_demo.write");
$hannes = ["name" => "Hannes", "nick" => "bjori", "citizen" => "Iceland"];
$hayley = ["name" => "Hayley", "nick" => "Ninja", "citizen" => "USA"];
$bobby = ["name" => "Robert Fischer", "nick" => "Bobby Fischer", "citizen" => "USA"];
$kasparov = ["name" => "Garry Kimovich Kasparov", "nick" => "Kasparov", "citizen" => "Russia"];
$spassky = ["name" => "Boris Vasilievich Spassky", "nick" => "Spassky", "citizen" => "France"];
try {
    $result = $collection->insertOne($hannes);
    printf("Inserted _id: %s\n\n", $result->getInsertedId());
    $result = $collection->insertOne($hayley);
    printf("Inserted _id: %s\n\n", $result->getInsertedId());
    $result = $collection->insertOne($bobby);
    printf("Inserted _id: %s\n\n", $result->getInsertedId());
    $count = $collection->count(["nick" => "bjori"]);
    printf("Searching for nick => bjori, should have only one result: %d\n\n", $count);
    $result = $collection->updateOne(["citizen" => "USA"], ['$set' => ["citizen" => "Iceland"]]);
    printf("Updated: %s (out of expected 1)\n\n", $result->getModifiedCount());
    $cursor = $collection->find(["citizen" => "Iceland"], ["comment" => "Excellent query"]);
    echo "Searching for citizen => Iceland, verify Hayley is now Icelandic\n";
    foreach ($cursor as $document) {
        var_dump($document);
    }
    echo "\n";
} catch (Exception $e) {
    printf("Caught exception '%s', on line %d\n", $e->getMessage(), __LINE__);
    exit;
}
try {
    $cursor = $collection->find();
    echo "Find all docs, should be 3, verify 1x USA citizen, 2x Icelandic\n";
    foreach ($cursor as $document) {
 /**
  * Update account
  */
 public function updateAccount(Request $request)
 {
     $data = ["name" => $request->name, "cp" => $request->cp, "phone" => $request->phone, "sexe" => $request->sexe, "news" => $request->news];
     $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
     $collection = new \MongoDB\Collection($manager, 'builders', 'account');
     $stat = ['email' => $request->email, 'data' => $data, 'created' => new \DateTime("now")];
     try {
         $collection->updateOne(["email" => $request->email], $stat);
     } catch (\Exception $e) {
         return response()->json(['state' => false]);
     }
     $data["email"] = $request->email;
     return response()->json(['data' => $data, 'state' => true]);
 }