I am facing a little trouble with converting this sql to codeigniter syntax. I can use this plain sql and getting results but I have to use return $this->db->query($sql, array($param))->result(); which I guess doesn't return an array (Not sure though, but i keep getting a error "Cannot use object of type stdClass as array in...", and I have no idea if that query can be modified to return an array or any other workaround is available.) Anyway, I guess the best thing to do for me is to follow the CI syntax of query and then use a return $query->result_array(); to get a result array from the query. I know it may be very basic stuff , but somehow I am not able to figure out how to exactly convert this sql to CI syntax. Any help would be appreciated. Thanks. Here is the sql below.
SELECT dirmast.entryID,dirmast.entryTitle,dirmast.entryShortDesc,dirsec.dirsecRefID
FROM dirmast,dirsec
WHERE dirsec.drtext = 'something'
AND dirsec.dirsecRefID = dirmast.entryID
GROUP BY dirsec.dirsecRefID
I think this will do what you want:
$this->db->select('dirmast.entryID,dirmast.entryTitle,dirmast.entryShortDesc,dirsec.dirsecRefID');
$this->db->from('dirmast');
$this->db->join('dirsec','dirsec.dirsecRefID = dirmast.entryID');
$this->db->where('dirsec.drtext','something');
$this->db->group_by('dirsec.dirsecRefID');
If you want your results in an array, you do ->result_array(), if you want it as an object, you do ->result(). Either way, you can use both Active Records and query().
Related
In CakePHP3, there is a ORM that helps with building queries.
From the documentation, I can see that
$query = $articles->find(); // build a query that has not run yet
$query->where(['id' => 1]); // Return the same query object
So in this case, I want the string
WHERE `articles`.`id` = 1
After much googling, I found out that there is a way to return just the where clause of a query object.
$query->where(['id' => 1])->clause('where'); // Return the where clause in the form of a QueryExpression
More googling leads me to find out how to get the QueryExpression to spit out string representation
$query->where(['id' => 1])->clause('where')->sql($valueBinder); // Return the where clause in string format
Here is my problem. I don't know what the $valueBinder is supposed to look like. I don't know how to initialize it.
I am also happy not to use ValueBinder as long as I can get the where clause in string format using CakePHP 3 ORM and in the right SQL dialect. Please assume I am using MySQL.
Please advise.
EDIT
I tried to use $query->valueBinder() as the $valueBinder.
It is empty and does not contain the associated c:0 to the value 1.
To directly answer your question, you can get the SQL for any clause this way:
$binder = new \Cake\ORM\ValueBinder();
$query->clause('where')->sql($binder);
That will return the SQL with the correct placeholders, not with the values to be used. The values live in the $binder variable and are used for statement objects.
As I can see, you only wanted to preserve the internal structure of the where clause to pass it to another query in a different request. Your solution is fine, but I'd like to add that you can also encode a full conditions tree from an existing query:
$where = serialize($query->clause('where'));
$anotherQuery->where(unserialize($where)); // A query in another request
In any case, you need to be careful with what you are unserializing as taking it directly from user input will certainly lead to security problems.
You can choose to omit this param if you like. Please see http://api.cakephp.org/3.0/class-Cake.Database.Query.html#_sql
In addition, you can use the Query member function traverse($visitor, $parts) to isolate the where clause. $visitor is a function that takes a value and a clause. You define the behavior of $visitor. $parts is an array of clause names. I suggest passing array('where') into this param.
My workaround is that I store the conditions in json string format.
Using the same example, what I do is
$data['conditions'] = json_encode(['Articles.id' => 1]); // encode into JSON string
$this->DynamicRules->patchEntity($dynamicRule, $data); // use in edit action of DynamicRulesController
then when I need to reuse the conditions, I do:
$articlesTable = TableRegistry::get('Articles');
$query = $articlesTable->find(); // new query for Articles
$rule = json_decode($dynamicRule->conditions, true); // get back the conditions in associative array format
$query->where($rule); // re-assign the conditions back
This got me what I ultimately wanted.
For my database query I have to use multiple where clause query in Codeigniter PHP. I wrote the code like this:
$this->db->and_where_in('category_name,publication_status','home_headline_sub',1);
But this query shows database query error in browser. Then I wrote this query:
$this->db->where('category_name,publication_status','home_headline_sub',1);
But it still give error. Can anyone help me to solve this? Thanks in advance.
You can chain database clauses, so you would write it as
$this->db->where('category_name','case')->where('publication_status','case')->where('home_headline_sub','case');
This would generate a query's WHERE clause as
// WHERE category_name = 'case' AND publication_status = 'case' AND home_headline_sub = 'case'
Documentation here: http://ellislab.com/codeigniter/user-guide/database/active_record.html#chaining
you to use array in it.
$this->db->where(array('category_name'=>case,'publication_status'=>case,'home_headline_sub'=>case));
but I guess you want to check your value against three columns. you can use
$this->db->or_where(array('category_name'=>1,'publication_status'=>1,'home_headline_sub'=>1));
I hope it will help you.
//The simple way
$this->db->where('foo_field', 'foo_value')
->where('bar_field', 'bar_value')
->where('more_field', 'more_value');
//using custom string
//if your sql is really a complex one you can simply write like these
$this->db->where("(foo_filed = 'foo_value') AND (bar_field = 'bar_value') AND (more_field = 'more_value')");
//or may be with something more complex like this
$this->db->where("(foo_filed = 'foo_value') AND ((bar_field = 'bar_value') OR (more_field = 'more_value'))");
//while using a custom string make sure you put them all in the "double quotation marks" and use no ,commas. It is all a single line. The braces are not necessary always but I like to use them.
Documentation
I have a query that is returning a single result and I'm wondering if there's a way to access the properties of that result without having to loop through it since I am limiting it to a single result.
Here is my query:
$user = Model_User::find()
->where('email_address', Input::post('email_address'))
->where('password', Input::post('password'))
->limit(1);
The only way I've found to access the results is to run the get() method on $user and loop through the result, but I figured I was missing something and that there was an easier way to return $user as a single object that I can work with since I am limiting it to a single result.
What's the most efficient way to do this?
Did you try
$user->get_one()?
You could also do
$user = Model_User::find_by_email_address_and_password(Input::post('email_address'), Input::post('password'));
Have a nice day :)
Uku Loskit ask you the right syntax.
If you want to retrieve always a single result, you can merge the code:
$user = Model_User::find()
->where('email_address', Input::post('email_address'))
->where('password', Input::post('password'))
->get_one();
An advice for you: be careful using directly Input::post('var_name'), it would be better to use a validation before saving variables.
Another way is to set the framework to perfrom some action, like htmlentities(), for every $_GET and $_POST variable.
I'm setting up a caching system in PHP for database queries.
So when a query like "SELECT * FROM table" is called, it either returns the cached results of that query or the results directly from the DB.
$query = "SELECT * FROM table";
[...]
$data = mysql_query($query);
[...]
fwrite($file,json_encode($data));
[...]
The problem is I'm trying to save the query results to a file and I can't find a textual format that works. I've tried json_encode and serialize, and they both return null or 0. Is there a format that will work for this without having to do mysql_fetch_array() and then serialize?
This is the error I get with json, obviously because I haven't converted the result to an array:
Warning: [json] (php_json_encode) type is unsupported, encoded as null
why not:
use native mysql cache?
optimize queries so they work fast and don't need to be cached?
It may not be a perfect solution for the issue you are handling, but I have found Justin Vincent's ezSQL Class to be really handy for working with situations like this. The class includes a caching function, which would satisfy the needs you mentioned in the question above. (Bonuses being that it handles alot of the more obtuse PHP<=>MySQL interactions as well.)
I want to get the raw query for $query->count("*").
I have tried
$s=$query->count("*");
$s=$s->createCommand()->sql;
It does not work.
Usually I do $s=$query->createCommand()->sql; to get raw sql which works fine. How to get it for count('*'). Please help.
You can't use createCommand() on $s, because count() method returns integer|string, it's not an object. To get count sql, you can use:
$query->select('count(*)')->createCommand()->sql