hey guys i have been struggling with this .i am a trying to select from a table using cakephp find all e.g
i want to select all from a table where (A == 1 AND B == 2) OR (A == 2 and B == 1) with just on query
here is my code so far
$conditions = array("OR"=>
array("Message.to_to"=>$daddy["User"]["id"],
"Message.from_from"=>$this->Auth->user("id")),
array("Message.to_to"=>$this->Auth->user("id"),
"Message.from_from"=>$daddy["User"]["id"])
);
To get your expected result (A == 1 AND B == 2) OR (A == 2 and B == 1), try nesting the 'and' conditions which are currently missing from your code.
You also need to specify the conditions parameter.
Try the following:
$conditions = array(
'conditions' => array(
"or"=> array(
"and" => array(
"Message.to_to"=>$daddy["User"]["id"],
"Message.from_from"=>$this->Auth->user("id"),
),
"and" => array(
"Message.to_to"=>$this->Auth->user("id"),
"Message.from_from"=>$daddy["User"]["id"],
),
),
),
);
Reference: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
Related
I have two dates 30-01-2017 & 02-02-2017
Need a condition in cakephp to search record these two dates doesn't lies in between.
Please use this.I think this would be helpful.
$searchFirstdate = '30-01-2017';
$searchLastdate = '02-02-2017';
$this->ModelName->find(
'all',array(
'conditions'=>array(
'ModelName .date_field >= ' => $searchFirstdate,
'ModelName .date_field <= ' => $searchLastdate
)
)
);
There is a NOT BETWEEN operand for MYSQL, so you could easily use:
$this->ModelName->find('all', array(
'conditions' => array(
'ModelName.date_field NOT BETWEEN ? AND ?' => array('30-01-2017', '02-02-2017')
)
));
From CakePHP Manual
I have a task planning app and I'm attempting to fetch tasks based on two criteria: either they have a due date within the next 2 weeks, or the task itself is ending within 2 weeks. Here is my non-working code:
$conditions['AND'] = array(
'OR'=>array(
'AND'=>array(
'Task.due_date >'=> $now,
'Task.due_date <'=> $twfn
),
'AND'=>array(
'Task.end_time > '=> $now,
'Task.end_time <' => $twfn
)
)
);
There are additional conditions, hence the outer $conditions['AND']
The SQL log shows:
WHERE ((((`Task`.`end_time` > '2015-06-30') AND (`Task`.`end_time` < '2015-07-14')))
Which implies that the OR isn't being evaluated. If it's relevant, every task has an end_time, but not necessarily a due_date.
Any help or pointers in the right direction would be much appreciated!
use this: the ANDs should be in array, because you are giving the same key(AND) for the array and the last one will be used
$conditions['AND'] = array(
'OR'=>array(
array(
'AND'=> array(
array(
'Task.due_date >'=> $now,
),
array(
'Task.due_date <'=> $twfn
)
),
),
array(
'AND'=>array(
array(
'Task.end_time > '=> $now,
),
array(
'Task.end_time <' => $twfn
)
)
)
)
);
more examples from docs
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
I'm trying to translate this query in a find, but without success. My find doesnt return anything.
My SQL (in postgresql)
select * from projectversion where project='10003' releasedate>=now()::date or releasedate is null
MY FIND
$projectversions = $this->find('list', array(
'recursive' => -1,
'fields' => array('vname'),
'conditions' => array('project' => $id_project,'releasedate >=now()::date','OR'=>array('releasedate is null'))));
Anyone can help me?
Your original SELECT doesn't appear to be valid:
select * from projectversion where project='10003' releasedate>=now()::date or releasedate is null
Should it be:
select * from projectversion where project='10003' AND (releasedate>=now()::date or releasedate is null)
If so, your conditions should look like:
'conditions'=>array(
'Model.project'=>10003,
OR=>array(
'Model.releasedate >= NOW()'
'Model.releasedate IS NULL'
)
)
produces:
WHERE Model.project = 10003 AND (Model.releasedate >= NOW() OR Model.releasedate IS NULL)
This is my first post on this site, so please be kind.
I have no trouble making this work using the find() method, but when I try to run it using the paginate component, no luck. I am simply trying to run a Haversine formula in the fields options to calculate distance. In the Cake Book, it says "You can also include other find() options, such as fields:". There is no caveat mentioned.
So, this is what I am trying to run:
$this->paginate = array(
'joins' => array(
array(
'table' => 'ride_types',
'alias' => 'RideTypes',
'type' => 'inner',
'conditions'=>array('RideTypes.id = Rides.ride_type')
)
),
'group' => array(
"Rides.id HAVING distance < $filter_radius AND ride_time >= '$filter_earliest_time' AND ride_time <= '$filter_latest_time' AND ride_date >= '$filter_earliest_date' AND ride_date <= '$filter_latest_date'"
),
'fields' => array(
"( 3959 * acos( cos( radians($filter_lat) ) * cos( radians( Rides.lat ) ) * cos( radians( Rides.lng ) - radians($filter_lng) ) + sin( radians($filter_lat) ) * sin( radians( Rides.lat ) ) ) ) AS distance"
),
'limit' => 5,
'page' => $paginate_page,
'order' => array('Rides.start_time DESC'),
);
$rides = $this->paginate('Rides',$options['conditions']);
I get this error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause'
SQL Query: SELECT COUNT(*) AS count FROM velobuddies.rides AS Rides inner JOIN velobuddies.ride_types AS RideTypes ON (RideTypes.id = Rides.ride_type) WHERE 1 = 1 GROUP BY Rides.id HAVING distance < 5
Again, if I apply all these exact same options to the find('all') method, it works perfectly, so I know the logic is correct... or at least workable.
So what is going on here?
Daniel this post may have the answer although it is based on CakePHP 1.3. It involves binding the Models based on the aggregation.
http://cakephp.1045679.n5.nabble.com/Pagination-with-HasMany-and-SQL-Aggregation-td1307450.html
It looks like you may have an issue with your model name. If your application follows the correct CakePHP conventions, then you should be using Ride.field instead of Rides.Field. In CakePHP queries you want to reference the Model and not the table name.
$v = array(1,11.38,15.8);
$sortByPrice = $this->Product->query
(
"SELECT *,
CASE `currency`
WHEN '1' THEN $v[0]
WHEN '2' THEN $v[1]
WHEN '3' THEN $v[2]
END AS 'ratio'
FROM products
ORDER BY price*ratio DESC
"
);
i want to convert what is above to a find function
i tried something like that(but it do not work)..
$v = array(1,11.38,15.8);
$bla = $this->Product->find('all', array(
'conditions' => array(
'Product.currency',
'((
CASE WHEN
Product.currency=1 THEN $v[0]
Product.currency=2 THEN $v[1]
Product.currency=3 THEN $v[2]
END
)) AS ratio'),
'order' => 'ratio',
'limit' => 10
));
can somebody to convert query into find
You are putting into the conditions key. Move it into the fields key or make it a virtualField - that may work.
Edit: also $v[0] in single quotes will not actually replace it with the variable - it will just appear as that text.