I have a long query like this:
$a=$this->Signin->find('all',
array(
'fields'=> array(
.....
),
'conditions' => array('mytable.user_type ...),
'order' => ....
'limit' => ....
));
user_type column can take values of 0 or 1.
I have a function parameter $type, regarding to this parameter, I need to change my SQL query. Regarding to $type I will query for
"mytable.user_type"=1,
"mytable.user_type"=0,
"mytable.user_type"=1 OR "mytable.user_type=0
I don't want to write same query three times. I want to change only conditions part. Is it possible?
you can define $type as an array and manage the values in $type
Like for multiple values
$type = array(1,0);
'conditions' => array('mytable.user_type'=>$type)
for single value
$type = array(1);
'conditions' => array('mytable.user_type'=>$type)
Hence you just need to manipulate $type as per condition, Hope this work
Try this:
$user_type = ($type == 'something') ? 1 : 0 ;
then, in your conditions:
'conditions' => array('mytable.user_type' => $user_type);
given that user_type can take only two values, your third query ("mytable.user_type"=1 OR "mytable.user_type=0) is unnecessary
EDIT :
if($type == 'conditions_where_usertype_is_necessary') $user_type = ($type == 'something') ? 1 : 0 ;
then, for your conditions:
if( isset($user_type) ) {
$options['conditions']['mytable.user_type'] = $user_type;
}
$options['conditions']['other_conditions'] = ...;
$this->Signin->find('all', $options);
Related
Im looking to create a simple query but i can't seem to figure out how to do it using Cakes conditions (cake 1.3), what im looking to do is quite simple, in SQL its just
SELECT * FROM table WHERE firstvalue != '' OR secondvalue != ''
So basically only return row if there is a value in either firstvalue or second value.
At the moment i have;
$conditions = array(
"NOT" => array(
'firstvalue' => ''
)
);
This works fine for the first value, but if i try and add anything to it, it still only returns the results for the firstvalue
Two ways to go about this:
$conditions = array(
'OR' => array(
array('NOT' => array('firstvalue' => '')),
array('NOT' => array('secondvalue' => ''))
)
);
Or, take advantage of the fact that "NOT a OR NOT b" == "NOT (a AND b)":
$conditions = array(
'NOT' => array(
array('firstvalue' => ''),
array('secondvalue' => '')
)
);
To add multiple fields in OR condition, you have to write the fields in array.
Like,
$conditions = array(
"OR" => array(
"NOT"=>array('firstvalue' => ''),
array('secondvalue'='')
)
)
);
I hope it will work for you.
I want to display all documents (select *) with sub-documents in PHP.
I know how to query all find() but I have no idea how to do it when I have sub-documents. I don't know if there's something like find() or I need to make loops fo every sub-documents that I'd have.
This would be the code
$mongodatabase->insertOne(
['name' => 'Alex',
'surname' => 'Turner',
'country' => 'England',
'birth' => array(
'day' => 6,
'month' => 'january',
'year' => 1986
),
]);
Something easy, just to learn. When I try a var_dump of day I get Undefined index and NULL.
$client = new MongoDB\client;
$db = $client->database;
$mongodatabase = $db->document;
$document = $mongodatabase->find();
foreach ($document as $doc) {
var_dump($doc->day);
}
However, I'd like to query all.
Use $exists - It helps us in identifying the elements which are not empty
db.collection_name.find({
"birth.day" : {
$exists : true
}
});
If you need to check not null and empty, then we need to use $type together with $exists, $type can be passed with different values and 10 is for null check
db.collection_name.find({
"birth.day" : {
$not : { $type : 10 },
$exists : true
}
});
when u find the exactly data from mongoldb u can use the shelter to limit the field
eg:
db.xxxxx.find(
{'status':'DELIVRD'}
);
I'm using LIKE query but when specialist_id's order change it say no records found,
if specialist_id array have [1,2,3] in it then all the users with three specialties should be in results regardless of sequence, sequence array may have [3,2,1] or [2,1,3] but the users with these three specialties should be in results, here is my code with LIKE query:
$field = $this->User->find(
'all', array(
'conditions'=>array(
'User.specialist_id LIKE' =>'%'.$value.'%',
'User.role'=>'careproviderRole',
'User.gender'=>$this->request->data['gender'])
)
);
foreach ($field as $key => $value)
{
$field[$key]['Specialist'] = $user;
}
I have also tried with FIND_IN_SET:
$field = $this->User->find(
'all', array(
'conditions' => array(
'User.role' => 'careproviderRole',
'FIND_IN_SET(\''.$this->request->data['specialist_id'].'\', User.specialist_id)')
)
);
When you use LIKE with mask '%some%', it find all results with substring 'some'.
If you need to get some specific set of ids from table (1, 2 or 3), then use sql operator IN.
Example:
SELECT `specialist_id` FROM `User` WHERE `specialist_id` IN (1,2,3);
In your code it will be:
$field = $this->User->find(
'all', array(
'conditions'=>array(
'User.specialist_id' => $arrayOfSpecialistIds,
'User.role'=>'careproviderRole',
'User.gender'=>$this->request->data['gender'])
)
I have an api with one optional parameter called limit, which takes an integer and limits the number of documents returned from the api in a get request.
Implementing this limit is fine in my PHP application when it is a required parameter, but when its not specified as part of my get request, what is the best way to handle it?
is there for example, a way to define $limit and set it to all documents? (in pseudo code, $limit = none)
$list = $collection->aggregate( array( array('$match' => array( ... )),
'$project' => array ( ... )), '$limit' => intval($this->limit) ));
$this->limit //this is the optional input parameter
As you can see above, when the limit parameter is required it functions as required. Now when its ommitted, how can I keep the above code but specify no limit?
You could manually generate your where clause.
// Declare a where clause with your fixed conditions
$whereClause = array(array('$match' => array(...)), array('$project' => array(...)));
// Check if there's a limit
if($this->limit != 0)
array_push($whereClause, array('$limit' => $this->limit));
// Finally, call with the where clause we've generated above
$list = $collection->aggregate($whereClause);
Short answer No!
But, you can use an if/else condition like this:
<?php
if(intval($this->limit) != 0 ) {
$list = $collection->aggregate( array(
array('$match' => array( ... )),
array('$project' => array ( ... ))
array('$limit' => intval($this->limit))
);
));
} else {
$list = $collection->aggregate( array(
array('$match' => array( ... )),
array('$project' => array ( ... )),
);
}
?>
I'm working on a project for PHPBB. I have a problem that I get some $row types. And they have some "id"s from MySQL, But I want theese "id"s by one by.
It's SQL query ;
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'u.*, z.friend, z.foe, p.*',
'FROM' => array(
USERS_TABLE => 'u',
POSTS_TABLE => 'p',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(ZEBRA_TABLE => 'z'),
'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
)
),
'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
AND u.user_id = p.poster_id'
));
$result = $db->sql_query($sql);
And then for user_id's
$rowset[$row['post_id']] = array(
'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
'user_id' => $row['user_id'],
.
.
I logged $row[user_id] for in a topic and it return ;
97777
97778
97779
97783
But I want just first id i mean -> "97777" . So how can i pass just "97777" to a variable?
Dont suggest about sql level because I can't change any SQL command.
Dont suggest about sql level because I can't change any SQL command.
This is the problem though, your PHP is clearly selecting all 9 rows;
Using WHERE user_id = '97777' you will only select the record you want.
Why can you not edit the SQL? aren't you familiar with SQL or is the code your are using your own?
Without any code it is hard to say what you want to do:
reset($row);
$first_key = key($row);
or
reset($rows);
$first_key = key($rows);
Hard to guess without seeing any code.
You can limit your records using LIMIT clause.
SELECT column_name FROM table_name LIMIT 1;
It will return the first record of that table.