For example, I have this MongoDB:
In yii2, I want to write a query to find an email = 'abc'. I have this code:
$query->select([])->from('Post')->where(['Comments.Email' => 'abc']);
But it does not work; please help me, as I'm beginner in Yii2 and MongoDB.
You can try this
Document::find()->where(["comments"=>["document"=>["email"=>"abc"]]])->all();
$res = $collection->find(["comments.email"=>"abc"]);
$rows = $res->toArray();
From documentation:
https://www.yiiframework.com/extension/yiisoft/yii2-mongodb/doc/api/2.1/yii-mongodb-collection#find()-detail
You can do that but using this query below,
$order = (new Query)->select([])->from('post')->where(['email' => 'abc'])->all();
Related
I want to use like queries for search in my app using MongoDb With PHP but I did not get the proper result.
Code:
$query = array("first_name" => "/.*a.*/" );
$updateResult = $this->dbCustomer->find($query);
Try this,
$query = array("first_name" => array("$regex" => "/.a./"));
$updateResult = $this->dbCustomer->find($query);
more details, please check this out
I got an answer and I try this and it's work perfect :
$query = array(array("first_name" => new MongoDB\BSON\Regex("$search_text", 'i')))
I just started learning Codeigniter 4. My query always generates NULL and I don't know why. How can I see the generated SQL Select command just like Codeigniter 3?
In Codeigniter 3 this command does the job:
echo $this->db->last_query();
And this is my controller code in Codeigniter 4 that I need to get the generated query:
$cityModel = new CityModel();
$cities = $cityModel
->select('city.name AS cityName')
->select('county.name AS countryName')
->select('province.name AS provinceName')
->join('province', 'city.province_id = province.id', 'left')
->join('county', 'city.county_id = county.id', 'left')
->result();
Update:
I tried this code but it's returning an empty string:
var_export((string)$cityModel->db->getLastQuery());
This should display the final query:
$cityModel->getLastQuery()->getQuery()
In CI 4 Refer Doc
you can use getLastQuery() as
$query = $db->getLastQuery();
echo (string)$query;
This code will help you to get the last query in codeigniter 4.
$this->db = \Config\Database::connect();
$data = $builder->get()->getResult();
echo $this->db->getLastQuery(); die;
You can use getCompiledSelect it will return the query SELECT command.
$sql = $cityModel->getCompiledSelect();
echo $sql;
Following Code will work in CI 4
$this->db->getLastQuery()->getQuery();
Simply just use $this->db->getLastQuery();
This worked for me
$myModel = new MyModel();// your any model
$myModel->db->getLastQuery()->getQuery();
$this->db is not directly accessible from controller, but if you have any Model instance then you can have the full access to db object.
Add this code in your class
$db = \Config\Database::connect();
$query = $db->getLastQuery();
echo $query;
I am looking for query in Codeigniter works like where OR, AND both works.
This is working Perfect!
$srcArr2['emp_id']=8;
$srcArr2['status']='2';
$this->db->where($srcArr2);
$query = $this->db->get('tickets');
$res = $query->result_array(); // as array
print_r ($res);
query is:
Need to add OR in where with $srcArr1['ass_id']=12;
I try but not working.
$srcArr1['ass_id']=12;
$srcArr2['emp_id']=8;
$srcArr2['status']='2';
$this->db->where($srcArr2);
$this->db->where_in($srcArr1);
$query = $this->db->get('tickets');
$res = $query->result_array(); // as array
print_r ($res);
Thank You, in Advance for help
You can use or_where_in to achieve the same,
$this->db->or_where_in($srcArr1);
Official doc.
$this->db->where($srcArr2);
$this->db->or_where($srcArr1);
https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-dataenter link description here
I am new in Yii. Now i have encountered problem with active record in yii.
So , I have normal sql here :
$sqlText = "SELECT *
FROM tbl_webservicetokens
WHERE clienttoken = '{$appToken}'
AND
systimestamp < expiredate";
I want to use active record. But i endeavored
$post=TBLWEBSERVICETOKENS::model()->find(
'CLIENTTOKEN=:appToken AND EXPIREDATE>:systimestamp',
array(
':appToken'=>$appToken,
':systimestamp'=>'systimestamp'));
But i had error!
any idea?
Try this query. I think this will work for you.
$post= TBLWEBSERVICETOKENS::model()->find(array(
'select'=>'*',
'condition'=>'CLIENTTOKEN=:appToken AND EXPIREDATE>:systimestamp',
'params'=>array(':appToken'=>$appToken,':systimestamp'=>'systimestamp'))
);
You are passing wrong data type (string) for the date time field have you noted that
$post=TBLWEBSERVICETOKENS::model()->find(
'CLIENTTOKEN=:appToken AND EXPIREDATE>:systimestamp',
array(
':appToken'=>$appToken,
':systimestamp'=>$systimestamp)); //$systimestamp where your time data type value resides
If you still have the error
Read Me!!!
Well, It seems to me i find answer:
$criteria = new CDbCriteria;
$criteria->condition = "ID = 1212 AND CLIENTTOKEN = 'ws546b041c85ad38a2c1f4224e1e39fe09cf76a3c8703c5'";
$models = TBLWEBSERVICETOKENS::model()->findAll($criteria);
Hi I'm new to CodeIgniter and I just want to know How will I query from my MySql Db, a Select Statement with a where clause, I know it can be searched from the net but whenever I try something I get errors, It's really frustrating. The string in the Where clause will be coming from a User Input. Thanks guys!
You can do as Mehedi-PSTU stated, however it seems as though you're a little new to this, so here's some extra information:
I'll copy Mehedi-PSTU for the most part here.
$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');
This will store the query object in the variable $query.
if you wanted to convert that to a usable array, you just perform to following.
$results = $query->result_array();
Or you can loop through it like this:
foreach($query->result_array() as $result){
// Perform some task here.
}
A better or even full understanding can probably come from:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Try something like this
$this->db->where('db_attr', $var);
return $this->db->get('table');
Try this one.
$id = 'your id';
$this->db->select("*");
$this->db->from("table_name");
$this->db->where('id','$id');
$query = $this->db->get();
return $query->result_array();
In Codeigniter with Method Chaining Style :-
$data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();