Jump to content

Why is there such a disconnect between databases and Pages?


gbstn10

Recommended Posts

Why is it so difficult/impossible to display the data from a databases' custom field in Pages? I have to make a databases template whenever I want to display anything held in a database. This isn't always suitable; sometimes it's overkill and sometimes it still doesn't allow me to do what I want. 

All I want to be able to do is, target a specific database entry held in a custom field and display it on a custom page. I have a list of people, I want to get specifics from my database and display them on various custom pages. I can't do that. Why not? The current setup is really limiting what I can do, and it's forcing me to hard-code my pages with data in each page, that I'll have to go through one-by-one to edit in the future; it's particularly frustrating as there's a lot of data and it would be a lot easier to edit one entry in a database rather than 50 entries in 5 pages.

To me this seems like it should be something basic with the software, so perhaps I'm not understanding how to do it. Is there a way to display targeted custom fields from the databases?

@Charles @Lindy

Link to comment
Share on other sites

Can you explain the actual use case better? I’m not sure I get it.

The usual way to show Pages database data outside of the actual database templates (e.g. on a page) would be through creating a block and putting in on the page. That is quite flexible and doesn’t require “hard-coded pages”. 

Link to comment
Share on other sites

That's... not something they are going to provide by default. I would ask that you reconsider your data design, I think I'd be less than pleased to have to edit a massive form on a single record that could affect multiple pages, and so will you in the long term.

Regardless, what you ask is still possible:

{{$databaseId = 1;}}
{{$recordId = 2;}}
{{$recordsClass = '\\IPS\\cms\\Records' . $databaseId;}}
{{$record = $recordsClass::load($recordId);}}
{$record->customFieldDisplayByKey('myFieldKey')|raw}

 

Link to comment
Share on other sites

That's... not something they are going to provide by default. I would ask that you reconsider your data design, I think I'd be less than pleased to have to edit a massive form on a single record that could affect multiple pages, and so will you in the long term.

Regardless, what you ask is still possible:

{{$databaseId = 1;}}
{{$recordId = 2;}}
{{$recordsClass = '\\IPS\\cms\\Records' . $databaseId;}}
{{$record = $recordsClass::load($recordId);}}
{$record->customFieldDisplayByKey('myFieldKey')|raw}

 

My design is not that complex. Something like below (obviously styled)

<ul>
<li>{$record->customFieldDisplayByKey('name')|raw}</li>
<li>{$record->customFieldDisplayByKey('age')|raw}</li>
<li>{$record->customFieldDisplayByKey('DOB')|raw}</li>
</ul>

As far as I see it, it's pretty simple; IPS offer a database, I want to store details about people in that database (name, age, DOB, etc) and then display that stored data on a custom page. Both databases and the custom pages use the same IPS add-on (Pages), yet I am unable to do that, which I think is crazy. There should be some basic code like this that allows me to simply insert what I want into the page…

<ul>
<li>{$database('3')->customFieldDisplayByKey('name')|raw}</li>
<li>{$database('3')->customFieldDisplayByKey('age')|raw}</li>
<li>{$database('3')->customFieldDisplayByKey('DOB')|raw}</li>
</ul>

Right now I have to use templates to display everything in my database, which completely restricts what you can do with the databases via the ACP.

 

** With regards to your code here…

{{$databaseId = 1;}}
{{$recordId = 2;}}
{{$recordsClass = '\\IPS\\cms\\Records' . $databaseId;}}
{{$record = $recordsClass::load($recordId);}}
{$record->customFieldDisplayByKey('myFieldKey')|raw}

… I couldn't get this working in a page. Perhaps I'm approaching it incorrectly. What I wanted to do was display 3 custom fields (name, age, DOB) for multiple people, but not everyone in my database. Do you know of a way to make that work or is it as I was discussing – not possible with the current setup?

Link to comment
Share on other sites

As far as I see it, it's pretty simple; IPS offer a database, I want to store details about people in that database (name, age, DOB, etc) and then display that stored data on a custom page. Both databases and the custom pages use the same IPS add-on (Pages), yet I am unable to do that, which I think is crazy. There should be some basic code like this that allows me to simply insert what I want into the page…

<ul>
<li>{$database('3')->customFieldDisplayByKey('name')|raw}</li>
<li>{$database('3')->customFieldDisplayByKey('age')|raw}</li>
<li>{$database('3')->customFieldDisplayByKey('DOB')|raw}</li>
</ul>

Right now I have to use templates to display everything in my database, which completely restricts what you can do with the databases via the ACP.

... Ok, do you really, really, expect the core to go out and retrieve the value of every record in the database and list them based on such a call? How would that code possibly work?(I am honestly wondering on this one, apologies for bluntness, by your wording one would think you would be expecting an array/iterator as the result, yet no looping suggests otherwise, so I'm lost...)

… I couldn't get this working in a page. Perhaps I'm approaching it incorrectly. What I wanted to do was display 3 custom fields (name, age, DOB) for multiple people, but not everyone in my database. Do you know of a way to make that work or is it as I was discussing – not possible with the current setup?

I gave the most basic of code, that would load a given record from a given database and display a given field from that record. While what you ask is still indeed possible, it does however  reach into the area of utterly custom. Which is why I can't see IPS providing an easy way to access such by default. Selecting out specific records by their primary_id_field is alone an odd request I can't see a widget/feed being able to fulfill, much less the type of expectations you seem to have of the app code.... I think by your wording you are maybe looking for getItemsWithPermission, but still feel like that could be a bad assumption.

You are going about a mile outside the things the app will *help* you do, while much remains possible, this is custom code, really. The standard database usage is embedded on a page replete with urls, follows, comments etc. Misusing them to do other things requires a developer.

Link to comment
Share on other sites

 

... Ok, do you really, really, expect the core to go out and retrieve the value of every record in the database and list them based on such a call? How would that code possibly work?(I am honestly wondering on this one, apologies for bluntness, by your wording one would think you would be expecting an array/iterator as the result, yet no looping suggests otherwise, so I'm lost...)

I didn't really think the specifics of the code through, I just gave a hurried example of what I would like to be able to do. It would be more like this…

{$database=('3')->$recordID=('12')->customFieldDisplayByKey('name')|raw}

The code may not be correct, but you get the idea. It would return the 'name' entry for the 12th record in the 3rd database. With something like that I could display a customised table of people and show only the people/details I want listed. More importantly, I could use the same database on multiple pages. Right now I have to make a database for every 'section' I want to create. If I could tap into the database, I'd have one focal point feeding various customised pages, instead of multiple databases feeding multiple templates.

I gave the most basic of code, that would load a given record from a given database and display a given field from that record. While what you ask is still indeed possible, it does however  reach into the area of utterly custom. Which is why I can't see IPS providing an easy way to access such by default. Selecting out specific records by their primary_id_field is alone an odd request I can't see a widget/feed being able to fulfill, much less the type of expectations you seem to have of the app code.... I think by your wording you are maybe looking for getItemsWithPermission, but still feel like that could be a bad assumption.

You are going about a mile outside the things the app will *help* you do, while much remains possible, this is custom code, really.

If you have any idea what I should be doing, then I'd appreciate any help.

Personally, I don't think I'm asking a lot of IPS. I think it makes sense to be able to use the data you need and not be limited to showing everything or nothing. I believe you should be able to display anything that is entered into your database. As it is now, I can't do that and I don't see why I am not able to.

Link to comment
Share on other sites

ok.... no idea why the code above doesn't suit, but I think I would be better off just answering the question posed in the topic title directly. It is because of the separation of concerns.

To put it quite literally, \IPS\cms\Databases sole responsibility is the data in cms_databases, no less, no more. \IPS\cms\Records1 is responsible for the records in cms_custom_database_1, no less, no more. \IPS\cms\Pages is responsible for the data in cms_pages, again, no more, no less. To have such a method baked into \IPS\cms\Databases would break encapsulation, and end up with there being far too many ways to access the data. THAT is why there is such a 'disconnect', and it's not going anywhere, it's good programming practice.

Link to comment
Share on other sites

 

Personally, I don't think I'm asking a lot of IPS. I think it makes sense to be able to use the data you need and not be limited to showing everything or nothing. 

If you expect this specific use tailored only to your specific site to be available as built in feature without requiring a single line of code in a template, then yes, you are asking for very much. 

If you are fine with a few lines of codes, then I don’t know what we are discussing. Marcher already posted the code you need. And yes, that code is for individual fields from individuals records. It doesn’t force you to show “everything or nothing”. 

Link to comment
Share on other sites

Guys, I got it working! I was – foolishly – forgetting to add start and end tags to my php to separate the blocks of code (hence the errors).

Thanks for all the comments/support, you've made my life 5000x easier :-)

Link to comment
Share on other sites

  • 1 month later...

Guys, I got it working! I was – foolishly – forgetting to add start and end tags to my php to separate the blocks of code (hence the errors).

Thanks for all the comments/support, you've made my life 5000x easier :-)

Hey @GabrielS , could you please post the exact code you ended up using, and where you used it? It would be really helpful.

Link to comment
Share on other sites

This works in the global template:

{{$databaseId = 2;}}
{{$recordId = 1;}}
{{$recordsClass = '\IPS\cms\Records' . $databaseId;}}
{{$record = $recordsClass::load($recordId);}}
{$record->customFieldDisplayByKey('field_key', 'raw')|raw}
oneliner:
{{$value = \IPS\cms\Records2::load(1)->customFieldDisplayByKey('field_key', 'raw');}}
{$value}

Does anyone know if it is possible to get the database ID using the database key? Or even better, could I load the the Records class without using the database ID at all?

Link to comment
Share on other sites

Hey @GabrielS , could you please post the exact code you ended up using, and where you used it? It would be really helpful.

You need to wrap the database/record you want to tap into in php tags.

<?php
    {{$databaseId = DATABASE_ID_NUMBER_HERE;}}
    {{$recordId = RECORD_ID_NUMBER_HERE;}}
    {{$recordsClass = '\\IPS\\cms\\Records' . $databaseId;}}
    {{$record = $recordsClass::load($recordId);}}
?>

Then you need to call the item you want.

{$record->customFieldDisplayByKey('NAME_OF_FIELD_ID', 'CHOOSE_LISTING_OR_DISPLAY_HERE')|raw}

That should be it.

*Make sure you replace only what is written in capitals.

 

Link to comment
Share on other sites

This works in the global template:

{{$databaseId = 2;}}
{{$recordId = 1;}}
{{$recordsClass = '\IPS\cms\Records' . $databaseId;}}
{{$record = $recordsClass::load($recordId);}}
{$record->customFieldDisplayByKey('field_key', 'raw')|raw}
oneliner:
{{$value = \IPS\cms\Records2::load(1)->customFieldDisplayByKey('field_key', 'raw');}}
{$value}

Does anyone know if it is possible to get the database ID using the database key? Or even better, could I load the the Records class without using the database ID at all?

From what I know, you need the database ID. If you go to the Pages menu, then hover over the database on the sidebar (it appears under 'Content') you can get the ID of the database (if your browser shows the URL when you hover over a link). Insert that at the top of the example I posted. Then you do the same for the RecordID. Once you have those two things, you just enter the custom field key that you set when setting up the custom field (you can find that by editing the custom field itself).

Link to comment
Share on other sites

From what I know, you need the database ID. If you go to the Pages menu, then hover over the database on the sidebar (it appears under 'Content') you can get the ID of the database (if your browser shows the URL when you hover over a link). Insert that at the top of the example I posted. Then you do the same for the RecordID. Once you have those two things, you just enter the custom field key that you set when setting up the custom field (you can find that by editing the custom field itself).

Thanks! I was hoping I could use the database key set in the settings of the database, but I guess that using the ID is fine for the time being.

Link to comment
Share on other sites

Thanks! I was hoping I could use the database key set in the settings of the database, but I guess that using the ID is fine for the time being.

You'd be best to ask someone like @Charles if that is possible. I really couldn't say definitively. I only know how to get the data via the method I posted :)

Link to comment
Share on other sites

  • 3 years later...
On 7/4/2015 at 3:58 PM, Marcher Technologies said:

That's... not something they are going to provide by default. I would ask that you reconsider your data design, I think I'd be less than pleased to have to edit a massive form on a single record that could affect multiple pages, and so will you in the long term.

Regardless, what you ask is still possible:


{{$databaseId = 1;}}
{{$recordId = 2;}}
{{$recordsClass = '\\IPS\\cms\\Records' . $databaseId;}}
{{$record = $recordsClass::load($recordId);}}
{$record->customFieldDisplayByKey('myFieldKey')|raw}

 

Sorry for bringing an old thread back from the dead. However, I figured this might help some other guys in the same situation. I am not trying to load a specific record. Instead, I want to load all records from a specific database. I had hoped for something like:

{{$databaseId = 1;}}

{{$rows = '\\IPS\\cms\\Databases'::load($databaseId);}}

but I guess that would have been too easy. Writing a SQL query to dump all the records is easy enough but, after that, you have to go through the process of field matching, etc... Is there an easy way to accomplish what I want to do? Thanks!

Link to comment
Share on other sites

\\IPS\\cms\\Databases'::load($databaseId)

will load a specific database!

If you want records, you'll need to use the record class instead of the database class.

{{$rows = \IPS\cms\Records1::getItemsWithPermission();}}

{{foreach $rows as $row}}
{$row->url()}
{{endforeach}}

 

Link to comment
Share on other sites

45 minutes ago, Daniel F said:

\\IPS\\cms\\Databases'::load($databaseId)

will load a specific database!

If you want records, you'll need to use the record class instead of the database class.


{{$rows = \IPS\cms\Records1::getItemsWithPermission();}}

{{foreach $rows as $row}}
{$row->url()}
{{endforeach}}

 

Fantastic! Just what I needed. Thanks!

Link to comment
Share on other sites

  • 1 month later...
On 6/25/2019 at 4:24 AM, Daniel F said:

\\IPS\\cms\\Databases'::load($databaseId)

will load a specific database!

If you want records, you'll need to use the record class instead of the database class.


{{$rows = \IPS\cms\Records1::getItemsWithPermission();}}

{{foreach $rows as $row}}
{$row->url()}
{{endforeach}}

 

 

I'm also trying to pull all records from a database, but when I use the above code, I only get the last 10 records from my database.

I've been trying to figure this out for a few days and haven't had any luck so far. Any help would be greatly appreciated.

Link to comment
Share on other sites

\IPS\cms\Records1::getItemsWithPermission();

This method accepts parameters to manipulate the results (i.e. one of the parameters allows you to change how many results are returned).

Please post in the appropriate peer assistance forums though if you need help writing custom code.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...