Zend Framewrok 2: How to write custom query using tablegateway - php

I need to write a update query in a model which use tablegateway. The update query sql like following.
UPDATE `config_settings` SET `CS_value` = CASE `CS_option`
WHEN 'CATEGORY_BANNER_MIN_WIDTH' THEN '$data->CATEGORY_BANNER_MIN_WIDTH'
WHEN 'CATEGORY_BANNER_MAX_WIDTH' THEN '$data->CATEGORY_BANNER_MAX_WIDTH'
WHEN 'CATEGORY_PROMOTION_MIN_WIDTH' THEN '$data->CATEGORY_PROMOTION_MIN_WIDTH'
WHEN 'CATEGORY_PROMOTION_MAX_WIDTH' THEN '$data->CATEGORY_PROMOTION_MAX_WIDTH'
WHEN 'PRODUCT_LARGE_IMAGE_WIDTH' THEN '$data->PRODUCT_LARGE_IMAGE_WIDTH'
WHEN 'PRODUCT_MEDIUM_IMAGE_WIDTH' THEN '$data->PRODUCT_MEDIUM_IMAGE_WIDTH'
WHEN 'PRODUCT_SMALL_IMAGE_WIDTH' THEN '$data->PRODUCT_SMALL_IMAGE_WIDTH'
ELSE `CS_value`
END;
I have no idea how to implement this. The update method of tablegateway only take array of table field name and its value. So how to write this query.
I know that i can execute this query using db adapter raw sql query but i don't want this. Beside this, some times we need to some custom query in select method of tablegateway. But i found no stable way in tablegateway.
For example:
select sum(CASE WHEN answers.type = 'his' THEN 1 ELSE 3 END) AS totalScore
FROM users_questions_answers join answers on cast(answers.id as int(8))=
users_questions_answers.answer_id group by users_questions_answers.user_id
How can i proceed in this situation. Any zend 2 expert suggestion will highly appreciated.
Thanks for your kind consideration.

Try this -
$this->tablegateway->update(array('CS_value' => new \Zend\Db\Sql\Expression('CASE CS_option
WHEN "CATEGORY_BANNER_MIN_WIDTH" THEN ?
WHEN "CATEGORY_BANNER_MAX_WIDTH" THEN ?
WHEN "CATEGORY_PROMOTION_MIN_WIDTH" THEN ?
WHEN "CATEGORY_PROMOTION_MAX_WIDTH" THEN ?
WHEN "PRODUCT_LARGE_IMAGE_WIDTH" THEN ?
WHEN "PRODUCT_MEDIUM_IMAGE_WIDTH" THEN ?
WHEN "PRODUCT_SMALL_IMAGE_WIDTH" THEN ?
ELSE CS_value END',
array($data->CATEGORY_BANNER_MIN_WIDTH, $data->CATEGORY_BANNER_MAX_WIDTH, $data->CATEGORY_PROMOTION_MIN_WIDTH, $data->CATEGORY_PROMOTION_MAX_WIDTH, $data->PRODUCT_LARGE_IMAGE_WIDTH, $data->PRODUCT_MEDIUM_IMAGE_WIDTH, $data->PRODUCT_SMALL_IMAGE_WIDTH))));
If CATEGORY_BANNER_MIN_WIDTH like used in the query are some constants then just remove the double quotes around it.
I have tried a similar query to the above one and it works just fine.

Related

Sql query in yii2

I would like to execute such a command:
SELECT number.phone_number
FROM number
LEFT JOIN ordered_number
ON ordered_number.number_id=number.id
WHERE ordered_number.order_id=123
with Yii2. I do this:
$numery = Number::find()
->select('number.phone_number')
->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.id'])
->where(['ordered_number.order_id' => 123])
->createCommand()
->rawSql;
But I get then this:
'SELECT `number`.`phone_number` FROM `number` LEFT JOIN `ordered_number` ON `ordered_number`.`number_id`='number.id' WHERE `ordered_number`.`order_id`=123'
Which doesn't work. When I put the first thing right into my base, I get 4 results, which is correct. But when I do (I think exactly the same) with Yii, I have troubles, because it is null. The only differences between what I have written and what appears after rawSql, are ` and '.
Sorry, I've just read docs for this method - it should be:
->leftJoin('ordered_number', 'ordered_number.number_id = number.id')
because using the where() kind of conditions with array leads to comparing field to string.
Maybe this link could help you a lot , the comments of ActiveRecord.

ORDER BY on MySQL stored function in CakePHP 3

I'm working on cakePHP 3. I have a user defined function(UDF or Routine) in mysql database. That function takes a parameter and returns an integer value. I have to order that returned value in MySQL order clause.
I know mysql query to use that function. i.e,
SELECT customer_id FROM table_name ORDER BY routine_name(param1); //param1 is 'customer_id' which I have written after SELECT
But I don't know that how to build this query in cakePHP 3. If anyone knows the solution, answer will be appreciate.
Here is my cakePHP 3 code.
$purchasesTable = TableRegistry::get("Purchases");
$query = $purchasesTable->find();
$sf_val = $query->func()->routine_name(['Purchases.customer_id' => 'literal']);
$query->select();
$query->order(
// Routine/Function call should be here as per MySQL query.
// So, I think here I have to do something.
);
I'd suggest that you have a closer look at the (API) docs, it's all mentioned there. You can pass expression objects to Query::order(), and in case you need to specify the direction, there's also Query::orderAsc() and Query::orderDesc().
So
$expression = $query->func()->routine_name(['Purchases.customer_id' => 'literal']);
$query->order($expression);
or
$query->orderAsc($expression);
or
$query->orderDesc($expression);
See also
Cookbook > Database Access & ORM > Query Builder > Selecting Data
API > \Cake\Database\Query::order()
API > \Cake\Database\Query::orderAsc()
API > \Cake\Database\Query::orderDesc()

Laravel: How to get last N entries from DB

I have table of dogs in my DB and I want to retrieve N latest added dogs.
Only way that I found is something like this:
Dogs:all()->where(time, <=, another_time);
Is there another way how to do it? For example something like this Dogs:latest(5);
Thank you very much for any help :)
You may try something like this:
$dogs = Dogs::orderBy('id', 'desc')->take(5)->get();
Use orderBy with Descending order and take the first n numbers of records.
Update (Since the latest method has been added):
$dogs = Dogs::latest()->take(5)->get();
My solution for cleanliness is:
Dogs::latest()->take(5)->get();
It's the same as other answers, just with using built-in methods to handle common practices.
Dogs::orderBy('created_at','desc')->take(5)->get();
You can pass a negative integer n to take the last n elements.
Dogs::all()->take(-5)
This is good because you don't use orderBy which is bad when you have a big table.
You may also try like this:
$recentPost = Article::orderBy('id', 'desc')->limit(5)->get();
It's working fine for me in Laravel 5.6
I use it this way, as I find it cleaner:
$covidUpdate = COVIDUpdate::latest()->take(25)->get();
Ive come up with a solution that helps me achieve the same result using the array_slice() method. In my code I did array_slice( PickupResults::where('playerID', $this->getPlayerID())->get()->toArray(), -5 ); with -5 I wanted the last 5 results of the query.
The Alpha's solution is very elegant, however sometimes you need to re-sort (ascending order) the results in the database using SQL (to avoid in-memory sorting at the collection level), and an SQL subquery is a good way to achieve this.
It would be nice if Laravel was smart enough to recognise we want to create a subquery if we use the following ideal code...
$dogs = Dogs::orderByDesc('id')->take(5)->orderBy('id')->get();
...but this gets compiled to a single SQL query with conflicting ORDER BY clauses instead of the subquery that is required in this situation.
Creating a subquery in Laravel is unfortunately not simply as easy as the following pseudo-code that would be really nice to use...
$dogs = DB::subQuery(
Dogs::orderByDesc('id')->take(5)
)->orderBy('id');
...but the same result can be achieved using the following code:
$dogs = DB::table('id')->select('*')->fromSub(
Dogs::orderByDesc('id')->take(5)->toBase(),
'sq'
)->orderBy('id');
This generates the required SELECT * FROM (...) AS sq ... sql subquery construct, and the code is reasonably clean in terms of readability.)
Take particular note of the use of the ->toBase() function - which is required because fromSub() doesn't like to work with Eloquent model Eloquent\Builder instances, but seems to require a Query\Builder instance). (See: https://github.com/laravel/framework/issues/35631)
I hope this helps someone else, since I just spent a couple of hours researching how to achieve this myself. (I had a complex SQL query builder expression that needed to be limited to the last few rows in certain situations).
For getting last entry from DB
$variable= Model::orderBy('id', 'DESC')->limit(1)->get();
Imagine a situation where you want to get the latest record of data from the request header that was just inserted into the database:
$noOfFilesUploaded = count( $request->pic );// e.g 4
$model = new Model;
$model->latest()->take($noOfFilesUploaded);
This way your take() helper function gets the number of array data that was just sent via the request.
You can get only ids like so:
$model->latest()->take($noOfFilesUploaded)->puck('id')
use DB;
$dogs = DB::select(DB::raw("SELECT * FROM (SELECT * FROM dogs ORDER BY id DESC LIMIT 10) Var1 ORDER BY id ASC"));
Dogs::latest()->take(1)->first();
this code return the latest record in the collection
Can use this latest():
$dogs = Dogs::latest()->take(5)->get();

Laravel Fluent Query Builder Update Query

I want to add two columns while using update, like this:
Update purchase_stock inner join stock on purchase_stock.fkstockid=stock.stockid SET currentavailable=currentavailable+subquantity where fkorderid='1';
Here is the current Fluent code:
DB::table('purchase_stock')->join('stock','stock.stockid','=','purchase_stock.fkstockid')->where('fkorderid',$orderId)->update(array('currentavailable'=>'currentavailable'+'subquantity'));**
But it throws error as below:
"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"syntax error, unexpected '=>'"
Does any one have solution?
You were very close with your fluent attempt, but there were two issues:
The plus sign needs to be inside the quotes, since you want the math done in SQL, not in PHP.
Need a DB::raw() around the value so Laravel doesn't think you're actually trying to set it to the string "currentavailable + subquantity"
So the final product looks like this:
DB::table('purchase_stock')
->join('stock', 'stock.stockid', '=', 'purchase_stock.fkstockid')
->where('fkorderid', $orderId)
->update(['currentavailable' => DB::raw('currentavailable + subquantity')]);
Мaybe you need to set these two fields from which tables. Exampl. DB::table('purchase_stock')->join('stock','stock.stockid','=','purchase_stock.fkstockid')->where('fkorderid',$orderId)->update(array('stock.currentavailable'=>'stock.currentavailable'+'stock.subquantity'));
Ohk!
I already tried this one but it is not working
As of now I am using DB::statement('') and it's working
So I have written whole update query within statement and it's working as somewhere I have read that it will not be helpful to return result set but will work with insert or update.

CodeIgniter Active Record or_where() function produces AND in sql?

I wrote an active record query in CodeIgniter and then I realised that I needed to use OR with to WHERE clauses. So I looked through the docs and found or_where which did what I wanted. But when I use it it produces AND in the output. I couldn't find any other questions on this issue.
I'm using CodeIgniter: 2.1.0
Here is my code (slightly cut down):
$this->db->select("p.*",false);
$this->db->from('projects p');
$this->db->join('customers c', 'p.c_id = c.c_id','left outer');
if(isset($options['add_root']))
$this->db->or_where('p.p_id=',1,FALSE);
//Get top level projects by default (1) or whatever parent is specified.
if(isset($options['p_id']))
$this->db->where('p.p_id=',$options['p_id'],false);
$query = $this->db->get();//query
I don't think you need or_where. I think you need better if/else in PHP.
The logic you probably want:
if(isset($options['p_id']))
{
// if p_id is set, then look for p_id
$this->db->where('p.p_id=',$options['p_id'],false);
// if p_id and add_root are set, then look for p_id OR p_id = 1
if(isset($options['add_root']))
$this->db->or_where('p.p_id=',1,FALSE);
}
elseif(isset($options['add_root']))
{
// look for p_id = 1 only
$this->db->where('p.p_id=',1,FALSE);
}
Because or_where is first it is simply defaulting to where, and then the subsequent where is the default: an "and".
You could also write the above with a series of elseif's but I view this as less clear:
if(isset($options['p_id']) && isset($options['add_root']))
$this->db
->where('p.p_id=',$options['p_id'],false)
->or_where('p.p_id=',1,FALSE);
elseif(isset($options['p_id']) || isset($options['add_root']))
$this->db
->where('p.p_id=',
// if add_root is set, then 1, otherwise p_id
(isset($options['add_root'])?1:$options['p_id']),false);
There's a small error in the order of the query that you're trying to run. You can add multiple 'where' clause which will get converted to a query with an 'AND' in between. But if you wanna use 'OR' instead you use a 'or_where'.
In your query you've used an 'or_where' clause, which is correct but you've used 'where' after that, which literally adds up to the previous query. So, you gotta use the 'where' clause first and then use the 'or_where' clause.
Just change the order and it would work.

Categories