[Solved] Help with querybuilders GetSelect needed
I want to create a view in 2.0.18.8 and found that instruction:
$View = $Database->Select('rp.*') ->Select('p.Name', '', 'Permission') ->From('RolePermission rp') ->Join('Permission p', 'rp.PermissionID = p.PermissionID') ->GetSelect(); $Construct->View('vw_RolePermission', $View);
So I try:
$Database = Gdn::Database(); $Sql = $Database->Select('*')->From('User')->GetSelect();
But I receive that error: Fatal error: Call to undefined method Gdn_Database::Select()
How can I use the GetSelect function to get back the SQL I need for creating the view?
Best Answer
-
x00 MVP
That documentation is a bit outdated to be honest, and doesn't make a lot of sense. I think that was done when V2 was still in the conceptual stage.
You are mixing up the database object with the query builder. they are related but different.
You'd want
Gdn::SQL()
Also
GetSelect()
is called internally, you rarely need to return the the select statement string, instead you can execute the query byGet()
, and return a result set.Note: most queries especially if you are doing a lot of them are called in the context of a model. You have existing models and you can create them.
In this case you already have a User model, with plenty of useful methods, whatsmore it is optimised with caching.
grep is your friend.
7
Answers
That documentation is a bit outdated to be honest, and doesn't make a lot of sense. I think that was done when V2 was still in the conceptual stage.
You are mixing up the database object with the query builder. they are related but different.
You'd want
Gdn::SQL()
Also
GetSelect()
is called internally, you rarely need to return the the select statement string, instead you can execute the query byGet()
, and return a result set.Note: most queries especially if you are doing a lot of them are called in the context of a model. You have existing models and you can create them.
In this case you already have a User model, with plenty of useful methods, whatsmore it is optimised with caching.
grep is your friend.
@x00: but GetSelect is the most Vanilla way to create a view, isn't it? I'm supposed to pass in a sql into the view function and I think it's better to build it and not to code it manually.
My example was just an example. I want to join two (huge) tables I create in a plugin (and I just can wait to release it!)
Are you wanting to create a mySQL View? This is not the same as a view as in a controller view that renders a page.
http://dev.mysql.com/doc/refman/5.0/en/views.html
grep is your friend.
Yes, I've needed a view in the database and your first response was a perfect fit to my question, thanks for that!
My problem from the opening posting is solved and I've already tested it.
But I was just wondering if there is another way of creating a (database) view than the above mentioned, because you said that GetSelect is rarely used and I might use Get instead. I think you were meaning that more general, but because I want to learn, I thought I ask again to be sure ;-)
What I'm saying is very rarely are query strings directly needed outside this abstraction.
Instead of database views there are also object caching options.
grep is your friend.