QueryOver HAVING clause with AND condition - nhibernate

I'm trying to create a HAVING clause with and AND in it and it creates a WHERE clause of it.
I tested it out and it seems like the problem only occurs when I have an AND or an OR.
Here you can see a simplified version of the code (both parts of the AND restriction is the same, just to simplify the problem).
eventQuery = eventQuery.Where(
Restrictions.And(
Restrictions.Le(
Projections.Conditional(
Restrictions.LeProperty( Projections.Property( () => aEvent.StartDate.End ), Projections.Max( () => aMeeting.Duration.Start ) ),
Projections.Max( () => aMeeting.Duration.Start ),
Projections.Property( () => aEvent.StartDate.End ) ),
DateTime.Now ),
Restrictions.Le(
Projections.Conditional(
Restrictions.LeProperty( Projections.Property( () => aEvent.StartDate.End ), Projections.Max( () => aMeeting.Duration.Start ) ),
Projections.Max( () => aMeeting.Duration.Start ),
Projections.Property( () => aEvent.StartDate.End ) ),
DateTime.Now ) )
);
Do you guys have any idea?

This problem doesn't seem to have a solution so we decided to create a View of this query instead so we can map it to a model and write the QueryOver with a Where clause against the selected-calculated fields.

Related

SQL - Select distinct but return all columns with CakePHP Find

I am looking to do the same thing as this using cakephp:
https://stackoverflow.com/a/6127471
$eachRWithCode = $this->MovieReview->find( 'all', array(
'fields' => 'MovieReview.*',
'group' => array('MovieReview.code'),
'recursive' => '-1',
'nofilter' => true,
'order' => 'MovieReview.code'
));
This code gives an error:
ERROR: column "MovieReview.id" must appear in the GROUP BY clause or be used in an aggregate function
what DBMS are you using? Mysql let you show columns that you don't use in aggregate function (but you are not , but ather DBMS don't. So every column you want to show you have to put it in a aggregate function such as MAX() or SUM() or COUNT()
so you have to do something like (assuming you have a score column):
$eachRWithCode = $this->MovieReview->find( 'all', array(
'fields' => array(
'COUNT(MovieReview.id)',
'AVG(MovieReview.score),
'MAX(MovieReview.code)
),
'group' => array('MovieReview.code'),
'recursive' => '-1',
'nofilter' => true,
'order' => 'MovieReview.code'
));

How to fitch the result from Yii active recorde query with relations and scopes

This is my code
$vacanciesObjects = Vacancies::model()->status('approved')->visibility('Visible')->removed(0)->archived(0)->findAll(
array(
'with'=>array(
'job'=>array(
//'select' => 'title',
'scopes'=>array(
'offline' => array(0),
),
'with' => array(
'employer'=>array(
'scopes'=>array(
'status_id_not_in' => array('Blocked'),
),
),
),
),
), 'limit'=> 5, 'condition' => 'number_of_views > 0', 'order' => 'number_of_views DESC',
)
);
I can get the values for the columns in table vacancies but not from other table, any help regarding this ?
sorry I'm newbie on Yii
I figure out this problem by this
$vacanciesObject->job['title']

converting sql query into cakephp format

Hi
i need the following sql query into cakephp find() format. the query it self is working fine but i need to change it.
$this->Part->query(
"SELECT `parts`.`id`,`parts`.`part_name`
FROM `parts`
LEFT JOIN (
SELECT `op` . *
FROM `order_parts` AS `op`
WHERE `op`.`order_id` =".$this->Session->read('orderid')."
) AS `vT`
ON ( `parts`.`id` = `vT`.`part_id` )
WHERE `vT`.`part_id` IS NULL"
);
thanks
If your relationship are Order HABTM Part and you have a table orders_parts with columns: id, order_id,part_id you should be able to do something like this:
First, get the ids of the parts which are in the order:
//each Part has one OrdersPart per order
$this->Part->bindModel(array('hasOne' => array('OrdersParts')));
$parts = $this->Part->find('list', array(
'fields' => array('Part.name'),
'conditions' => array(
'OrdersParts.order_id' => $this->Session->read('orderid'),
),
'recursive' => 2
));
Now get the parts which are not in the order:
$this->Part->find('all', array(
'conditions' => array(
"NOT" => array('Part.id' => array_keys($parts))
),
));

Cakephp 1.3 - Ordering a group of results

I have the following model:
$values = $this->PtlUserdata->find('all', array(
'fields' => array(
'PtlUserdata.field',
'PtlUserdata.value',
'PtlUserdata.timestamp'
),
'conditions' => array(
'PtlUserdata.user_id' => $user->get('id'),
),
'order' => array('PtlUserdata.timestamp' => 'DESC'),
'group' => array('PtlUserdata.field')
));
I'm trying to order the results by timestamp and then group the results by field, so I can get the most recent record with that field name.
Does anybody know how to do this is cakephp?
Thanks in advance for any help, much appreciated :)
This is the default behavior for the database. You will have to come up with some sort of workaround to do it.
An idea: CakePHP: Group by ID and Order by date
Does this work:
$values = $this->PtlUserdata->find('all', array(
'fields' => array(
'PtlUserdata.field',
'PtlUserdata.value',
'max(PtlUserdata.timestamp)'
),
'conditions' => array(
'PtlUserdata.user_id' => $user->get('id'),
'PtlUserdata.timestamp' => 'max(PtlUserdata.timestamp)',
),
'group' => array(
'PtlUserdata.field',
'PtlUserdata.timestamp'
)
));
I haven't had the chance to test this but you definitely need to be grouping by PtlUserdata.timestamp.

Aggregate query in fluent nhibernate

select T.Id, count(Th.ampount)
from TH, T
where Th.Tid= T.id
group by T.Id
How to write above query using fluent nhibernate. I don't want to use CreateSQLQuery().
session.QueryOver<TH>()
.JoinAlias(th => th.Ts, () => tAlias)
.SelectList(list => list
.SelectGroup(th => th.Id)
.SelectCount(() => tAlias.amount)
// or did you mean
.SelectSum(() => tAlias.amount)
)
.List<object[]>();