To find record in yii2 I use following code:
$response = Response::findOne(['unique_url' => $unique_url]);
But it return record regardless $unique_url case.
How to do it case sensitive?
I think you should use LIKE BINARY
and for this you should extended you modelSearch adding the clause in query condition
public function search($params)
{
$query = YuorModel::find();
.......
.......
$query->andFilterWhere(['like binary', 'unique_url', $this->unique_url])
->andFilterWhere(['like', 'your_field2', $this->your_field2])
.......
Best solution which I found for this:
Response::find()->where('BINARY [[unique_url]]=:unique_url', ['unique_url'=>$unique_url])->one();
Related
I try to get the result of search queries with a where clause in the query using TNTSearch, but it doesn't work. The query didn't get the where clause.
Controller
<?php
public function getLigues(Request $request)
{
if ($request->has('recherche')) {
$ligues = Structure::search($request->recherche)->where('type_structure_id', '2')->get();
} else {
$ligues = Structure::where('type_structure_id', 2)->paginate(1);
}
return view('structure/ligues', compact('ligues'));
}
Does anyone have any ideas on how to get the filter query?
Please go through this issue #59 on official TNT Search Repository.
See contributors comment on same issue.
Where() clause yet does not support!
There are other workarounds to achieve this testcase, for that check issues on repository.
In Laravel 4.2 I want to get Compiled Query.
This is what i have:
$product = Product::where('id', '=', '100')->get();
I want compiled query like:
select * from products where id = 100
Purpose of the question is: i want to use it as sub query in another query.
I have searched and found Class Grammer and Class MySQL But i did not found solution for that.
Is there any solution?
Your help would be appreciated.
The easiest way is probably mostly finding out what query was just executed, rather than what the query will be. Something like this:
function latestQuery()
{
$queries = DB::getQueryLog();
return end($queries);
}
Hopefully that's the same for your purposes.
You can get the SQL query like this:
use DB;//write this at the top of the file above your Class
DB::enableQueryLog();//Enable query logging
$product = Product::where('id', '=', '100')->get();
dd(DB::getQueryLog());//print the SQl query
You can register an event listener in your routes file(in development phase), which will listen for the laravel query event and var_dump the executed query.
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
But this will turn out to be messy. So better use something like Clockwork. It is awesome, you can view all the executed query in your browser.
You can use grammer for subqueries, See below example for reference :
$users = DB::table('users')
->where('user.id', '=', $userID)
->join('product', 'product.userid', '=', 'user.id');
$price = $users->select(array(DB::raw('SUM(price)')))
->grammar
->select($users); // Compiles the statement
$result = DB::table('users')->select(array(DB::raw("({$price}) as price)))->get();
This add a subquery to your main query.
i am new with codeigniter.
i have used the following code to execute query recursively.
Suppose $q query select 4 id (10,11,20,24)
then for each id showreply function (in foreach) call recursively then how can return the combine result.
$resultq3 = $this->showreply($reply_id);
<?php
public function showreply($reply_id)
{
$q1 =$this->db->select('*')
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id1)
->order_by('fr.id ')->get();;
foreach($q1->result_array() as $row4)
{
$id = $row4['id'];
$parent_id = $row4['parent_id'];
if($parent_id!=0)
{
$this->showreply($id);
}
}
return $result;
}
?>
I'm not really understanding what it is you're asking here. Maybe showing the showReply function would help, but you already have the combined result and are splitting that out in your foreach so what's the problem? Also why are you assigning reply_id to reply_id1? What is the point of that? Just use $reply_id in your query.
You're also executing an if statement that makes little sense since you can filter out the id's you don't want in the query itself (and are you seriously ever going to have an id that = 0?)
In fact the more I look at this code the more confused I become. Where is $id getting populated for $this->showreply($id)?
<?php
public function showreply($reply_id)
{
$q1 =$this->db->select('*')
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id)
->where('fr.parent_id !=',0)
->order_by('fr.id ')->get();;
//$i=0;
foreach($q1->result_array() as $row4)
{
$parent_id = $row4['parent_id'];
$this->showreply($id);
}
//below is the actual answer to your question on how to return the combined results.
return $q1->result_array();
}
?>
Okay after rereading your question I think I have a better understanding. If you pass the id's as an array like this:
$reply_id = array(10,11,20,24);
You can then modify your query to use:
$this->db->where_in('fr.parent_id', $reply_id);
That will return the results as one combined result with all 4 ids included.
I'm working with the latest codeIgniter released, and i'm also working with jquery datatables from datatables.net
I've written this function: https://gist.github.com/4478424 which, as is works fine. Except when I filter by using the text box typing something in. The filter itself happens, but my count is completely off.
I tried to add in $res = $this->db->count_all_results() before my get, and it stops the get from working at all. What I need to accomplish, if ($data['sSearch'] != '') then to utilize the entire query without the limit to see how many total rows with the search filter exists.
If you need to see any other code other than whats in my gist, just ask and I will go ahead and post it.
$this->db->count_all_results() replaces $this->db->get() in a database call.
I.E. you can call either count_all_results() or get(), but not both.
You need to do two seperate active record calls. One to assign the results #, and one to get the actual results.
Something like this for the count:
$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
And for the actual query (which you should already have):
$this->db->select($your_columns);
$this->db->from('table');
$this->db->where($your_conditions);
$this->db->limit($limit);
$query = $this->db->get();
Have you read up on https://www.codeigniter.com/userguide2/database/active_record.html#caching ?
I see you are trying to do some pagination where you need the "real" total results and at the same time limiting.
This is my practice in most of my codes I do in CI.
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
); // Return this back to the controller.
I typed the codes above without testing but it should work something like this. I do this in all of my projects.
You dont actually have to have the from either, you can include the table name in the count_all_results like so.
$this->db->count_all_results('table_name');
Count first with no_reset_flag.
$this->db->count_all_results('', FALSE);
$rows = $this->db->get()->result_array();
system/database/DB_query_builder.php
public function count_all_results($table = '', $reset = TRUE) { ... }
The
$this->db->count_all_results();
actually replaces the:
$this->db->get();
So you can't actually have both.
If you want to do have both get and to calculate the num rows at the same query you can easily do this:
$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();
$results = $db_results->result();
$num_rows = $db_results->num_rows();
Try this
/**
* #param $column_name : Use In Choosing Column name
* #param $where : Use In Condition Statement
* #param $table_name : Name of Database Table
* Description : Count all results
*/
function count_all_results($column_name = array(),$where=array(), $table_name = array())
{
$this->db->select($column_name);
// If Where is not NULL
if(!empty($where) && count($where) > 0 )
{
$this->db->where($where);
}
// Return Count Column
return $this->db->count_all_results($table_name[0]);//table_name array sub 0
}
Then Simple Call the Method
Like this
$this->my_model->count_all_results(['column_name'],['where'],['table name']);
If your queries contain a group by, using count_all_results fails. I wrote a simple method to work around this. The key to preventing writing your queries twice is to put them all inside a private method that can be called twice. Here is some sample code:
class Report extends CI_Model {
...
public function get($page=0){
$this->_complex_query();
$this->db->limit($this->results_per_page, $page*$this->results_per_page);
$sales = $this->db->get()->result(); //no table needed in get()
$this->_complex_query();
$num_results = $this->_count_results();
$num_pages = ceil($num_results/$this->results_per_page);
//return data to your controller
}
private function _complex_query(){
$this->db->where('a', $value);
$this->db->join('(subquery) as s', 's.id = table.s_id');
$this->db->group_by('table.column_a');
$this->db->from('table'); //crucial - we specify all tables here
}
private function _count_results(){
$query = $this->db->get_compiled_select();
$count_query = "SELECT count(*) as num_rows FROM (".$query.") count_wrap";
$r = $this->db->query($count_query)->row();
return $r->num_rows;
}
}
Hello how can i like if i use substr(); do so i only get like 400 number of characters from the database out?
You have to use core mysql function SUBSTRING to achieve this.
In codeigniter the query can be written as -
$this->db->select("SUBSTRING('COLUMN_NAME',5)");
$query = $this->db->get('TABLE_NAME');
foreach ($query->result() as $row)
{
//process result here.
}
You can use codeigniters limiter (test helper) to display only what you want
$string = "Here is a nice text string consisting of eleven words.";
$string = character_limiter($string, 400);
You can pull the entire string out of the database but only use the number of characters you need.
Or
take a look at this tutorial using "left" in mysql
http://net.tutsplus.com/tutorials/php/how-to-create-blog-excerpts-with-php/
It too late, but this is for someone like me looking for solution
public function getDetails(){
// mytable(id,name,about,...,status)
$this->db->select(array('id', 'name', 'SUBSTRING(about,1,180) AS about', 'status'));
$result=$this->get('mytable');
return result_array();
}