summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/query.someproperty.of.type.number.md
blob: e3ea8cc5c146b1d8c5b8c2a2bd74f6a491fe2540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## Match distinct numeric property value

```php
{{#ask: [[NumericProperty::1111]]
 |?NumericProperty
}}
```

```php
// Create property instance
$property = new DIProperty( 'NumericProperty' );
$property->setPropertyTypeId( '_num' );

$dataItem = new DINumber( 1111 );

$dataValue = DataValueFactory::getInstance()->newDataValueByItem(
	$dataItem,
	$property
);
```

```php
// Create and store the SemanticData object in order to be able to match a value
$subject = new DIWikiPage( 'Foo', NS_MAIN )

$semanticData = new SemanticData( $subject );

$semanticData->addDataValue(
	$dataValue
);

ApplicationFactory::getInstance()->getStore()->updateData(
	$semanticData
);
```

```php
// Create a description that represents the condition
$descriptionFactory = new DescriptionFactory();

$description = $descriptionFactory->newSomeProperty(
	$property,
	$descriptionFactory->newValueDescription( $dataItem )
);

$propertyValue = DataValueFactory::getInstance()->newPropertyValueByLabel(
	'NumericProperty'
);

$description->addPrintRequest(
	new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
);

// Create query object
$query = new Query(
	$description
);

$query->querymode = Query::MODE_INSTANCES;
```

```php
// Try to match condition against the store
$queryResult = ApplicationFactory::getInstance()->getStore()->getQueryResult( $query );

// PHPUnit
$this->assertEquals(
	1,
	$queryResult->getCount()
);
```