Hi Guys I am working on Parse php SDK ,Where I need to run a OR query but it does not working .I have read some of the article about this but did not get succcess (https://www.parse.com/questions/parsequeryequalto-or-parsequeryequalto)
This is my code
$query1 = new ParseQuery("Chat");
$query1->equalTo("sender", "emraanhashmi000");
$query2 = new ParseQuery("Chat");
$query2->equalTo("receiver", "emraanhashmi000");
$query3 = ParseQuery.orQueries($query1,$query2);
$result = $query3->find();
Please suggest me how can I run Or query in php
Given that $query1 and $query2 gives you the result you expect, you should use the following syntax to OR them together.
$query3 = ParseQuery::orQueries([$query1, $query2]);
$result = $query3->find();
Also, remember that parse.com is closing down and you should migrate to the open-source "parse-server" ASAP.
Related
I have a Magento issue. I'm using Magento ver. 1.9.1.1.
The issue is that I am seeing duplicate results for a page which contains a list of jobs (its a recruitment page). The problem is each of the results is duplicated. I've never done any real Magento development before and it seems like a steep learning curve.
I've checked the content and that has only been entered once.
This is the offending code:
//I tried this line with no effect
//$collection = Mage::getModel('cms/page')->getCollection()->distinct(true);
// $collection contains the duplicate results
$collection = Mage::getModel('cms/page')->getCollection();
Can anyone give me any ideas on how I can solve this? Even an idea of where to look in the code would be good.
I've found two data structures when iterating through the collection. These are _origData and _Data. Don't know why its using both of these but I managed to fix/hack it by doing:
if($key == "_origData"){
continue;
}
Surely there's a better way to do this?
Thank you in advance :)
Have you tried applying filters to your collection?
See below:
$getStoreId = Mage::app()->getStore()->getId();
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter($getStoreId)
->addFieldToFilter('is_active', 1);
Resources:
Class Mage_Cms_Model_Mysql4_Page_Collection
You can see the code details on above mentioned Model class.But if you want custom code you can try like this :
$collection = Mage::getModel('cms/page')->getCollection();
$collection->getSelect()
->join(
array('s' => $collection->getTable('cms/page_store')),
's.page_id = main_table.page_id AND s.store_id != 0',
array('store_id')
)
->columns(array('stores_count' => new Zend_Db_Expr('COUNT(s.store_id)')))
->group('main_table.page_id')
->having('stores_count = ?', 1)
->having('s.store_id = ?', $storeId)
;
I have a URL www.mydomain.com/jobs?applications=2|4|6
I am trying to get the values to work with my Input::get but failing. I have tried using array but this doesn't work. Can anyone advise? I'm unfamiliar with using Laravel with this structure of querystring.
$applicationIDs = Input::get('applications');
$applications = Job::with('users');
if(!empty($applicationIDs)){
$applications->whereIn('id', $applicationIDs);
}
$applications = $applications->get();
Your applications parameter is just a string. Use explode to turn it into an array of ids:
$applicationIDs = explode('|', Input::get('applications'));
Yes,I understand I can cache queries like this
$CI->db->cache_on();
$query = $CI->db->query("select photo,gender from users where id=".$id);
$CI->db->cache_off();
But I need it in this way
$CI->db->cache_on();
$CI->db->select(array('photo','gender'))->from('users')->where('id',$id);
$CI->db->cache_off();
$row = $CI->db->get()->row_array();
I'm getting the data,but nothing is created in cache/ directory . So I think it's not being cached.
Can someone please help on how to cache in the second case?
Update
when I do this
$CI->db->cache_on();
$CI->db->select(array('photo','gender'))->from('users')->where('id',$id)->get();
$CI->db->cache_off();
$row = $CI->db->row_array();
PDO driver says, row_array() is an undefined function.
In
$CI->db->cache_on();
$CI->db->select(array('photo','gender'))->from('users')->where('id',$id);
$CI->db->cache_off();
$row = $CI->db->get()->row_array();
move the get method to be called before the cache_off method is called, like this
$CI->db->cache_on();
$query = $CI->db->select(array('photo','gender'))->from('users')->where('id',$id)->get();
$CI->db->cache_off();
$row = $query->row_array();
Then it is working like your manual query :)
It is doing that because the query doesn't run until you call the get method, hence why the row_array doesn't exist
I have been working on this for like more than 4 hours. I have select query using active record I am doing: $this->db->like ('items.name',$search);
everything works fine but whenever there is single quote (') in the $search string it gives this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's%' OR default_items.short LIKE 'faith\'s%' LIMIT 5' at line 5
I have just checked now that it is adding double back slashes \\ instead of single in my active record for LIKE query. I tried in MySQL bt removing one slash and it is working.
My code:
$q = "faith's";
$query = $this->db->select('items_categories.slug as category_slug, items_categories.name as cat_name, items.name, items.price_value, items.cover_photo, items.slug');
$query->select('default_items.short as short',false);
$query->select('date(default_items.date_created) as date_created',false);
$query->join('items_categories','items_categories.id=items.root_id','inner');
$query->join('users','items.company_id=users.id','inner');
$query->like('items.name',$q);
$query->or_like('items.short',$q);
$query->limit(5);
$result = $query->get($this->_table);
$both_prod_results = $result->result();
I am using pyrocms 2.x.
You can try the following code maybe it can help you, but you have to add \ before every' in your requests :
$value = "faith\'s";
$sql_request = "`short` LIKE '%". $value ."%'";
$query = $this->db
->select('*')
->where($sql_request, null, false)
->get('default_items');
$result = $query->result();
dump($result);
i think i need to answer my own question.
Well this is a hack(don't think if it is secure)
I have patched my MYSQLI Driver:
i have replaced this:
return str_replace(array($this->_like_escape_chr, '%', '_'),
array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
$str);
with this:
return str_replace(array($this->_like_escape_chr, '%', '_'),
array($this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
$str);
it was adding extra slash. and also don't think it will allow sql injections etc anything.
if anyone knows this is right then please comment.
Thanks
Umair
This is my active record code in CodeIgniter:
$this->db->...
$this->db->join('post_likes', 'post_likes.user_id="'.$this->db->escape($online_user).'" AND post_likes.post_id=post.id', 'left');
And this is how it is interpreted:
LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id
it gives the error:
`user_id`="`3"`
How to write a direct number in active record?
Update:
removing escape
to test it on your computer you dont need to have a database. Just trying this code shows the error:
$this->db->select('*')
->from('mytable')
->join('post_likes', 'post_likes.user_id="3" AND post_likes.post_id=post.id', 'left');
$query=$this->db->get('');
var_dump($this->db->last_query());
exit(0);
result:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '` AND post_likes.post_id=post.id' at line 3
SELECT * FROM (`mytable`) LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id
You SHOULD not use the double quotes in SQL query:
$this->db->join('post_likes', "post_likes.user_id = $online_user AND post_likes.post_id=post.id", 'left');
Update:
This is a bug in the current CI stable version (fixed in v3.0-DEV), CI ActiveRecord methods (which doesn't implement really ActiveRecord) are prepared for simple usages.
I fixed this issue before by hacking the core files (by adding a parameter to join method to disable _protect_identifires).
There we go:
In system/database/DB_active_rec.php line #310, add $escape as 4th parameter:
public function join($table, $cond, $type = '', $escape = TRUE)
And change $match[3] = ... to:
if ($escape === TRUE)
{
$match[3] = $this->_protect_identifiers($match[3]);
}
So, you can use join($table, $cond, $type = '', $escape = FALSE) to disable escaping.
In addition, setting _protect_identifires globally to FALSE is not in a correct direction.
the only option remains is using custom query():
$sql = "SELECT * FROM some_table WHERE id = ?"
$this->db->query($sql, array(3));
Try this
$this->db->join('post_likes', "post_likes.user_id=$the_userid AND
post_likes.post_id=post.id", 'left');
or
$this->db->join('post_likes', 'post_likes.user_id="'.$the_userid.'" AND
post_likes.post_id=post.id', 'left');
Update:
Define
$db['default']['_protect_identifiers']= FALSE;
in "application/config/database.php" at the end.
Simple solution would be to temporarily set the protect_identifiers off before join query, like so:
$this->db->_protect_identifiers = false;
After making join query you could set it back to true
Works for me in CodeIgniter version 2.1.2
try this one
$this->db->join('post_likes', 'post_likes.user_id="{$online_user}" AND post_likes.post_id=post.id', 'left');
please let me know if you face any problem.
Dont use $this->db->escape
$this->db->join('post_likes', 'post_likes.user_id="'.$online_user.'" AND post_likes.post_id=post.id', 'left');