Skip to content

Commit 999d1e4

Browse files
author
twalder-docnet
committed
CHANGE default indexing from FALSE to TRUE. Now easier to get started, more consistent with https://cloud.google.com/datastore/docs/concepts/indexes. Fixes #10, closes #17
1 parent ea51d27 commit 999d1e4

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

examples/dynamic_schema.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Create a single record in GDS
4+
*
5+
* @author Tom Walder <[email protected]>
6+
*/
7+
require_once('boilerplate.php');
8+
9+
$obj_store = new GDS\Store($obj_gateway, 'Friend');
10+
11+
// So now create a simple Model object
12+
$obj_charlie = new GDS\Entity();
13+
$obj_charlie->name = 'Charlie';
14+
$obj_charlie->age = 26;
15+
$obj_charlie->height = 120;
16+
$obj_store->upsert($obj_charlie);
17+
echo "Created: ", $obj_charlie->getKeyId(), PHP_EOL;
18+
19+
// So now create a simple Model object
20+
$obj_max = new GDS\Entity();
21+
$obj_max->name = 'Max';
22+
$obj_max->age = 26;
23+
$obj_max->height = 122;
24+
$obj_store->upsert($obj_max);
25+
echo "Created: ", $obj_max->getKeyId(), PHP_EOL;
26+
27+
echo "Query 1:", PHP_EOL;
28+
foreach($obj_store->fetchAll("SELECT * FROM Friend WHERE age = 26") as $obj_result) {
29+
echo "Got: ", $obj_result->getKeyId(), ' ' , $obj_result->name, PHP_EOL;
30+
}
31+
32+
echo "Query 2:", PHP_EOL;
33+
foreach($obj_store->fetchAll("SELECT * FROM Friend WHERE age = 26 AND height = 122") as $obj_result) {
34+
echo "Got: ", $obj_result->getKeyId(), ' ' , $obj_result->name, PHP_EOL;
35+
}

src/GDS/Mapper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ private function createProperties(Entity $obj_gds_entity)
196196
$int_dynamic_type = Schema::PROPERTY_DATETIME;
197197
break;
198198
}
199+
// No break on purpose - 'other' objects will be cast to string ;(
199200

200201
case 'resource':
201202
case 'NULL':
@@ -205,7 +206,7 @@ private function createProperties(Entity $obj_gds_entity)
205206
$int_dynamic_type = Schema::PROPERTY_STRING;
206207
$mix_value = (string)$mix_value;
207208
}
208-
$arr_property_map[$str_field_name] = $this->createProperty(['type' => $int_dynamic_type, 'index' => FALSE], $mix_value);
209+
$arr_property_map[$str_field_name] = $this->createProperty(['type' => $int_dynamic_type, 'index' => TRUE], $mix_value);
209210
}
210211
}
211212
return $arr_property_map;
@@ -223,9 +224,9 @@ private function createProperty(array $arr_field_def, $mix_value)
223224
$obj_property = new \Google_Service_Datastore_Property();
224225

225226
// Indexed?
226-
$bol_index = FALSE;
227-
if(isset($arr_field_def['index']) && TRUE === $arr_field_def['index']) {
228-
$bol_index = TRUE;
227+
$bol_index = TRUE;
228+
if(isset($arr_field_def['index']) && FALSE === $arr_field_def['index']) {
229+
$bol_index = FALSE;
229230
}
230231
$obj_property->setIndexed($bol_index);
231232

src/GDS/Schema.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct($str_kind)
6969
* @param bool $bol_index
7070
* @return $this
7171
*/
72-
public function addProperty($str_name, $int_type = self::PROPERTY_STRING, $bol_index = FALSE)
72+
public function addProperty($str_name, $int_type = self::PROPERTY_STRING, $bol_index = TRUE)
7373
{
7474
$this->arr_defined_properties[$str_name] = [
7575
'type' => $int_type,
@@ -85,7 +85,7 @@ public function addProperty($str_name, $int_type = self::PROPERTY_STRING, $bol_i
8585
* @param bool $bol_index
8686
* @return Schema
8787
*/
88-
public function addString($str_name, $bol_index = FALSE)
88+
public function addString($str_name, $bol_index = TRUE)
8989
{
9090
return $this->addProperty($str_name, self::PROPERTY_STRING, $bol_index);
9191
}
@@ -97,7 +97,7 @@ public function addString($str_name, $bol_index = FALSE)
9797
* @param bool $bol_index
9898
* @return Schema
9999
*/
100-
public function addInteger($str_name, $bol_index = FALSE)
100+
public function addInteger($str_name, $bol_index = TRUE)
101101
{
102102
return $this->addProperty($str_name, self::PROPERTY_INTEGER, $bol_index);
103103
}
@@ -109,7 +109,7 @@ public function addInteger($str_name, $bol_index = FALSE)
109109
* @param bool $bol_index
110110
* @return Schema
111111
*/
112-
public function addDatetime($str_name, $bol_index = FALSE)
112+
public function addDatetime($str_name, $bol_index = TRUE)
113113
{
114114
return $this->addProperty($str_name, self::PROPERTY_DATETIME, $bol_index);
115115
}
@@ -121,7 +121,7 @@ public function addDatetime($str_name, $bol_index = FALSE)
121121
* @param bool $bol_index
122122
* @return Schema
123123
*/
124-
public function addFloat($str_name, $bol_index = FALSE)
124+
public function addFloat($str_name, $bol_index = TRUE)
125125
{
126126
return $this->addProperty($str_name, self::PROPERTY_FLOAT, $bol_index);
127127
}
@@ -133,7 +133,7 @@ public function addFloat($str_name, $bol_index = FALSE)
133133
* @param bool $bol_index
134134
* @return Schema
135135
*/
136-
public function addBoolean($str_name, $bol_index = FALSE)
136+
public function addBoolean($str_name, $bol_index = TRUE)
137137
{
138138
return $this->addProperty($str_name, self::PROPERTY_BOOLEAN, $bol_index);
139139
}
@@ -145,7 +145,7 @@ public function addBoolean($str_name, $bol_index = FALSE)
145145
* @param bool $bol_index
146146
* @return Schema
147147
*/
148-
public function addStringList($str_name, $bol_index = FALSE)
148+
public function addStringList($str_name, $bol_index = TRUE)
149149
{
150150
return $this->addProperty($str_name, self::PROPERTY_STRING_LIST, $bol_index);
151151
}

0 commit comments

Comments
 (0)