Marco's Note

Note down useful and important things

Symfony2 – Add Legends in a FormType Class

In order to add extra variable in a Symfony 2 form type(in this example, the legend) in FormView, you have to implement this in your form type class.

1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\FormInterface;

//...

public function buildView(FormView $view, FormInterface $form, array $options)
    {
        //this add the label, aka legend of the form
        $view->vars = array_replace($view->vars, array(
            'label'        => 'Asset',
        ));
    }

jQuery to Get Node Text

In this example, we want to get only “Campus 1″ as a string returned.

1
2
3
4
5
6
7
8
9
10
11
12
<ul class="nestable-view">
    <li id="node-that-we-want">
        Campus 1
        <ul>
            <li>Block A
                <ul>
                    <li>Room A1</li>
                    <li>Room A2</li>
                    <li>Room A3</li>
                </ul>
            //...
</ul>

In a normal jQuery function, if you want to get content of a node. You would come up with something like this

1
$('#node-that-we-want').text();

However, this is a bad idea. All of the text including the node and spacing will be preserved in the result. It is not what we want. Instead of using .text() function, using a method like this would be helpful

1
$.trim(ele.firstChild.nodeValue);

(“ele” should be a javascript element, not jQuery object.)

This will trim all the whitespace, and return only the value we wanted.

Sample deploy.rb for Capifony to Deploy Symfony 2 Application on MAMP

As I’m working on my JamTube web (it is online now!), I have to seek a way to easily update my code to production environment.

I have chosen Capifony which as far as I know the most complete solution so far.

However, getting it setup with MAMP on a Lion server is not easy. After many tries and putting things together, finally I have came up the deploy file that should work all the way in.

I hope this could save you or others lots of time on deployment.

Speed Up PHP Composer Cloning With Different Protocols

I have recently discovered a ways to speed up my composer cloning process

1
2
3
4
5
6
// composer.json
"config": {
    "bin-dir": "bin",
    "process-timeout": "10000",
    "github-protocols": ["https"]
},

This would change protocol use to clone from git (by default) to https which is much faster.

Removing an Embedded Document in Doctrine MongoDB ODM

Since Doctrine didn’t provide any convenience methods which can remove an embedded document straight away. Here is the method that I have used.

1
2
3
4
5
6
7
8
9
10
11
public function deletePropertiesAction($id, $pid)
{
    $dm = $this->getManager();
    $asset = $dm->getRepository('MDBAssetBundle:Asset')->findOneById($id);
    foreach($asset->getProperties() as $property ){
        if($property->getId() == $pid) { //scan for matching Id of the element
            $asset->getProperties()->removeElement($property); // remove the element from PersistenceCollection
            $dm->flush($asset); // update the database
        }
    }
}