Doctrine2: CHAR with annotations

Posted November 7th, 2012 in Razno by Metod

Ever wondered if there is a better way of defining a CHAR (mysql) with doctrine2 annotations?

From doctrine2 docs:


Not a very nice approach imo. I dug around the code and found a somewhat neater solution:


Now you don't need to manually type in column definitions. Enjoy!

Debugging PHP: Part II – References

Posted October 28th, 2012 in Razno by Metod

If you read around the internets, you will see alot of references are bad stuff. Well, they are if you deal with them in the wrong way. Let’s take a look at an example:

 $cultures) {
    foreach ($cultures as $culture => $regions) {
        // ...
    }
}

// ...

Just a simple code to replace any missing english translations with the ascii ones. For which we decided to use a reference. Which is all good, except … !

Can you spot the problem?

In the second nested foreach, in the second run of that foreach we get an error that the supplied value is not an array. But we know it is an array! So what is happening here?

Well, that reference we used has bitten us. Remember, how the values assigned in foreach loops do not get unset when we leave that foreach scope? The problem is that the &$regions did not get unset automatically and we kept that reference all the way down to that problematic foreach. In the first run of the second foreach we then effectively appoint the value of $cultures to the $all_regions variable, replacing it’s original array values. Thus we get the error.

What was the correct code?

Just unset the reference.


And the problem goes away. These kinds of bugs are hard to spot. Especially since there is quite some code between the two. So when using references keep in mind: Always unset your references once you're done with them!

Debugging PHP: Part I – Type casting

Posted October 27th, 2012 in Razno by Metod

Here and there I run into some interesting bugs which I think would be nice to share. So here goes, bug #1 – Type casting.

setSync((int)$type === 'sometype');

// ...

if ($entity->getSync()) {
    // this should be run
}

// ...

Can you spot the problem? That if never gets evaluated. Why?

The problem is here:

$entity->setSync((int)$type === 'sometype');

In this case, the (int) cast only casts the $type variable, not the whole expression. So (int)$type === 0 which definitely is not equal to ‘sometype’ and also since we are using strict checking, type int is not equal to type string. The result is always boolean false.

The correct line should be:

$entity->setSync((int)($type === 'sometype'));

So watch out with casting and parenthesis.

Symfony2: Bundle does not contain any mapped entities

Posted September 14th, 2012 in Symfony2 by Metod

There is one more tip to solving this mistery. Make sure you have a namespace statement in the entity line.


This will cause the error. But if you add the namespace (duh moment) it will work again.


Post messages to a Teamspeak3 instance

Posted September 9th, 2012 in Snippets by Metod

This is a simple script for posting messages to a Teamspeak3 instance.

It uses the TeamSpeak3 Framework.

Symfony2: Reload user roles

Posted June 22nd, 2012 in Razno, Symfony2, Tehnikalije by Metod

If you are not using FOSUserBundle for managing your user system, then you might have stumbled onto a problem of reloading user roles when you change them. Here is a possible solution.

Your UserInterface::equals() method might look something like this:

Now all you need to do is change it so it also checks the user roles:

And it should work!

Now whenever you change any user’s roles it will be effective immediately after the next page refresh instead of logout / login.

Quick tip: Joomla white page and rsync

Posted April 15th, 2012 in Razno, Tehnikalije by Metod

I recently worked on a joomla page. And since I love rsync, I made myself a build/sync script to update the page on the test server. The command was similar to the following:

$ rsync --exclude-from=build/rsync.exclude ...

The contents of rsyn.exclude contained:

cache/*
logs/*
tmp/*
...

But when I first ran the script, all I got was a white blank page. No apache errors, no PHP errors. Strange. Then it hit me. Joomla has some cache classes within cache/ directories. So removing the cache/* from rsync.exclude and running the script again made it work.

Of course then there is another matter of clearing the cache, but it is beyond the scope of this quick tip.

HTML5: contenteditable full example

Posted April 11th, 2012 in Zanimivosti by Metod

This post was inspired by a nice article on html5doctor: http://html5doctor.com/the-contenteditable-attribute/

The contenteditable attribute allows us to edit content of the element on the fly. However there are a few catches in the actual implementation that are not mentioned above.

Let us just check my implementation. I’m using jQuery and jQuery UI for the javascript part and PHP for the backend.

Of course the example was simplified to not clutter it too much.

I also recommend reading the html5doctor’s excellent article on the matter.

Have fun!

Symfony2: How to set the correct host for routing in console command

Posted March 31st, 2012 in Symfony2, Tehnikalije by Metod

Quick tip for setting the correct host in your custom symfony2 command.

$this->getContainer()->get('router')->getContext()->setHost('www.example.com');

This will set your host correctly for routing and it will no longer default to ‘localhost’.

A bit longer version:

Job found still running after platform shutdown eclipse

Posted March 15th, 2012 in Razno by Metod

If your eclipse crashed and refuses to start, saying something along the lines of: Job found still running after platform shutdown. Jobs should be canceled by the plugin that scheduled them during shutdown, then try the following.

$ rm -rf YOUR_PROJECT_DIR/.metadata/.plugins/org.eclipse.core.resources

Start eclipse. However, all your projects will be gone.

You can import them via File -> Import -> Existing Projects into Workspace; then select the directory where you had your projects.

EDIT: I just found a better way. (source)

$ rm YOUR_PROJECT_DIR/.metadata/.plugins/org.eclipse.core.resources/.snap

This way you do not need to reimport your projects.

ANOTHER WAY:

As IGx89 suggested in the comments, you can also try the following:

$ rm YOUR_PROJECT_DIR/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi

http://stackoverflow.com/questions/11310970/eclipse-juno-doesnt-start/12045547#12045547