Datas

Les datas sont stockées dans des "tables" dans des fichiers à plat en YAML.

Les fichiers sont dans le dossier de la config path_data et ont le nom suivant table.<nom_de_table>.yaml

Exemple du fichier fourni dans cette démo table.persons.yaml :

#
# Example of "simple" table of persons
# YAML is easy to maintain
#

- id: 1
  name: Andromache
  gender: Female
  language: Spanish
- id: 2
  name: Karen
  gender: Female
  language: Filipino
- id: 3
  name: Weber
  gender: Male
  language: Albanian
- id: 4
  name: Kiel
  gender: Male
  language: Haitian Creole
- id: 5
  name: Gottfried
  gender: Male
  language: Filipino
- id: 6
  name: Addy
  gender: Female
  language: Persian
- id: 7
  name: Robby
  gender: Female
  language: Hiri Motu
- id: 8
  name: Agata
  gender: Female
  language: Nepali
- id: 9
  name: Inness
  gender: Male
  language: Filipino
- id: 10
  name: Lindon
  gender: Male
  language: Somali
- id: 11
  name: Antonetta
  gender: Female
  language: Tamil
- id: 12
  name: Erminia
  gender: Female
  language: Burmese
- id: 13
  name: Dunn
  gender: Male
  language: Hungarian
- id: 14
  name: Chariot
  gender: Male
  language: Aymara
- id: 15
  name: Nappie
  gender: Male
  language: Lithuanian
- id: 16
  name: Matias
  gender: Male
  language: Ndebele
- id: 17
  name: Carmela
  gender: Female
  language: Icelandic
- id: 18
  name: Colene
  gender: Female
  language: Luxembourgish
- id: 19
  name: Rayna
  gender: Female
  language: Dutch
- id: 20
  name: Cart
  gender: Male
  language: Macedonian
- id: 21
  name: Cecelia
  gender: Female
  language: Filipino
- id: 22
  name: Catriona
  gender: Female
  language: Pashto
- id: 23
  name: Conrade
  gender: Male
  language: Kannada
- id: 24
  name: Terencio
  gender: Male
  language: French
- id: 25
  name: Sarah
  gender: Female
  language: Marathi
- id: 26
  name: Patrizius
  gender: Male
  language: Fijian
- id: 27
  name: Bunni
  gender: Female
  language: Dhivehi
- id: 28
  name: Tillie
  gender: Female
  language: Finnish
- id: 29
  name: Nil
  gender: Male
  language: Swahili
- id: 30
  name: Salome
  gender: Female
  language: Hiri Motu
- id: 31
  name: Alexio
  gender: Male
  language: Papiamento
- id: 32
  name: Iggie
  gender: Male
  language: Montenegrin
- id: 33
  name: Jody
  gender: Male
  language: Gagauz
- id: 34
  name: Ingeborg
  gender: Female
  language: Icelandic
- id: 35
  name: Isak
  gender: Male
  language: Latvian
- id: 36
  name: Benyamin
  gender: Male
  language: Dhivehi
- id: 37
  name: Jemie
  gender: Female
  language: Oriya
- id: 38
  name: Kimbell
  gender: Male
  language: Tok Pisin
- id: 39
  name: Tallie
  gender: Female
  language: Oriya
- id: 40
  name: Leonore
  gender: Female
  language: Bulgarian
- id: 41
  name: Burr
  gender: Male
  language: New Zealand Sign Language
- id: 42
  name: Conni
  gender: Female
  language: Aymara
- id: 43
  name: Godfree
  gender: Male
  language: Italian
- id: 44
  name: Humfrid
  gender: Male
  language: Greek
- id: 45
  name: Care
  gender: Male
  language: Quechua
- id: 46
  name: Ambrosius
  gender: Male
  language: Bengali
- id: 47
  name: Eveleen
  gender: Female
  language: Chinese
- id: 48
  name: Jonie
  gender: Female
  language: Polish
- id: 49
  name: Robinetta
  gender: Female
  language: Guaraní
- id: 50
  name: Yulma
  gender: Male
  language: Montenegrin
- id: 51
  name: Chen
  gender: Male
  language: English
- id: 52
  name: Finn
  gender: Male
  language: Hebrew
- id: 53
  name: Floyd
  gender: Male
  language: Papiamento
- id: 54
  name: Umberto
  gender: Male
  language: Nepali
- id: 55
  name: Xerxes
  gender: Male
  language: Greek
- id: 56
  name: Inez
  gender: Female
  language: Dhivehi
- id: 57
  name: Jane
  gender: Female
  language: Northern Sotho
- id: 58
  name: Seka
  gender: Female
  language: Punjabi
- id: 59
  name: Addie
  gender: Male
  language: Kashmiri
- id: 60
  name: Nikolai
  gender: Male
  language: Pashto
- id: 61
  name: Tess
  gender: Female
  language: Bulgarian
- id: 62
  name: Ricoriki
  gender: Male
  language: Indonesian
- id: 63
  name: Cheston
  gender: Male
  language: Chinese
- id: 64
  name: Arny
  gender: Male
  language: Korean
- id: 65
  name: Emylee
  gender: Female
  language: Assamese
- id: 66
  name: Michal
  gender: Male
  language: Persian
- id: 67
  name: Mimi
  gender: Female
  language: Thai
- id: 68
  name: Onfre
  gender: Male
  language: Dari
- id: 69
  name: Davy
  gender: Male
  language: Icelandic
- id: 70
  name: Joey
  gender: Male
  language: Amharic
- id: 71
  name: Baily
  gender: Male
  language: Telugu
- id: 72
  name: Marcos
  gender: Male
  language: Ndebele
- id: 73
  name: Linus
  gender: Male
  language: Dutch
- id: 74
  name: Hill
  gender: Male
  language: Latvian
- id: 75
  name: Anet
  gender: Female
  language: Gagauz
- id: 76
  name: Hilton
  gender: Male
  language: Italian
- id: 77
  name: Kakalina
  gender: Female
  language: Tsonga
- id: 78
  name: Fransisco
  gender: Male
  language: Chinese
- id: 79
  name: Hyman
  gender: Male
  language: Arabic
- id: 80
  name: Bryan
  gender: Male
  language: Aymara
- id: 81
  name: Zollie
  gender: Male
  language: Dutch
- id: 82
  name: Daphna
  gender: Female
  language: Portuguese
- id: 83
  name: Jane
  gender: Female
  language: Dhivehi
- id: 84
  name: Germayne
  gender: Male
  language: Thai
- id: 85
  name: Violette
  gender: Female
  language: New Zealand Sign Language
- id: 86
  name: Ty
  gender: Male
  language: Kannada
- id: 87
  name: Hayward
  gender: Male
  language: Bosnian
- id: 88
  name: Tani
  gender: Female
  language: New Zealand Sign Language
- id: 89
  name: Hali
  gender: Female
  language: Pashto
- id: 90
  name: Elysee
  gender: Female
  language: Croatian
- id: 91
  name: Lita
  gender: Female
  language: Moldovan
- id: 92
  name: Therese
  gender: Female
  language: Arabic
- id: 93
  name: Bertie
  gender: Male
  language: Tetum
- id: 94
  name: Clayton
  gender: Male
  language: Kazakh
- id: 95
  name: Augustine
  gender: Male
  language: Romanian
- id: 96
  name: Orly
  gender: Female
  language: Korean
- id: 97
  name: Missie
  gender: Female
  language: Haitian Creole
- id: 98
  name: Clara
  gender: Female
  language: Dzongkha
- id: 99
  name: Martainn
  gender: Male
  language: Icelandic
- id: 100
  name: Cherice
  gender: Female
  language: Gagauz

Pour y accéder on utilise arnapou/pfdb qui permet de faire des recherches, des groupements, des tris et aussi de modifier les données.
La documentation est en ligne sur pfdb.arnapou.net : tout ce qui est faisable en code PHP est faisable dans twig à travers l'objet app.db.

Retrouver un élément à partir de son id

L'élément ayant pour id 5
{{ app.db.table('persons').get(5) | json_encode(constant('JSON_PRETTY_PRINT')) | raw }}

{
    "id": 5,
    "name": "Gottfried",
    "gender": "Male",
    "language": "Filipino"
}

Lister

5 lignes à partir de la dizième
{% for person in app.db.table('persons').find.limit(10, 5) %}

11 Antonetta Female Tamil
12 Erminia Female Burmese
13 Dunn Male Hungarian
14 Chariot Male Aymara
15 Nappie Male Lithuanian

Chercher et trier

Les noms contenant an puis trié par nom
{% for person in app.db.table('persons').find(app.db.expr.contains('name', 'an')).sort('name') %}

80 Bryan Male Aymara
78 Fransisco Male Chinese
79 Hyman Male Arabic
57 Jane Female Northern Sotho
83 Jane Female Dhivehi
88 Tani Female New Zealand Sign Language