I was trying to rip off every connection function of Laravel's eloquent to give me SQL clause without querying it with PDO ( I also cleaning off every PDO instances from the code) and make eloquent a standalone package. I just want it to give me sql clause without connect to any db when I use an eloquent model just like this :
Request : Users::with('group')->take(5)->get();
Response : Select * from users.....
Can this be done ?
use ->toSql() instead of ->get()
Related
I'd like to make a MySQL query with doctrine2 QueryBuilder (Symfony 3.4 app) with a NOT BETWEEN statment.
The doctrine provide a ->expr()->between(..) but not ->expr()->notBetween(..)
Is there a way to negate the between with the query builder.
I don't want to use a native or a DQL query if possible.
Note: I think a possible solution is to use ->expr()->lt(..) and/or ->expr()->gt(..) but I want to know if notBetween is possible.
Thanks
Expected:
A NOT BETWEEN SQL statement with Doctrine2 QueryBuilder
After some attempts, I found a suitable solution for me:
The QueryBuilder provide a ->expr()->not(...), so in this case this is possible:
$qb->exp()->not($qb->between('field', 'from', 'to')
SQL result:
NOT (BETWEEN field from AND TO)
I've used a slow Eloquent query and I changed it to faster one but with raw SQL
There is GroupBy used in the later part of code and now it fails.
$app_visits = DB::select(DB::raw('select id, place from table where 1=1'));
$app_visits = $app_visits->groupBy('place');
is there an easy way to use similar method with the raw table I'm getting?
Can't you just also use in the raw query the groupby?
$app_visits = DB::select(DB::raw('select id, place from table where 1=1 GROUP BY place'));
Using Codeigniter 3 at work with Oracle 12c and sporadic uses of the Query Builder class.
Query Builder is selecting from Oracle with escaped table names. I would like to configure the class to not do this. I'd like this call:
$query = $this->db->get('customers');
to go from
select * from 'customers';
to
select * from customers;
in generated SQL.
Is there a config item I don't know about or am I going to have to fix one of my coworker's queries every three weeks until the end of time?
You have to use next syntax
$this->db->select('SELECT * FROM customers', false)->get();
since second parameter in select($tablename, $escape=NULL) method will prevent table name or field names escaping when set to FALSE.
As far as I can see, the only way to make a query to ElasticSearch in Yii2 is to run ElasticModel::find()->query($query), where $query is a complex array containing the actual query written in ElasticSearch query DSL.
The query is huge and quickly becomes unmanageable. For SQL Yii2 provides a powerful query builder class that supports tons of useful methods like andWhere(). For ElasticSearch everything goes into one gigantic query expression, very much like building an SQL expression string by hand.
Is there any high-level wrapper for ElasticSearch query DSL for Yii2? If not, is there a standalone library with similar functionality?
If you intend to build for version 1.6 of elastic, I have created a query builder for my company and published it here
You will use it as a standalone query builder, and at the end you will need to get the final query array and pass it to the query executer.
To install it, you can simply use composer composer require itvisionsy/php-es-orm or download the zipped version here.
The link above contains some examples, and here is a copy:
//build the query using different methods
$query = \ItvisionSy\EsMapper\QueryBuilder::make()
->where('key1','some value') //term clause
->where('key2',$intValue,'>') //range clause
->where('key3','value','!=') //must_not term clause
->where('key4', ['value1','value2']) //terms clause
->where('email', '#hotmail.com', '*=') //wildcard search for all #hotmail.com emails
->sort('key1','asc') //first sort option
->sort('key2',['order'=>'asc','mode'=>'avg']) //second sort option
->from(20)->size(20) //results from 20 to 39
->toArray();
//modify the query as you need
$query['aggs']=['company'=>['terms'=>['field'=>'company']]];
//then execute it against a type query
$result = TypeQuery::query($query);
//i am not sure about Yii way to execute, according to the question, it should be:
$result = ElasticModel::find()->query($query);
The package also include a simple ElasticSearch ORM class which maybe useful for you. Take a look at it here.
Hope this helps you...
I have 2 databases defined in config/databases, mysql and mysql2
The default connection is mysql
I need to get this data from mysql2
$programs=DB::table('node')->where('type', 'Programs')->get();
The docs tell me I can change the connection using
$programs=DB::connection('mysql2')->select(...)
which would let me run a sql statement to get an array for $programs.
But I am wondering if there is a way to combine the 2 statements i.e. use query builder on specific db::connection.
You should use:
$programs=DB::connection('mysql2')->table('node')->where('type', 'Programs')->get();