Thursday 24 June 2010

Symfony / sfGuardUser: MySQL errno: 150 Failing Query "ALTER TABLE sf_guard_user......."

I've been trying to link up my symfony project (using Doctrine) to sfGuardPlugin with profiles. However, when trying to rebuild the schema and insert sql, I encountered the following error:

SQLSTATE[HY000]: General error: 1005 Can't create table 'phoenix.#sql-740_d7' (errno: 150). Failing Query: "ALTER TABLE sf_guard_user_profile ADD CONSTRAINT sf_guard_user_profile_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id)". Failing Query: ALTER TABLE sf_guard_user_profile ADD CONSTRAINT sf_guard_user_profile_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id)


This isn't an incredibly useful error message. Turns out that it's to do with the way MySQL handles cascading constraints. To cut a long story short, there's more info in the links posted below, but basically you need to remember that "type: integer" does not mean it will be stored as INTEGER in MySQL. As a matter of fact, "type: integer" in a schema.yml file translates into a "BIGINT(20)" in MySQL - which means when you add a constraint to a normal INTEGER field, it fails.

To store an integer value in schema.yml, use "type: integer(4)". This will translate into a type of INTEGER in MySQL.

Explained in a comment I found:
Something you might be interested in, that took me quarter an hour. integer(11) in schema.yml does not result in int(11) in MySQL, but instead you have to write integer(4) in your schema.yml. integer integer int/serial integer(1) tinyint smallint/serial integer(2) smallint smallint/serial integer(3) mediumint int/serial integer(4) int int/serial integer(5) bigint bigint/bigserial However, in my case integer without a number resulted in BIGINT.


More info:
http://trac.symfony-project.org/wiki/sfGuardPluginExtraDocumentation
http://mirmodynamics.com/post/2009/04/02/SQLSTATE%5BHY000%5D%3A-General-error%3A-1005-Can-t-create-table-*-%28errno%3A-150%29 (especially see the last comment)
http://bytes.com/topic/mysql/answers/865699-cant-create-table-errno-150-foreign-key-constraints

Friday 4 June 2010

WeReward brings incentive advertising to iPhone

This is a Sponsored Post written by me on behalf of IZEA. All opinions are 100% mine.


Chops WeReward (WeReward.com) is a new iPhone app from Izea (the makers of PayPerPost and Sponzai, among various similar services).

Their latest offering is basically a way for iPhone users to make money by performing small, easy-to-do actions on their mobile device. The application integrates with Facebook, Twitter and Foursquare, and offers a variety of incentiv-ized postings.

According the the official website:
Earn WeReward points by completing tasks and checkins at your favorite businesses with your iPhone. Each point is worth a penny, earn 1,000 points and cashout to PayPal.
1000 points is equal to $10 at that conversion rate, which is pretty decent. Of course, it will take a long time to make much money when you're getting paid one penny for each tweet/checkin/share. But it all adds up.

The key to this application succeeding is the location-based relevance. Because offers are tailored to your location, many of the offers you see will actually be useful. In fact, your friends may find your posts helpful, because in many cases they are things that you would already be posting about anyway.

If you can get paid to do stuff that's useful to your friends, and takes little or no extra effort on your behalf, what's the catch? Nothing, really. If you're prepared to take a small amount of time and risk your reputation posting advertisements on your profile, there's nothing else that could really go wrong. Personally I would do it - provided the offers are relevant.

If you have an iPhone you can Download the App today - it only takes a minute. I'd love to hear your feedback (as I don't have an iPhone myself - I use Android). It's something I would definitely use myself, and helps to progress the whole micro-payments infrastructure. Interesting also that they use PayPal for payments. This is a sensible choice, but given MasterCard's entry into the market is something that could change relatively quickly.

Check out the video of how it works in action:

Visit my sponsor: WeReward for iPhone

Wednesday 2 June 2010

Javascript Card Guessing Game - Sample Code

I've been doing some coding in Javascript lately, and thought I would share some of the code I've been writing. Hopefully some of it will be useful for anyone looking for some sample code in Javascript, or examples of looping, keeping tallies, or writing basic functions.

Without further ado, here is the code. It's a simple playing card guessing game.
Javascript Card Guessing Game

Tuesday 1 June 2010

Android: Using Accelerometer to Calculate Total Force

I'm in the process of building an app that calculates how far you could throw something, based on the speed you swing your phone. This involves tracking your phone's movement as you swing it, and calculating the total maximum force involved. I couldn't find any examples on how to do this, so I've mocked up a bit of a tutorial below.

How do we get the accelerometer values?

private SensorManager mgr=null;
mgr=(SensorManager)ctxt.getSystemService(Context.SENSOR_SERVICE);
mgr.registerListener(listener,
mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);



private SensorEventListener listener=new SensorEventListener() {
        public void onSensorChanged(SensorEvent e) {
            if (e.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
                //Total acceleration will be sqrt(x^2+y^2+z^2)
                double netForce=e.values[0]*e.values[0];    //X axis               
                netForce+=e.values[1]*e.values[1];    //Y axis
                netForce+=(e.values[2])*(e.values[2]);    //Z axis (upwards)
               
                netForce = Math.sqrt(netForce) - SensorManager.GRAVITY_EARTH;    //Take the square root, minus gravity
               
                Log.d("ForceCalculator", "Net force:"+netForce+"");
            }
        }
       
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // unused
        }
    };


This will create and register an event listener for the accelerometer. This listener then calculates the net force.

This calculation is pretty self-explanatory. It involves three components - X axis, Y axis, and Z axis. To calculate the magnitude of these vector components, we simply take the square root of their combined squares:
netForce = sqrt( x^2 + y^2 + z^2 )
The only point to note is that we then subtract the acceleration due to gravity (approx. 9.8m/s^2). Otherwise we could have a resting acceleration of +9.8.

Full code will be coming later, when I've got some more time to play around with it. For now, this should show you how to get net force using the accelerometer. Still to come is code to calculate maximum force over a specified period of time, with threshold starting and finishing velocities.