Select WHERE IN in Laravel 8 with Facades\DB - sql

If i try select without params it work fine
Log::debug(DB::connection('myconnect')->select('SELECT id FROM table WHERE city_id in ( '.implode(',', $cities)].' ) '));
Result is
local.DEBUG: array (
0 =>
(object) array(
'id' => 6606,
),
1 =>
(object) array(
'id' => 6611,
),
2 =>
(object) array(
'id' => 6631,
),
3 =>
(object) array(
'id' => 6861,
),
4 =>
(object) array(
'id' => 7163,
),
)
But if i try to use parameter
Log::debug(DB::connection('myconnect')->select('SELECT id FROM table WHERE city_id in ( ? ) ',[implode(',', $cities)]));
Only first record returned
local.DEBUG: array (
0 =>
(object) array(
'id' => 6606,
),
)
What am I doing woring?

Number of question marks has to be equal with the number of elements in $cities.
Or you could do it like this:
DB::table('table')
->whereIn('id', $cities)
->get();

Related

How to sub query a select into another select and put inside a variable with laravel?

I want to make a select that solves the fact that I have to perform two validations one for each table, what i'm doing now is validating the $table1 and validating the $table2 ,i just want to validate $table2
$table1 = DB::table('name_table1')
->where('invoice_id','{$table2}.id')
->get();
$table2 = DB::table('name_table2')
->select('name','invoices','{$table} as itens_of_invoice')
->get();
dd($table);
And return me something like;
Array (
[0] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[1] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[2] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
)
I don't find any solution if someone ever did this please help me

ManyToMany left join query builder

I have entity A with ManyToMany doctrine relation wite entity B
I want to get data of the entity A using query builder in this way
$data = array(
0 => array(
'entity_A_field_1' => 'entity_A_field_1_value',
'entity_A_field_2' => 'entity_A_field_2_value',
'entity_A_field_3' => 'entity_A_field_3_value',
'entity_B' => array(
0 => array(
'entity_B_field_1' => 'entity_B_field_1_value',
'entity_B_field_2' => 'entity_B_field_2_value',
'entity_B_field_3' => 'entity_B_field_3_value',
),
2 => array(
'entity_B_field_1' => 'entity_B_field_1_value',
'entity_B_field_2' => 'entity_B_field_2_value',
'entity_B_field_3' => 'entity_B_field_3_value',
),
),
),
1 => array(
'entity_A_field_1' => 'entity_A_field_1_value',
'entity_A_field_2' => 'entity_A_field_2_value',
'entity_A_field_3' => 'entity_A_field_3_value',
'entity_B' => null
),
);
her is my entities: entity A is product
class Product
{
/**
* #ORM\ManyToMany(targetEntity="Category", inversedBy="products",cascade={"persist", "remove"})
*
* #ORM\JoinTable(
* name="store_product_category"
* )
*/
private $categories;
Entity B is category
class Category
{
/**
* #ORM\ManyToMany(targetEntity="Product", mappedBy="categories")
*/
private $products;
I want to get all my products with their categories using query builder and without conditions (if they have| null if they don't). i need to get them all.
$data = array(
0 => array(
'product_id' => 'product_id_value',
'product_name' => 'product_name_value',
'product_reference' => 'product_reference_value',
'categories' => array(
0 => array(
'category_id' => 'category_id_value',
'category_name' => 'category_name_value',
'category_token' => 'category_token_value',
),
2 => array(
'category_id' => 'category_id_value',
'category_name' => 'category_name_value',
'category_token' => 'category_token_value',
),
),
),
1 => array(
'product_id' => 'product_id_value',
'product_name' => 'product_name_value',
'product_reference' => 'product_reference_value',
'category' => null
),
);

Codeigniter: Update multiple Rows in Codeigniter in one statement

I am having dictionary as
array(
'ID1' => 1 ,
'ID2' => 2,
'Status' => 'Done'
)
While I have only 2 columns in my table, ID and Status. I want to update two rows having IDs 1, and 2, using only one time ($this->db->update). Is it possible or should I have to make custom query.
I want to do like ($this->db->update(where ID1 == 1 && ID2 == 2)).
Either
// Get your status
$item = array( 'Status' => $response['Status'] );
// unset status
unset($response['Status']);
// I assume except status rest all values are ids so
// array_values gives id
$this->db->where_in('id', array_values($response));
// Update table
$this->db->update('table', $item );
OR
You have to modify your array
$data = array(
array(
'id' => 1 ,
'Status' => 'Done'
),
array(
'id' => 2 ,
'Status' => 'Done'
)
);
and then
$this->db->update_batch('table_name', $data, 'id');
Like this you can modify
( for comment : Dear I cant do so, because I am getting response from some service. And I am not able to edit this response. )
[akshay#localhost tmp]$ cat test.php
<?php
$response = array(
'ID1' => 1 ,
'ID2' => 2,
'Status' => 'Done'
);
$data = array();
$item = array( 'Status' => $response['Status'] );
unset($response['Status']);
foreach($response as $new){
$item['id'] = $new;
$data[] = $item;
}
print_r($data);
// Here you update
// $this->db->update_batch('table_name', $data, 'id');
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => Array
(
[Status] => Done
[id] => 1
)
[1] => Array
(
[Status] => Done
[id] => 2
)
)
//this will update the values where ids equal any value in the second were_in parameter
//data is array of values to update
$data = [
'Status'=>'some status',
];
//array of ids to update
$ids = [1,2];
$this->db->where_in('id', $ids)->update('table_name', $data);

SUM function on multiple columns in Lithium ORM

I want something like this:
$res = Model::find('all', array(
'fields' => array(
'SUM(col1)' => array(
'alias' => 'col1_total',
),
'SUM(col2)' => array(
'alias' => 'col2_total',
)
)
);
expected generated SQL:
SELECT SUM(col1) AS col1_total, SUM(col2) AS col2_total
FROM `tbl` AS `Model` WHERE 1;
I tried many ways.
is this possible?
a working example for a single col:
$res = Model::find('all', array(
'fields' => 'SUM(col1)'
)
);
Cool!
working example:
$res = Model::find('all', array(
'fields' => array(
'SUM(col1) AS col1_total',
'SUM(col2) AS col2_total'
)
);

Yii Cgridview how to render in view 2 or more values from the same table in 1 column

I have a Cgridview that has multiple columns. I would like to merge and render the values of 3 columns in 1 column.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'policy-grid',
'dataProvider'=>$dataProvider,
'filter'=>$dataProvider->model,
'selectableRows' => 1, // you can select only 1 row!!
'selectionChanged'=>'function(id){ var objectId = $.fn.yiiGridView.getSelection(id);
if (isNaN(objectId) || objectId == ""){return;} location.href =
"'.$this->createUrl('policy/view').'&id="+$.fn.yiiGridView.getSelection(id);}',
'htmlOptions'=>array('class'=>'grid-view grid_pointer',),
'columns'=>array(
array(
'name' => 'compliance',
'type' => 'html',
'value' => '$data->status == "ACTIVE" ? (CHtml::image($data->compliance ==
"SUFFICIENT" ? "images/policy_sufficient.png" : "images/policy_insufficient.png"))
: "N/A" ',
'filter' => VFFormUtil::getFilter(VFFormUtil::POLICY_COMPLIANCE),
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'status',
'filter' => VFFormUtil::getFilter(VFFormUtil::ACTIVE_INACTIVE),
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'coverage_left',
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'model_year',
'htmlOptions' => array('style'=>'text-align: center'),
),
'make',
'model',
How would I combine 'model_year','make' and 'model', into 1 column in the views?
The correct way would be to create a new property variable in the model, and join the values afterFind().
See How to have multiple fields (D/M/Y) for single date property in Yii?, but you have to swap the find and save functions, as that user wants to do the opposite (split single column into three fields instead).
You can create a getter in the model:
public function getModelLabel()
{
return $this->model_year.' '.$this->make.' '.$this->model;
}
Then you can use modelLabel in your GridView as if it were a real attribute.