Pagination not working Subsonic - sql

I am having trouble with the pagination. It does not work, i.e. List staffs count is always zero for the code below.
If I remove the Paged method, correct results appear. How else can I solve this? I am using Subsonic 2.2. Pls help - thanks
SubSonic.SqlQuery query = DB.Select().Paged(startIndex, pageSize)
.From<Staff>()
.InnerJoin(StaffLocation.Schema.TableName, StaffLocation.Columns.StaffId, Staff.Schema.TableName, Staff.Columns.StaffId)
.InnerJoin(StaffClientGroup.Schema.TableName, StaffClientGroup.Columns.StaffId, Staff.Schema.TableName, Staff.Columns.StaffId)
.InnerJoin(StaffOutcome.Schema.TableName, StaffOutcome.Columns.StaffId, Staff.Schema.TableName, Staff.Columns.StaffId);
query.Where("1").IsEqualTo("1");
if (regionId > 0) query.And(StaffLocation.Columns.RegionId).IsEqualTo(regionId);
if (clientGroup > 0) query.And(StaffClientGroup.Columns.ClientGroupId).IsEqualTo(clientGroup);
if (outcome > 0) query.And(StaffOutcome.Columns.OutcomeId).IsEqualTo(outcome);
query.Distinct();
query.OrderBys.Add(Staff.Columns.FirstName);
List<Staff> staffs = query.ExecuteTypedList<Staff>();
return staffs;

Put a breakpoint on the second to last line and when it's hit execute query.BuildSqlStatement() in the Visual Studio immediate window and inspect the generated SQL. That might help narrow it down.

Well, I can tell you that it does work :) and I have a feeling that...
Your startIndex is wrong - try using 1 or 2
Your pageSize isn't set
What is "Where("1").IsEqualTo("1")?
You might try to grab the SQL to see what's being produced...

Related

Product Index Using Django ORM

I have a list of Products with a field called 'Title' and I have been trying to get a list of initial letters with not much luck. The closes I have is the following that dosn't work as 'Distinct' fails to work.
atoz = Product.objects.all().only('title').extra(select={'letter': "UPPER(SUBSTR(title,1,1))"}).distinct('letter')
I must be going wrong somewhere,
I hope someone can help.
You can get it in python after the queryset got in, which is trivial:
products = Project.objects.values_list('title', flat=True).distinct()
atoz = set([i[0] for i in products])
If you are using mysql, I found another answer useful, albeit using sql(django execute sql directly):
SELECT DISTINCT LEFT(title, 1) FROM product;
The best answer I could come up with, which isn't 100% ideal as it requires post processing is this.
atoz = sorted(set(Product.objects.all().extra(select={'letter': "UPPER(SUBSTR(title,1,1))"}).values_list('letter', flat=True)))

phalcon querybuilder total_items always returns 1

I make a query via createBuilder() and when executing it (getQuery()->execute()->toArray())
I got 10946 elements. I want to paginate it, so I pass it to:
$paginator = new \Phalcon\Paginator\Adapter\QueryBuilder(array(
"builder" => $builder,
"limit" => $limit,
"page" => $current_page
));
$limit is 25 and $current_page is 1, but when doing:
$paginator->getPaginate();
$page->total_items;
returns 1.
Is that a bug or am I missing something?
UPD: it seems like when counting items it uses created sql with limit. There is no difference what limit is, limit divided by items per page always equals 1. I might be mistaken.
UPD2: Colleague helped me to figure this out, the bug was in the query phalcon produces: count() of the group by counts grouped elements. So a workaround looks like:
$dataCount = $builder->getQuery()->execute()->count();
$page->next = $page->current + 1;
$page->before = $page->current - 1 > 0 ? $page->current - 1 : 1;
$page->total_items = $dataCount;
$page->total_pages = ceil($dataCount / 100);
$page->last = $page->total_pages;
I know this isn't much of an answer but this is most likely to be a bug. Great guys at Phalcon took on a massive job that is too big to do it properly in their little free time and things like PHQL, Volt and other big but non-core components do not receive as much attention as we'd like. Also given that most time in the past 6 months was spent on v2 there are nearly 500 bugs about stuff like that and it's counting. I came across considerable issues in ORM, Volt, Validation and Session, which in the end made me stick to other not as cool but more proven solutions. When v2 comes out I'm sure all attention will on the bug list and testing, until then we are mostly on our own. Given that it's all C right now, only a few enthusiast get involved, with v2 this will also change.
If this is the only problem you are hitting, the best approach is to update your query to get the information you need yourself without getPaginate().

How can I make rowCount return 1 and not 0?

I know that if I try to make an update using PDO and the affected rows are 0, then rowCount returns 0.
As I'm working on a new administration panel I was wondering how can I fix this because somebody ( the administrator ) can try to save something and for me rowCount 0 means no update in database and for this I'm showing an error.
I'm thinking. Can be some error for an Update and return 0 at rowCount unless if there ware 0 affected rows ? Because if not, then the next verification should fix my problem.
if($q->rowCount() == 0 || $q->rowCount() == 1) { show success message }
So is this a good practice ? Any advice please ?
Thank you and sorry for my language.
PDO implements methods that return error information.
Depending on what you're trying to do, you should look at
PDO::errorCode(), or
PDOStatement::errorCode()

Semantic Zoom Catastrophic failure on empty group

I am using the SemanticZoom control with a grid view and collection view source. All is working fine until I attempt to select ('jump to') an empty group - this causes an unhandled Catastrophic failure.
http://social.msdn.microsoft.com/Forums/nb-NO/winappswithcsharp/thread/6535656e-3293-4e0d-93b5-453864b95601
Does anybody know if there is a way to fix this - I want to 'allow' an empty group if I can.
Thanks
Okay, so I ran into this issue and fixed it. I know this is quite an old thread, but I'll answer it in case someone else runs into this. Here's how I did it.
The issue seems to be that the group set as the DestinationItem needs to have items in it. In the case that this group is empty, this poses a problem. The problem does not occur when the group is in between other filled groups, because it has a length of 0 and therefore is untargetable. When it is at the end (or possibly the beginning, haven't tried that), this poses an issue because the GridView/ListView targets the last one in the list when the mouse is past the end of the view.
So, how to solve it:
You need to add an event handler for OnViewChangeStarted. In this handler, you will have access to the SemanticZoomViewChangedEventArgs.
The first thing you need to do is find out if the Group being targetted is empty. I am using a custom Group class here, but have cast it to IEnumerable, just because I only need to know how many items are in it. Linq allows me to find the count of the IEnumerable.
using System.Linq;
private void Zoom_OnViewChangeStarted(object sender, SemanticZoomViewChangedEventArgs e)
{
var group = e.DestinationItem.Item as IEnumerable;
if (group != null && group.Count() == 0)
{
e.DestinationItem.Item = MyViewModel.Groups.Last(itemGroup => itemGroup.Count() > 0);
}
}
As you'll note, the next step is to set the DestinationItem.Item to the Last group that has items in it. You may also want to handle the First group that has items in it, but that's quite easy to do as well. In place of MyViewModel, insert your ViewModel. In place of Groups, insert your collection property.
I hope this helps and happy coding!

Rails 3 Error when using OR with a where clause

I'm having a strange issue when using the Where clause in Rails. I imagine it has something to do with my OR operator syntax. Here is my query:
Cookie.where("ID = ? OR ID = ? OR ID = ? OR ID = ?", chocolate.to_s, sugar.to_s, peanut_butter.to_s, oatmeal.to_s)
When I attempt to execute the query my application just hangs endlessly. However, If I submit a query that looks like this:
Cookie.where("ID = ?", chocolate.to_s)
I get the expected result. Any help with the proper format to this where clause would be greatly appreciated. Thanks.
UPDATE
I went into the rails console and did as you suggested. This was the result:
SELECT \"TBLCookies\".* FROM \"TBLCookies\" WHERE (ID in ('4','3','2','1'))
This seems correct. I opened up SQL Developer, pasted this query in, and ran it. I got the expected result. So everything seems to be fine there. However, when I try to run the query in the rails console I get nothing back. The console just hangs indefinitely. Any insight into what could be going wrong would be great.
Why not do this
Cookie.where("ID in (?)",[chocolate, sugar, peanut_butter, oatmeal].collect(&:to_s))