I'm a fan of CakePHP, but some times it can do some goofy things. One such example is the afterFind() Model callback, whose $results parameter may contain data in two different formats depending on how the find results were retrieved elsewhere in the code. This breaks the blackbox concept of functions in which the function shouldn't care less of what happens outside it's scope. Because of this oddity, you have to support either the model data existing in the $results array under a key named after the model, or the data existing directly in the $results array as simple key/value pairs. I attempted to make this easy to deal with by doing the following:
[php]$res = ($primary)
? &$result['MyModel']
: &$result;[/php]
But for whatever reason, PHP fails with an "unexpected &" error after the ternary `?` operator. The ternary syntax is just a shorthand I use for a really easy if-then, so I decided to expand it and see what happens.
[php]if($primary) {
$res = &$result['MyModel'];
} else {
$res = &$result;
}[/php]
This code runs without a hitch. So then what's happening? Your guess is as good as mine. I honestly can't discern what's making PHP break on the ternary version. Although the ternary syntax is rather simple and can be considered synonymous to the if-then code, PHP seems to handle them differently and consequently breaks reference assignment when using ternary.
Sunday, November 28, 2010
Thursday, October 7, 2010
Deleting Duplicate Rows in SQL Server Based on a Single Column
An import from a client's product list resulted in some incorrectly duplicated products. Normally this would be easy to sort out, except that the duplicates differed by a category column, so the good ol' DISTINCT keyword was of no help. This would also have been easy to do if working with MySQL by using a GROUP BY clause to select a row by distinct instances of a particular column. SQL Server, however, is another story. As I found, if you try the GROUP BY trick, SQL Server will whine.
I found some reasoning for this online, which is all well and good, except that MySQL does this like a champ and makes life easier as a result. So off I went to find a solution for SQL Server... I eventually stumbled on a page from the SQL Authority, by Pinal Dave (a very humble guy, judging by the title of his site). The gem, however, wasn't the post itself but a comment by Madhivanan further down the page. Thanks to Madhivanan, I came up with the following solution.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'ItemNumber' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I found some reasoning for this online, which is all well and good, except that MySQL does this like a champ and makes life easier as a result. So off I went to find a solution for SQL Server... I eventually stumbled on a page from the SQL Authority, by Pinal Dave (a very humble guy, judging by the title of his site). The gem, however, wasn't the post itself but a comment by Madhivanan further down the page. Thanks to Madhivanan, I came up with the following solution.
DELETE t FROM(select (row_number() over (partition by ItemNumberorder by ItemID)) as cnt, * from Item) as tWHERE t.cnt > 1
This snippet will give you a count of how many times a particular item number appears in my database's Item table. We isolate only the ones that appear more than once and delete them from the table. Not as simple as using a GROUP BY, but it gets the job done.
Friday, March 26, 2010
Hyperterminal Replacement For Linux
This is a little gem I found recently when I needed to communicate with my OpenLog board. A lot of tutorials want you to pull up HyperTerminal in Windows to talk to OpenLog over the USB->UART bridge. My biggest problem with this is that I'm primarily a Linux user, where HyperTerminal is unavailable. That's all well and good, however, I'll just boot into my Windows 7 install and do it, right? Wrong. Windows 7 no longer ships with HyperTerminal. I jumped back to my Ubuntu Linux install and started hunting for a HyperTerminal replacement. The best solution I ended up finding was a command-line application called minicom. This handy little app is a bit to get the hang of, but once you're using it, it works like a charm. Here's a quick rundown of connecting to a serial device over USB using Minicom.
Wednesday, March 24, 2010
Flashing OpenLog Firmware in Ubuntu Linux
I recently hit a bug in the Sparkfun OpenLog v1.1 firmware that left the device useless. I found out the hard way that version 1.1 only supports up to 255 log files. Once it hits this limit the firmware doesn't know what to do with itself and loops endlessly. This even prevents you from entering command mode where you could otherwise reset the log number. Nate Seidle at SparkFun quickly released an update, v1.2, to correct this problem I was having. But now I had to figure out how to flash the firmware on my OpenLog. It turns out the process is extremely easy in Ubuntu, but the GitHub documentation targets mostly Windows, so I decided to document the process for Ubuntu users from start to finish here.
Labels:
arduino,
linux,
OpenLog,
Programming,
sparkfun,
Technology,
ubuntu
Thursday, January 14, 2010
8-bits of Processing Goodness
The last semester of my undergrad program in CSE is finally here! This semester I have a Senior Design course where students form their own groups and come up with an idea related to the curriculum and implement it. Now if only I had an idea of what to design...
I've been participating in autocross events for years and have always wished I could afford a data acquisition system like those you see used in Formula 1, Indy, Le Mans, and/or NASCAR. With a DAQ, I'd be able to see exactly what my car is doing at an event and use that information to help me drive the course faster on my next run. Hopefully.
That got me thinking. Surely I'm not the only amateur autocrosser wishing for an affordable data acquisition system made for the weekend warrior. In fact, LOTS of people across the nation, and across the globe would probably love such a system. I didn't know of any that existed on a college student budget, so I figured why not make one? It just so happens this senior design semester is the perfect time to get started. But where to begin?
I've been participating in autocross events for years and have always wished I could afford a data acquisition system like those you see used in Formula 1, Indy, Le Mans, and/or NASCAR. With a DAQ, I'd be able to see exactly what my car is doing at an event and use that information to help me drive the course faster on my next run. Hopefully.
That got me thinking. Surely I'm not the only amateur autocrosser wishing for an affordable data acquisition system made for the weekend warrior. In fact, LOTS of people across the nation, and across the globe would probably love such a system. I didn't know of any that existed on a college student budget, so I figured why not make one? It just so happens this senior design semester is the perfect time to get started. But where to begin?
Labels:
arduino,
Cars,
electronics,
School,
senior design,
Technology
Sunday, November 29, 2009
PHP Recursive Strings
I needed to construct a recursive string of regular expression patterns. Parts of a string may be built from another string which may also be built of other strings. If you're familiar, think of production rules in a context-free grammar. If you're not, then just take for granted this recursive string idea would be useful. I initially decided to build all my patterns into a single array, giving me a nice data collection to access them. I was also under the assumption that my recursive idea would work best with an array as the interpreter would have to account for all strings in the array at once. This would prevent order issues if one string relied on another but wasn't yet declared. Unfortunately, PHP simply doesn't seem to work as I had hoped.
Consider the following snippet of code:
[code lang="php"]<?php
$a = array(
0 => "test {$a[1]}",
1 => "blah {$a[2]}",
2 => "argh"
);
echo $a[0]."\n"; // outputs: test
$s2 = "argh";
$s1 = "blah {$s2}";
$s0 = "test {$s1}";
echo $s0."\n"; // outputs: test blah argh
?>[/code]
At first, we attempt to use an array to handle the string recursion, then we use string variables.We can see from the output that the array code simply doesn't work. It appears you can't access parts of the array from within itself. At the moment, it looks like I'm stuck avoiding arrays for this. If anyone out there knows a way around this, please let me know!
Consider the following snippet of code:
[code lang="php"]<?php
$a = array(
0 => "test {$a[1]}",
1 => "blah {$a[2]}",
2 => "argh"
);
echo $a[0]."\n"; // outputs: test
$s2 = "argh";
$s1 = "blah {$s2}";
$s0 = "test {$s1}";
echo $s0."\n"; // outputs: test blah argh
?>[/code]
At first, we attempt to use an array to handle the string recursion, then we use string variables.We can see from the output that the array code simply doesn't work. It appears you can't access parts of the array from within itself. At the moment, it looks like I'm stuck avoiding arrays for this. If anyone out there knows a way around this, please let me know!
Sunday, July 12, 2009
Google Apps aren't ready for Chrome OS
Google recently announced their Google Chrome OS project that will see release some time in 2010. It certainly looks to be a promising idea, though I can't help but think that the project's success lies heavily on a very small window of implementation decisions. The slightest deviation from the "perfect" solution could make Chrome OS more a gimmick than anything. As the OS aims to be the most web-driven ever designed, it's clear Google will use their slew of online applications to support the OS. In their current state however, the use of Google Apps as a true replacement for desktop applications is laughable. The biggest flaw in the implementation of all Google's online applications is the necessity to sync the app before going offline. In their current state, if I were to go offline and maintain my current online experience, I would have to sync my Google reader feeds, my Gmail inbox, and my Google Docs files before going offline. How is Chrome OS going to be useful when it's applications are worthless during unexpected spurts of offline use?
You might assume that Google's offline technology, Google Gears, just isn't capable of seamless online/offline transitions, but a quick look through their developer tutorial proves otherwise.
Update (2009-12-23): It looks like Gmail has the most robust offline support of all the Google web apps now. Reader still forces you to manually sync, you can't create new documents in Docs if you're offline, and Calendar still only lets you look at your events without changing anything. This is promising though, as Gmail is now a suitable replacement for a desktop e-mail client. One step at a time.
You might assume that Google's offline technology, Google Gears, just isn't capable of seamless online/offline transitions, but a quick look through their developer tutorial proves otherwise.
In a "background sync", the application continuously synchronizes the data between the local data store and the server. This can be implemented by pinging the server every once in a while or better yet, letting the server push or stream data to the client (this is called Comet in the Ajax lingo).This is exactly the type of synchronization feature that Google's apps need to make Chrome OS possible. Without it, Chrome OS will never succeed. Gears has been capable of background synchronization, yet each time Google adds offline capability to one of their apps they force you to manually sync your data before going offline. I've never understood this strategy and now it appears they must revise their offline strategy for the Chrome OS project. I'd imagine Google to think more ahead than this, but let's hope they catch on and we start seeing background sync rolled into their web apps as the Chrome OS launch nears.
The benefits of background synching are:
The downside is that the sync engine might consume resources or slow down the online experience with its background processing (if it's not using the WorkerPool). Using WorkerPool the cost of synching is minimized and no longer affects the user's experience.
- Data is ready at all times, whenever the user chooses to go offline, or is accidentally disconnected.
- The performance is enhanced when using a slow Internet connection.
Update (2009-12-23): It looks like Gmail has the most robust offline support of all the Google web apps now. Reader still forces you to manually sync, you can't create new documents in Docs if you're offline, and Calendar still only lets you look at your events without changing anything. This is promising though, as Gmail is now a suitable replacement for a desktop e-mail client. One step at a time.
Subscribe to:
Posts (Atom)