Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NCMB PHP SDKでサーバサイドからmBaaSを使おう /ncmb-php-sdk-de...

NCMB PHP SDKでサーバサイドからmBaaSを使おう /ncmb-php-sdk-details

2018-08-24 に開催された「NCMB PHP SDKでサーバサイドからmBaaSを使おう」セミナーの発表資料です。

KOYAMA Tetsuji

August 25, 2017
Tweet

More Decks by KOYAMA Tetsuji

Other Decks in Programming

Transcript

  1. ॳظԽ use Ncmb\NCMB; $appKey = 'xxxxxxxxxxxxxxx'; $clientKey = 'yyyyyyyyyyyyyyyy'; NCMB::initialize($appKey,

    $clientKey); w ͭͷΩʔΛ༻͍ͯॳظԽ w Ҏ߱ͷαϯϓϧίʔυ͸ॳظԽ͞Ε͍ͯΔ͜ͱ͕લఏ w ໊લۭؒ͸/DNC
  2. Ϣʔβ؅ཧɿαΠϯΞοϓ use Ncmb\User; $user = new User; $user->userName = 'Ncmb_User';

    $user->password = 'si20xG!Ajb5%yu'; $user->signUp(); w Ϣʔβ໊ͱύεϫʔυΛ༻͍ͯ৽نొ࿥
  3. Ϣʔβ؅ཧɿϩάΠϯ use Ncmb\User; $userName = 'Ncmb_User'; $password = 'si20xG!Ajb5%yu'; $user

    = User::login($userName, $password); w Ϣʔβ໊ͱύεϫʔυΛ༻͍ͯϩάΠϯ
  4. Ϣʔβ؅ཧɿϢʔβʹ෇Ճ৘ใΛ௥Ճ w 6TFSΫϥε͸0CKFDUΛܧঝ͍ͯ͠ΔͷͰɺ෇Ճ ৘ใΛ௥ՃͰ͖Δ use Ncmb\User; $userName = 'Ncmb_User'; $password

    = 'si20xG!Ajb5%yu'; $user = User::login($userName, $password); $user->age = 30; $user->rank = 27; $user->save();
  5. σʔλετΞɿอଘ w ΦϒδΣΫτͷϓϩύςΟʹ஋Ληοτͯ͠ɺ TBWF ϝιουΛݺͿ use Ncmb\Object; $className = 'example';

    $obj = new Object($className); $obj->intKey = 1; $obj->floatKey = 1.0; $obj->stringKey = 'String'; $obj->dateKey = new DateTime(); $obj->save();
  6. σʔλετΞɿอଘ w Ң౓ܦ౓ w TFU ϝιου use Ncmb\Object; use Ncmb\GeoPoint;

    $className = 'example'; $obj = new Object($className); $geoPoint = new GeoPoint(35.1, 139.2); $obj->set('geoKey', $geoPoint); $obj->save();
  7. σʔλετΞɿอଘ w ഑ྻʹ͸TFU"SSBZ ϝιουΛ༻͍Δ use Ncmb\Object; $className = 'example'; $obj

    = new Object($className); $obj->setArray('arrayKey', [1, 2, 3]); $obj->save();
  8. σʔλετΞɿJEΛࢦఆͨ͠ಡࠐ w PCKFDU*E͕͢Ͱʹ෼͔͍ͬͯΔ৔߹ w a/DNCa0CKFDUίϯετϥΫλʹPCKFDU*EΛ౉͢ w GFUDI ϝιουͰಡࠐΈ use Ncmb\Object;

    $className = 'example'; $objectId = '3Dw7EnByRYvtZ3rh'; $obj = new Object($className, $objectId); $obj->fetch();
  9. σʔλετΞɿσʔλͷߋ৽ w ಡΈࠐΜͩ0CKFDUʹରͯ͠ɺσʔλΛ௥Ճมߋ ͠TBWF ͢Δ͚ͩɻ use Ncmb\Object; $className = 'example';

    $objectId = '3Dw7EnByRYvtZ3rh'; $obj = new Object($className, $objectId); $obj->fetch(); $obj->appendKey = 'sample'; $obj->save();
  10. ϑΝΠϧετΞอଘ w อଘ͢Δσʔλͷ಺༰ɺ/$.#্ͷϑΝΠϧ໊ Λࢦఆͯ͠ɺ'JMFDSFBUF'SPN%BUB ϝιου ΛݺͿ use Ncmb\File; $data =

    'sample file data'; $fileName = 'sample.txt'; $file = File::createFromData($data, $fileName); $file->save();
  11. ϑΝΠϧετΞݕࡧ w 'JMFHFU2VFSZ ͰϑΝΠϧετΞ༻ͷ2VFSZΦ ϒδΣΫτΛऔಘ w ͋ͱ͸ࣗ༝ʹ৚݅Λࢦఆ use Ncmb\File; $query

    = File::getQuery(); $file = $query ->equalTo('fileName', 'sample.txt') ->first(); $data = $file->getData();
  12. ϓογϡ௨஌ૹ৴ w ඞཁͳύϥϝʔλΛઃఆͯ͠ a/DNCa1VTITFOE ΛݺͿ͚ͩ use Ncmb\Push; $options = [

    'immediateDeliveryFlag' => true, 'title' => 'NCMB push', 'message' => 'sample message', 'target' => ['android'], ]; $pushId = Push::send($options);
  13. ϓογϡ௨஌࡟আ w 1VTIEFMFUF ʹPCKFDU*EΛ౉͢ w ҎԼ͸ਖ਼ৗऴྃͨ͠ϓογϡ௨஌Λશͯ࡟আ͢Δྫ use Ncmb\Push; $query =

    Push::getQuery(); $query->equalTo('status', Push::STAT_SEND_OK); $pushes = $query->find(); foeach ($pushes as $push) { Push::delete($push->getObjectId()); }
  14. εΫϦϓτ࣮ߦ w "1*͔Βୟ͚Δͷ͸࣮ߦػೳͷΈ w a/DNCa4DSJQUFYFDVUF Λ༻͍Δ use Ncmb\Script; $script =

    'sample.js'; $params = [ 'query' => ['name' => 'NCMB Tarou'], ]; $result = Script::execute($script, $params);
  15. εΫϦϓτ(&5Ҏ֎ͷϝιου w a/DNCa4DSJQUFYFDVUF ͷୈҾ਺ use Ncmb\Script; $script = 'sample.js'; $params

    = [ 'query' => ['name' => 'NCMB Tarou'], ]; $method = 'PUT'; $result = Script::execute($script, $params $method);
  16. w "$- w ϩʔϧ w ϦϨʔγϣϯ w ϙΠϯλʔ w 2VFSZ

    w ಛघΦϖϥϯυ w ಈ࡞ઃఆͷ্ॻ͖ 1)14%,ͷ࢖͍ํ্ڃฤ
  17. "$- w "DDFTT$POUSPMM-JTU w /DNCa"DMΛ༻͍Δ use Ncmb\Acl; $acl = new

    Acl; $acl ->setPublicReadAccess(true) ->setPublicWriteAccess(true);
  18. "$-ࢦఆํ๏ w a/DNCa"DMͷϝιου setReadAccess: ಛఆϢʔβͷಡࠐݖݶηοτ getReadAccess: ಛఆϢʔβͷಡࠐݖݶήοτ setWriteAccess: ಛఆϢʔβͷॻࠐݖݶηοτ getWriteAccess:

    ಛఆϢʔβͷॻࠐݖݶήοτ {set,get}Public{Read,Write}Access: Ұൠʹର͢Δݖݶ getRole{Read,Write}Access: setRole{Read,Wreite}AccessWithName: ϩʔϧʹର͢Δݖݶ
  19. "$-ࢦఆྫ w શһ͕ಡΊΔ w VTFSͱVTFS͚ͩॻ͚Δ use Ncmb\Acl; $acl = new

    Acl; $acl ->setPublicReadAccess(true) ->setWriteAccess($user1, true) ->setWriteAccess($user2, true);
  20. ϩʔϧ w "$-ʹର͢ΔϢʔβͷηοτ use Ncmb\Role; $roleName = 'myRole'; $role =

    Role::createRole($roleName); $role->getUsers()->add($user1); $role->getUsers()->add($user2); $role->save(); $acl = new Acl; $acl->setRoleWriteAccessWithName($roleName, true);
  21. ϩʔϧೖΕࢠ w ϩʔϧʹϩʔϧΛ௥Ճ use Ncmb\Role; $role1 = Role::createRole('childRole'); $role1->getUsers()->add($user1); $role1->getUsers()->add($user2);

    $role1->save(); $role2 = Role::createRole('parentRole'); $role2->getRoles()->add($role1); $role2->save();
  22. ϙΠϯλ w ΦϒδΣΫτʹผΦϒδΣΫτͷࢀরΛ௥Ճ use Ncmb\Object; $className = 'example'; $obj1 =

    new Object($className, 'Tf1DG2Bvlf4mXn0a'); $obj1->fetch(); $obj2 = new Object($className); $obj2->pointerKey = $obj1; $obj2->save();
  23. ϦϨʔγϣϯ w ϙΠϯλͱಉ༷ʹผΦϒδΣΫτͷࢀরΛ௥Ճ w ಉҰΫϥεʹݶΒΕΔ͕ɺෳ਺ͷΦϒδΣΫτΛઃ ఆͰ͖Δ use Ncmb\Object; // $obj,$obj2

    ͸ಡࠐࡁΈͱ͢Δ $obj = new Object('example'); $relation = $obj->getRelation('relKey', 'example'); $relation->add($obj1); $relation->add($obj2); $obj->save();
  24. 2VFSZ࢖༻๏ w 0CKFDUͷݕࡧ͸2VFSZΛΫϥε໊Λ౉ͯ͠OFX w ͦΕҎ֎͸֤ΫϥεͷHFU2VFSZ ϝιου use Ncmb\Query; $className =

    'example'; $query = new Query($className); $query->equalTo('intKey', 1); $objs = $query->find(); $query = Push::getQuery();
  25. 2VFSZͷ৚݅ઃఆ w a/DNCa2VFSZΛ༻͍༷ͯʑͳ৚݅ͰݕࡧͰ͖Δ equalTo: ஋͕౳͍͠ notEqualTo: ஋͕౳͘͠ͳ͍ lessThan: ࢦఆͨ͠஋ΑΓখ͍͞($lt) lessThanOrEqual:

    ࢦఆͨ͠஋ҎԼ ($lte) greaterThan: ࢦఆͨ͠஋ΑΓେ͖͍ ($gt) greaterThanOrEqual: ࢦఆͨ͠஋Ҏ্ ($gte) containedIn: ஋ؚ͕·ΕΔ ($in) notContainedIn: ஋ؚ͕·Εͳ͍ ($nin)
  26. 2VFSZͷ৚݅ઃఆ ଓ͖ exists: ஋͕ଘࡏ͢Δ ($exists) doesNotExists: ஋͕ଘࡏ͠ͳ͍ regex: ਖ਼نදݱʹϚον ($regex)

    inArray: ഑ྻʹؚ·ΕΔ ($inArray) notInArray: ഑ྻʹؚ·Εͳ͍ ($ninArray) containsAll: ͢΂ͯͷ஋ؚ͕·ΕΔ ($all) matchesQuery: αϒΫΤϦʹ߹க͢ΔσʔλΛϙΠϯ λͱ͍ͯ࣋ͬͯ͠Δ ($inQuery) relatedTo: ΦϒδΣΫτID͕ࢦఆ͞Εͨ਌ͷϦϨʔγϣϯʹඥͮ͘ࢠ
  27. 2VFSZpOEͱpSTU w pOE ͢΂ͯฦ͢ w pSTU ࠷ॳͷͭͷΈ use Ncmb\Query; $className

    = 'example'; $query = new Query($className); $query->equalTo('category', 5); $obj = $query->first(); echo $obj->name;
  28. Φϖϥϯυ"EE use Ncmb\Object; $obj = new Object('example'); $obj->setArray('arrKey', [1, 2,

    3]); $obj->save(); $obj->add('arrKey', [10, 11]); $obj->save(); w ഑ྻʹ஋Λ௥Ճ
  29. Φϖϥϯυ"EE6OJRVF use Ncmb\Object; $obj = new Object('example'); $obj->setArray('arrKey', [1, 2,

    3]); $obj->save(); $obj->addUnique('arrKey', [2, 3, 4]); $obj->save(); w ഑ྻʹ஋Λ௥Ճ ॏෳ͸௥Ճ͠ͳ͍
  30. Φϖϥϯυ3FNPWF use Ncmb\Object; $obj = new Object('example'); $obj->setArray('arrKey', [1, 2,

    3]); $obj->save(); $obj->remove('arrKey', [2, 3]); $obj->save(); w ഑ྻ͔Β஋Λ࡟আ
  31. ಈ࡞ઃఆͷ্ॻ͖ྫ w ϩʔΧϧͷϙʔτʹϑΣΠΫαʔόΛཱͯͨྫ use Ncmb\NCMB; $appKey = 'xxxxxxxxxxxxxxx'; $clientKey =

    'yyyyyyyyyyyyyyyy'; $config = [ 'apiHost' => 'localhost', 'scriptApiHost' => 'localhost', 'port' => 10080, 'protocol' => 'http', ]; NCMB::initialize($appKey, $clientKey, $config);
  32. ߜΓࠐΈ഑৴ w ഑৴୺຤ొ࿥࣌ʹɺډॅ஍ͱ೥୅͕ηοτ͞Ε͍ͯΔͱ͢Δ use Ncmb\Push; $options = [ 'target' =>

    ['android', 'ios'], 'searchCondition' => [ 'place' => 'Kanagawa', 'age' => '30s', ], : ]; $pushId = Push::send($options);
  33. ݸผ഑৴ w ഑৴୺຤ొ࿥࣌ʹɺVTFS*E͕ొ࿥͞Ε͍ͯΔͱ͢Δ use Ncmb\Push; $options = [ 'target' =>

    ['android', 'ios'], 'searchCondition' => [ 'userId' => 'xxxxxxxxx', ], : ]; $pushId = Push::send($options);
  34. &ODPEFS w 1)1ͷม਺Λ/$.#3&45"1*ͷσʔλදݱʹม׵ ͢Δ w &ODPEBCMF*OUBSGBDFΛ࣮૷͍ͯͨ͠ΒFODPEF ϝ ιουΛݺͼग़͢ use Ncmb\Encoder;

    $encoded = Encoder::encode(new DateTime()); var_dump($encoded); // array(2) { // ["__type"]=> // string(4) "Date" // ["iso"]=> // string(24) "2017-08-24T09:01:39.000Z" // }