php CRUD passing parameters - php

I'm working on a php service.
I'd like to pass an object with the parameters to use in the "where" clause and also the "order" and "limit" params.
I'm wondering whether there is a standard way/library to make up the query based on the parameters given, or whether I'll need to roll one.
I'm using ezsql to connect to mysql.
Any pointers, much appreciated.

Writing one yourself shouldn't be too hard. If you don't want to do so (DRY, right?), you may check out sites like phpclasses.org or use an abstraction layer from Doctrine (take a look at its QueryBuilder if it is what you need)

Another suggestion if you would like to use already existing code: Zend Framework has a "query builder", Zend_Db_Select.
This does not require the whole Zend Framework, but it does have some depedencies you will have to include.
Documentation: http://framework.zend.com/manual/en/zend.db.select.html

Related

Doctrine Query builder - how to select based on field inside JSON column

I have a table with a JSON column, it's type longtext (DC2Type:json). This table has an Entity in Doctrine ORM in my Symfony project. I would like to query based on fields inside the JSON column, using the Doctrine query builder I have in a variable $qb
How do I do this? Everything I found online says to install a 3rd-party package to enable this. Is there no way to just do it with Doctrine's query builder without installing another package?
One (maybe dumb) workaround I tried was to treat the column as a string, and do...
$qb->andWhere("my_data LIKE \"%id:\\\"1,%\"");
For example, if I wanted to query the JSON column my_data to find the blobs that contain id":1, in the string. This fails with a very strange syntax error, and isn't the right way to query a JSON field anyway. HOWEVER, doing the LIKE query directly in SQL client works the way I want, so I also don't know why this is failing in Doctrine.
EDIT: This is MySQL / MariaDB.
Doctrine Query Language is pretty limited. It covers only the most basic/common SQL functions, which is enough for like 99% use cases, but not all.
If you have a MariaDB version that natively supports JSON (so 10.2 or later) you can use native functions to work with the JSON data. (If you don't then your workaround is the only option regardless, with perhaps some additional filtering in the application).
To be able to use these functions in DQL you either need to define them yourself or indeed use a third party library like scienta/doctrine-json-functions (note that it has documentation for how to use it with Symfony, and it's really simple).
If you need just a single extra function and for some reason don't want the whole bundle, you could just copy that single class and use it as your own.
Alternatively you can forgo DQL and write SQL directly, but that way you can't hydrate into objects directly and use other Doctrine magic with the data. But it can be enough for simple use cases.

Sandboxing DQL queries in Doctrine2

Is there a simple way to somehow filter DQL statements to be able to use DQL conditions in url query parameter for instance in a REST API? Like users/?q="firstName='John' AND contacts IS EMPTY".
Something like sandboxing mode in twig. Maybe a custom query walker could be used but probably someone has solved it already?
This is what custom Repository classes are for. In your custom Repository class, you can create oneor more methods that accept parameters and use the querybuilder to build up your query depending on the url parameters passed. See the documentation for the QueryBuilder class for many examples.
So i created a custom AST Walker. Looks like it works. But it's not fully tested yet.
https://gist.github.com/maryo/6762610

PECL solr package issue

In solr PECL php package,to the solrQuery class we can add parameters using solrparam::set methods as name value pair.So Inorder to build a query we can use this SolrParams class.
I am just trying to figure out what are the use cases of all the methods in solrquery object.
like
***"SolrQuery::addFacetDateField — Maps to facet.date
SolrQuery::addFacetDateOther — Adds another facet.date.other parameter
SolrQuery::addFacetField — Adds another field to the facet
SolrQuery::addFacetQuery — Adds a facet query
SolrQuery::addField — Specifies which fields to return in the result
SolrQuery::addFilterQuery — Specifies a filter query
SolrQuery::addHighlightField — Maps to hl.fl"***
....etc.
We can simply use the solrparam to add parameters to the query, then what is the use of these.
Thanks
These methods were added to ease the use of Solr Functions, Unfortunately the current documentation misses a lot of use cases, that I'm currently working on.
It's much easier and more consistent to use methods for query functionalities like date facet without going into the Solr Documentation each time to pull parameter names. It's also less error prone.
For the time being, if you wish to learn more about these functionalities, you can check the Solr Wiki http://wiki.apache.org/solr/SimpleFacetParameters#facet.date.other
The SolrParams is the ancestor of SolrQuery, SolrQuery has much more features than the bare bones(SolrParams). In the documentation its passed for SolrClient::query() as the query method accepts argument of type SolrParams (which means SolrParams any of its descendents).
There is nothing special about those methods. You can use the API in both ways without any penalty. Using SolrQuery methods makes your code more explicit so it's better for readability I presume.

MySQL query builder PHP class

I am building application which needs to have OOP style MySQL query builder. I want to be able to flexibly build complex queries using only PHP and to get resulting query string for execution with my own database driver.
Does anyone know of a good standalone query builder for PHP? Please note that I don't need database driver I need bare MySQL query builder class (preferably written with camel style function and variable names).
Finally I took Doctrine ORM
and modified it little bit to build SQL instead of DQL.
This works very nice and it able to construct complex queries.
Edit:
You can find my final stable implementation in Stingle framework. Look at Db/QueryBuilder plugin.
DSQL - Query Builder for PHP is exactly what you're looking for, no dependencies and has MIT license:
$query = new atk4\dsql\Query();
$query ->table('employees')
->where('birth_date','1961-05-02')
->field('count(*)')
;
echo "Employees born on May 2, 1961: ".$query->getOne();
https://github.com/atk4/dsql
Documentation: http://dsql.readthedocs.io/
The PhpToolCase library has a stand-alone query builder tool that is quite easy and handy to use.
There is full join support aswell: http://phptoolcase.com/guides/ptc-qb-guide.html
And ya, it seems to be written with camel style function and variable names :)
There is one version of query builder there (LGPL licenced). I haven't used it, but you might want to take a look at it, if it suits your purposes:
http://code.google.com/p/mysql-query-builder/

How do you handle Zend_Paginator?

I was looking at Zend_Paginator in Zend Framework project using MVC, and it looks like an intersting tool.
It looks like we can give a select object as argument to the factory, it's interesting because it means that i should return a select object from my model, is it a good way to do ?
It seems to be a bit heavy to do this, since it won't be needed all the times...
I can also give an array, which could come as a result of my method model, but in the case where i have a lot of data, it could be complicated to retrieve all the data from my database each times.
How can i handle this ?
From the doc: http://framework.zend.com/manual/en/zend.paginator.usage.html
However, it is possible to directly supply a count or count query yourself. See the setRowCount() method in the DbSelect adapter for more information.
And
In the case of the Null adapter, in lieu of a data collection you must supply an item count to its constructor.
I would suggest doing the count yourself, and then manually setting it. That is, based upon the reading I just did. Also, the doc states that if you go the NULL route, you can provide an item-count (Integer) to the Paginator constructor instead - this seems a bit more reasonable than querying for the number with each request.
I have posted about this a few weeks ago. It is found here:
http://blog.ekini.net/2009/06/22/zend-framework-how-to-use-zend_paginator/
It is a pretty straight-forward tutorial. It starts with the form, then the controller, and goes down to the view and the paginator file.
Well, i finally found an interesting way to do.
First, i implemented a Domain Model pattern after a read on Matthew Weier O'Phinney's blog, who explains how to.
Then, i created my own adapter of Zend_Paginator, to agree with my Model.
It's the most interesting way to do, i've found until now.

Categories