ajax call: class Silverstripe/ORM/DB non found

Silverstripe Version: 4.1

Question:
In a js file (inside public/resources/themes/my_thema/javascript) I make an ajax call according to the jquery standard.
the php file of the call I put it in public/class/myfile.php
Here a call a query to Silverstripe db so to the beggining of the file I put
use Silverstripe/ORM/DB

Details of your query go here

// Include any relevant code. If you have a lot of code, link to a gist instead.

Can you provide a bit more info?

Are you calling that file directly? (eg. http://example.com/class/myfile.php) If so, then you’re bypassing the Silverstripe core code, so things like the class autoloader, etc. won’t work.

Assuming the above, you’d be better off adding a method to one of your page controllers to handle the request, and set up the routing accordingly. (Have a look here for details: https://docs.silverstripe.org/en/4/developer_guides/controllers/)

If you’re not calling it directly, can you provide a bit more info about where it is in your code base, what the file contains, etc. and we might be able to help.

A post was merged into an existing topic: Need help, new to this program

this is my code:

in public\resources\themes\mytheme\javascript\lista.js
function editTitle() {
var lista_id = getUrlParameter(‘id’);
if (lista_id!=’’) {
$(’#titololista’).blur(function(){
//alert(‘aa’);
$.ajax({
type: ‘POST’,
url: “public/class/test.php”,
dataType: ‘json’,
data: { //dati passati al php
lista_id: lista_id,},
success: function(result) {alert(‘ok’);},
async: true
});});
}}

in public\class\test.php

<?php use SilverStripe\ORM\DB; //in vendor folder function article($param){ $query="select * from table"; $query=DB::query($query); ...... } error: Class SilverStripe\ORM\DB non found

Hi,

OK, so it looks like the JS is making a request directly to the PHP file. My advice would be to move that functionality into the Silverstripe codebase, since you want to be able to gain access to Silverstripe dataobjects, etc. Have a look at the docs link to controllers I posted earlier. Depending on exactly what you’re looking to do, it could be as simple as adding a new method to your PageController class to handle the request.

Can’t find a solution using the code I’m using instead?
I tried to move the ORM functions into the public vendor area but it doesn’t work either

I’m sure there is a solution using the structure you currently have… you could probably include and bootstrap all the functions you need… what I gave you is the best solution as I see it, that should work and retain all the flexibility and security that is provided by default when using the proper framework.

Is there a reason you don’t want to do it using the way I suggested?

1 Like

As @Tim said, it really sounds like you’re approaching this the wrong way (sorry if that sounds offensive) and this will probably make your work really difficult. The components of the Silverstripe framework generally aren’t designed to be used discretely (as opposed to a library of independent functions like LoDash) they really assume that the whole request goes through Silverstripe.

If you’re new to Silverstripe development it’s definitely worth running through the lessons: https://www.silverstripe.org/learn/lessons/v4/

BTW, I noticed that your php file is in the public/class/ directory, but the intention of the public folder in Silverstripe 4 is that it should only include static resources such as css and images. Having PHP files in your public directory may be a security risk.

Thanks for the help, the explanations you have given me are clear. I try to change the code using the silverstripe rules.