PDA

View Full Version : Creating a Grid with Data from DB using PHP



reynierpm
8 Jul 2009, 2:36 PM
I'm a Symfony[1] programmer but I want to use ExtJS for Views. This is not a problem at all but now I have my first doubts. Because Symfony use MVC pattern I have a model class wich have a method to return some data (using Doctrine ORM) from DB (PostgreSQL). I want to show that data in a Grid and with Pagination support.

I'm looking the API Doc for Grid and read that every Grid Panel needs a store wich is the Data to show (correct me if I'm wrong). Using this enviroment: Symfony + ORM + Model, how I can do this? Any tip? Any tutorial?

[1] http://www.symfony-project.org
Cheers and thanks in advance

mjlecomte
8 Jul 2009, 2:43 PM
It's not really any different than any other serverside technology. Your backend sends to the client a data packet. That data packet is typically XML or JSON. So you just need to figure out how to send JSON with your backend. Everything else on the client is server side agnostic (it doesn't care what your backend is), you just provide the data in XML or JSON. There's a few threads around for other people using MVC type frameworks, but really your issue should just be with your backend framework and using ajax.

mystix
8 Jul 2009, 5:54 PM
you might want to check out the extjs plugin for symfony by @lvanderee:
http://extjs.com/forum/showthread.php?p=333730

reynierpm
9 Jul 2009, 7:59 AM
Hi:
Searching on "Learning Center" I found this: http://extjs.com/learn/Tutorial:Grid_PHP_SQL_Part1 and I'm trying to follow every step to learn how to use Grids with PHP. See that code build in Symfony:


static public function getLatestJSON() {
$last1day = date('Y-m-d H:i:s', time() - 86400);
$query = Doctrine_Query::create()
->select('j.*, (j.endtime - j.starttime) as duration, s.jobstatuslong, c.name, p.name, f.*')
->from('Job j')
->leftJoin('j.Status s')
->leftJoin('j.Client c')
->leftJoin('j.Pool p')
->leftJoin('j.Fileset f')
->where("j.endtime < ?", $last1day)
->addWhere("j.jobstatus IN ('T', 'E', 'e', 'f', 'A')")
->orderBy('j.starttime, j.jobid')
->limit(2)
->addGroupBy('j.jobid, j.job, j.name, j.type, j.level, j.clientid, j.jobstatus, j.jobtdate, j.volsessionid,
j.volsessiontime, j.jobfiles, j.jobbytes, j.readbytes, j.joberrors, j.jobmissingfiles, j.poolid,
j.filesetid, j.purgedfiles, j.hasbase, j.priorjobid, j.schedtime, j.realendtime, s.jobstatus,
s.jobstatuslong, c.clientid, c.name, p.poolid, p.name, f.filesetid, f.fileset, f.md5, f.createtime,
j.starttime, j.endtime');
$results = $query->fetchArray();
$encode_results = json_encode($results);
}

This return the data encoded with JSON is as Ext needs. See below:


[{"jobid":1,"job":"SP_F03_SIGESPRO-FD.2009-06-17_16.42.32_12","name":"SP_F03_SIGESPRO-FD","type":"B","level":"F","clientid":1,"jobstatus":"T","jobtdate":"1245271414","volsessionid":1,"volsessiontime":1245271252,"jobfiles":3117,"jobbytes":"502427343","readbytes":"522339424","joberrors":0,"jobmissingfiles":0,"poolid":3,"filesetid":1,"purgedfiles":0,"hasbase":0,"priorjobid":0,"schedtime":"2009-06-17 16:42:32","starttime":"2009-06-17 16:42:34","endtime":"2009-06-17 16:43:34","realendtime":"2009-06-17 16:43:34","duration":"00:01:00","Status":{"jobstatus":"T","jobstatuslong":"Completed successfully"},"Client":{"clientid":1,"name":"salvasprod_f03_sigespro-fd"},"Pool":{"poolid":3,"name":"SP_F03_SIGESPRO_Pool"},"Fileset":{"filesetid":1,"fileset":"SP_F03_SIGESPRO-FS","md5":"+C\/n\/9AbeC+8p9\/CA3ArqB","createtime":"2009-06-17 16:42:32"}},{"jobid":2,"job":"SP_F03_Fiscalia-FD.2009-06-17_16.43.19_20","name":"SP_F03_Fiscalia-FD","type":"B","level":"F","clientid":2,"jobstatus":"T","jobtdate":"1245271421","volsessionid":2,"volsessiontime":1245271252,"jobfiles":2001,"jobbytes":"3238894","readbytes":"7188676","joberrors":1,"jobmissingfiles":0,"poolid":4,"filesetid":2,"purgedfiles":0,"hasbase":0,"priorjobid":0,"schedtime":"2009-06-17 16:43:19","starttime":"2009-06-17 16:43:36","endtime":"2009-06-17 16:43:41","realendtime":"2009-06-17 16:43:41","duration":"00:00:05","Status":{"jobstatus":"T","jobstatuslong":"Completed successfully"},"Client":{"clientid":2,"name":"salvasprod_f3_fiscalia-fd"},"Pool":{"poolid":4,"name":"SP_F03_Fiscalia_Pool"},"Fileset":{"filesetid":2,"fileset":"SP_F03_Fiscalia-FS","md5":"XXd3v6\/Ub\/+Vx8N23S+\/ID","createtime":"2009-06-17 16:43:19"}}]


At this point every is fine but I have my doubts. In the definition of Grid I can see this:


proxy : new Ext.data.HttpProxy( {
url : 'database.php', // File to connect to
method : 'POST'
}),

But in my case is not just a file called database.php is a class wich contains methods. See below:


public function executeIndexJSON(sfWebRequest $request) {
Job::getLatestJSON(); // Testing Purpose (function presented above)
}

So my first question is: how I call this method/page from Ext? Normally I call it as http://sfwebacula/frontend_dev.php/main/IndexJSON but doesn't know how to call from Ext. Any help?

Cheers and thanks

mjlecomte
9 Jul 2009, 8:05 AM
Sounds like you want Ext.Direct. Read the blog entry about it.

reynierpm
9 Jul 2009, 8:07 AM
Sounds like you want Ext.Direct. Read the blog entry about it.
Where is located this entry? I can't find it
Cheers

mjlecomte
9 Jul 2009, 8:14 AM
http://extjs.com/blog/2009/05/13/introducing-ext-direct/
http://extjs.com/blog/2009/06/03/ext-js-30-rc2-release-stable-robust-and-enhanced/

reynierpm
9 Jul 2009, 8:36 AM
Ok, I read every you leave me but I'm not clear yet. I not understand your point here with Ext Direct. Can you tell me what Ext Direct can do for me?

Cheers

mjlecomte
9 Jul 2009, 9:41 AM
You indicated you wanted to be able to call a class method directly, so I figured that's what you were after ... RPC type control.

There's an example in the demos that shows a grid being populated using Direct. I believe the blog links to some server side stacks (including php and symfony even IIRC).

It's not your only solution, just a way to go. Ext.Direct is relatively new, introduced with Ext 3.0.0.