- concrete5 Cookbook
- David Strack
- 344字
- 2021-08-13 16:15:54
Getting a Page object by its ID
In many situations, you will need to load a Page
object using its unique numerical ID from the database. As mentioned earlier, pages are actually extensions of the Collection
object, so you will actually be loading the page by its collection ID.
Getting ready
We will need to know the numerical ID of the page that you are attempting to load. Often this will be stored in other objects, perhaps even a custom entity that you have stored in the database. For example, say you have a recipes table in the database, and each recipe has its own page in the concrete5 website. You store the page ID for each recipe, so you can load the relevant Page
object whenever necessary.
How to do it...
First you will have to get the page ID for the Page
object that you are trying to load. For this example, we will just hard-code the ID of the page that we are loading. The steps are as follows:
- Open
/config/site_post.php
in your favorite code editor. - First, let's store the ID of the page we are loading into a variable. In real-world use, you will likely have this ID stored in a database table, or available from some other code that you have run. The ID of the home page in concrete5 defaults to
1
, so we will use that for our page's ID:$pageId = 1; // load the home page
- Now that we have the ID of the page that we need to load, use the
getByID
function to get thePage
object that we need:$page = Page::getByID($pageId);
- We can inspect the
$page
variable and verify that it contains aPage
object.my_debug($page);
How it works...
The page ID (also known as collection ID) is unique in the database, so calling the getByID
function will load the correct page data and return a Page
object that you can interact with.
See also
- The Getting the current Page object recipe
- The Getting a Page object by its path recipe
- Learning Scala Programming
- 精通JavaScript+jQuery:100%動(dòng)態(tài)網(wǎng)頁(yè)設(shè)計(jì)密碼
- INSTANT OpenCV Starter
- Python高級(jí)機(jī)器學(xué)習(xí)
- 你必須知道的204個(gè)Visual C++開(kāi)發(fā)問(wèn)題
- The DevOps 2.4 Toolkit
- The Complete Coding Interview Guide in Java
- jQuery Mobile移動(dòng)應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(第3版)
- App Inventor創(chuàng)意趣味編程進(jìn)階
- 編寫高質(zhì)量代碼:改善Objective-C程序的61個(gè)建議
- Hack與HHVM權(quán)威指南
- INSTANT Apache Hive Essentials How-to
- Android嵌入式系統(tǒng)程序開(kāi)發(fā)(基于Cortex-A8)
- Android應(yīng)用開(kāi)發(fā)攻略
- INSTANT PLC Programming with RSLogix 5000