Symfony2 and me – let’s be friends – Part 5

Today, I spent a lot of time figuring out, why my tables did not want to relate and adding a new entry resulted in errors with KEY CONSTRAINTS.

In the end, I believe it was because my tables where using differen MySQL DB enginges. THe existing tables where InnoDB, but the newly created fos_user table was using default MyISAM.
Manually switching the fos_user to InnoDB as well made it possible to have the relations being set up correctly.

Damn, took a while to figure it out. Ideally, the propel:reverse as well as the existing FOSBundle schema files would create/contain the corresponding entries so it would be more obvious… Pull Request anyone?

http://www.propelorm.org/reference/schema.html

Anyway… after looking at my exisitng DB design again and again, I decided… NO, I’ll change everything. There is not much use in trying to adopt to the old design.
I’d rather redesign the tables and then, for the migration of the old data, create a script to bridge and convert.

So, I played with my schema.xml again and again and then migrated and now my tables are mostly empty, to start from scratch… let’s fill them next week

Symfony2 and me – let’s be friends – Part 4

Been some time… but I want to move on now.
Last thing I wanted to understand and solve is how to create relations between my existing tables (with relations) and the User table/class create by FOS User Bundle.
The whole “problem” is: I’m creating my own bundle where my app should be in, and I already recreated a propel schema from the existing db in that bundle.
FOSUserBundle has it’s own schema (and model classes) in it’s schema directory, and now the question: How to relate them so I can benefit from the generated code of propel.

Is it as simple as setting the relations “like always” only in two different files? Let’s see. In my case, I have “users” that can have “events”, so this would be a relation.
To make it a little more complex, I was not using “simple” ids, but “ident_ids”, which are a long random string that was stored in a cookie to provide an easy (not so secure) type of identification.

Means: My FOS-User needs an additional column, being the “ident_id” – and this should be relating to the ident_id of the events table…
Let’s give it a try and add a column to the FOS User schema.xml

<column name="ident_id" type="varchat" size="255" required="true" />

And now add the relation in my schema.xml of my bundle

<foreign-key foreignTable="fos_user" name="user_FK_1">
<reference local="ident_id" foreign="ident_id"/>
</foreign-key>
<index name="user_FI_1">
<index-column name="ident_id"/>
</index>

I decided I might have to use “fox-User” as the foreign table, not the PhpName user. We’ll see.

Well, my first php app/console propel:model:build failed, because I wrote “varchat” as column type, but I managed to solve this. And then the build succeeded. Did I get what I wanted?

When thinking about how to test it, I managed to realize that I don’t even HAVE a fos_user table in my DB yet, so it’s time for some propel migrations.

php app/console propel:migration:gen creates a migration file for me that reveals it is going to prepare a fos_user table (including an idend_id column), a fos_group and a fos_user_group table. All right, migrate!

So, I need some data and I register on my website using the /register path created by the FOSUserBundle. Then, with phpmyadmin, I wanted to relate this new user (by adding an ident_id) to my related table – and mysql responds with “key constraint failed”…. hmpf, somethings wrong. What? We’ll see next time

Symfony2 and me – let’s be friends – Part 3

So far, I have a base project with Propel as my ORM, connected to an exisitng DB, re-engineered the schema and could start to do something with it, now.

My next goal is to create a user authentication. After a lot of thoughts on if/yes/no I decided to use the FOSUserBundle – hoping it will provide some features I could use and still would integrate if I want to use OPTIONAL ALTERNATIVE Facebook and Twitter login…

Step 7: Add FOSUserBundle

I’m following the steps in the docs and add the line to composer and do an update. It suggests Doctrine, BTW, but I ignore that, thank you. But, for Propel, it says I need to move my schema.xml to app/Resources/FOSUserBundle/config/propel/schema.xml. Well, my schema already contains a User (which is unused atm) that contains an id – so it might work?!
Now did the configurationa steps of the doc (including installation of the typehintable behaviour) and reran my propel-build… SUCCESSFULLY, so I really could see a page on /app_dev.php/login. Impressive!

Well, no, it did not… First: I missed that I should copy the schema.xml of the bundle to “app/……”. Second: Looks like I got mislead with my expectations about schema.xml, where to put it, and where Models will be generated… Can I have FOSBundle generate it’s User class into my Bundles folders? How can I create relations between the User Class of FOSBundle and my app?

Questions to be answered later, I hope

Symfony2 and me – let’s be friends – Part 2

I have a running, blank symfony2 project now, with Propel instead of Doctrine – but no DB or bundle yet

Step 5: Gimme a bundle

I know there is this bundle generator, so let’s see it:
app/console generate:bundle
I chose my namespace and hit some [Return]s – and I’m good to go – WOW, yes it’s there! Not much in there, yet, but I’ll check later.

Step 6: The database

Nice so far! Now I’d like to connect to my exisitng database and use Propel’s magic database reverse-engineering
Thanks to composer I don’t have to deal with namespace and prefix configurations, so I can go to the db- connection settings directly
So I add the basic propel config with parameters (http://www.propelorm.org/cookbook/symfony2/working-with-symfony2.html) to my app/config.yml; then I need to set the paramters in parameters.yml
No, how can I check if the connection works? Why not run the reverse-engineering process directly?
app/console propel:reverse “No generated files” Hmmm, my connection might not work – but there is no error :-(
SO, I manually check the permissions again (quickly dumping an adminer into the web dir) and YES, there is a problem with the permissions. SO I fix this, then run app/console propel:reverse again – and I have a file!!!

Step 7: Adjust the db schema

For the time being, the schema looks quite ok, so I’ll just move it from the generated-schemas folder to a more appropriate destination, being the Resources/config folder of my bundle.
To run propel:build now, I only need to add a namespace=”my\bundle\namespace” to the database tag of the xml, and it generates my models!

Enough preparational stuff, more coding next days!

Symfony2 and me – let’s be friends – Part 1

My journey, starting a rewrite of an exisitng project from scratch with Symfony2

Step 1: Get the framework:

composer.phar create-project symfony/framework-standard-edition path
After giving my VM some more RAM (512MB wasn’t enough) it succeeded.

Step 2: Get rid of Doctrine and grab Propel

So I edit the composer.json file and delete the two doctrine lines (if someone other lib needs doctrine/common for annotations, they will solve the dependency, right?
Then add "propel/propel-bundle": "1.1.*" to the file instead, as stated on http://www.propelorm.org/cookbook/symfony2/working-with-symfony2.html
Then I run “composer.phar update” and it updates my monolog, removes doctrine and installs propel, its bundle and phing.
After that, however, the symfony cache update fails – if course, I removed Doctrine which is configured somewhere in the the Kernel.

So I remove the Doctrine line from app/AppKernel.php and all doctrine stuff from app/config/config.yml – and HOORAY, my app/console is green again

Step 3: Does it work?

My webserver is already configured to check for an app.php in the web subfolder, so it works – but first I’ll add my IP to app_dev.php so I can see all the debug beauty of Symfony2
Checking the Debug toolbar I notice Propel is missing – of course, I need to add it to the kernel where I kicked out Doctrine
So I add new Propel\PropelBundle\PropelBundle(), to app/AppKernel.php where I commented Doctrine befor, and app/console knows some good old Propel commands now.

Step 4: Freeze this into git

This is a good start, so I want to get this status into git. I’m surprised to see that there IS already a lot of git-information in the directory, being the symfony2 history.
Quick fix, rm -rf .git and git init again, here I come. On the plus side, I already have a nice .gitignore :D
git add .; git commit -m “Fresh start”

How to trace down a memory leak in APE with valgrind

How to trace down a memory leak in APE with valgrind

Given:

You already have a working/compiled APE server running and you still have the sources.

Rebuild (don’t use build.sh) with the following steps

Enter your APE source code directory and then do

cd deps/js/src/
./configure --enable-debug --enable-valgrind
make
cd ../../../
make

Now copy your rebuilt libape and aped to it’s default locations (see http://www.xosofox.de/2010/11/get-your-ape-up-to-date/)

Now run your ape with valgrind as a wrapper (I prefer to cd to /etc/ape so I don’t have to specify the ape.conf)

valgrind --leak-check=full --log-file=/path/to/mem.log /usr/bin/aped

pkill aped

All leaks will be written to the log and can be analyzed

Note: valgrind is slowing down the application dramatically in terms of CPU

(Thanks to Anthony @paraboul, all the information is coming from a chat with him)

Symfony Day 2011 in Cologne

Symfony Day was amazing!!!

I love the community and it was great fun to see all the guys I already met during the last years and this year’s Symfony Live in Paris.

Again, a big THANK YOU to all organisers and of course all the people that make Symfony2 happen!

I just wanted to quickly paste my (sparse) notes that I took for myself – in case they help out someone else as well.

Basically, they are just some reminders for me of stuff that I wanted to check out.

Silex

  • Silex routes can use value() and assert() to be enhanced
  • Silex needs cookbook contributions
  • Silex can easily be functional tested (and require “” can have a return value!)

SPL

A shame, I never really looked at it before!? So take a closer look at http://php.net/manual/de/spl.exceptions.php and SPL in general

Links to follow/read

Pragmatic Programmer: http://pragprog.com/the-pragmatic-programmer/extracts/tips

Sonata

Check out www.Sonata-project.org - adminbundle

See, if it’s really that complicated as the talk made it seem

I did not note so much else, either because I already knew it, wanted to take a look at the presentations later, or player #sfdayjeopardy (thanks to Jeremy “Rick Astley” Mikola for that)

There’s one more thing

Actually, there were two things: Fabpot is a visionary! And his vision, of looking at Symfony2 not (only) as a framework but as a collection of several, individual and powerful components is great. MAYBE not for the developer looking for “a thing that does all the magik™” but for the more experienced one that can pick the best of a wide variety of components. Like some other PHP projects do now…

AND: Me, being a big fan of Propel, was happy to see all the momentum propulsion it got recently, including development support from inside sensio.

Hope to see you all again in June(?) in Paris and in November(?) in Berlin?

Fluchstaben

Quick post: I finished a bookmarklet, based on this years April Fool’s Day fun of www.karopapier.de:
FLUCHSTABEN

Klick this, it’s a JS call that will make some random letters of this website move.

Why I did it? Because it works!

You can save it as a bookmark(let) and use it on every webpage out there

1) Press the bookmark
2) Press multiple times for additional fun
3) Click one of the flying letters to make them go back where they belong.

Like it?

PS: “Fluchstaben” is a combination of the German words for “flying” and “letters” (“Flug” & “Buchstaben”)

A TURTED based real-time thing that I’m proud of…

I recently finished a nice little example of how to use real-time data for an exciting and equally relaxing view:

Blocker Champ Real Time


http://reloaded.karopapier.de/blocker

What’s that?
It shows a list of players and the number of games where it is their turn.
As soon as they make a move, they are moved to the new position with a nice animation.

Interestingly enough, the hardest part was the JQuery/UI animation stuff.
Since it already is based on the components that are soon to become the TURTED project the real time events were already there to be intercepted and used.

So, if you feel stressed, just sit back, relax and watch the squares move – the ZEN of Karopapier

JS Performance: Numbered Array vs Associative Array

In JS, you sometimtes have to keep track of lists. Usernames, sessions, available Pictures ,…
And often, you have to add and delete some of them.

So, how do you handle these lists? There are basically two options:

a) Good old numbered Array
b) Associative Array, key/value store

Examples?

var users=[];
users[0]=”Adam”;
users[1]=”Bob”;
users[2]=”Claire”;

users={};
users['Adam']=”Adam”;
users['Bob']=”Bob”;
users['Claire']=”Claire”;

Both have their advantages and disadvantages.
An array keeps the order, you have numbered keys. However, deleting or finding an element requires you to walk through the array.

So, the question to me was: How about performance? How big is the difference?

Long story short:
In case you do not care about the order and will only have unique keys, use an associative array!
To add a value:
assoArray[key]=value;
To get a value:
var myVal=assoArray[key];
To delete a key/value:
delete(assoArray[key]);

This is easy and performs much better. At least in my quickly hacked, unoptimised test you can find here: Simple test html
If you think I did something wrong, please enlighten me.