symfony adding file image constraints to form

let’s say you want to constraint what people are uploading and you don’t want to use assert statements in the entity. you can do it in the formbuilder

use Symfony\Component\Validator\Constraints\File;
$form = $this->createFormBuilder($entity)
            ->add('logo', FileType::class,
                    'label' => false,
                    'attr' => array(
                        'accept' => "image/jpeg, image/png"
                    'constraints' => [
                        new File([
                            'maxSize' => '2M',
                            'mimeTypes' => [
                            'mimeTypesMessage' => 'Please upload a JPG or PNG',

doctrine owning side and inverse side

owning and inverse side can be confusing. Then there is uni and bi directional relationships.

For example:

     * @ORM\ManyToMany(targetEntity="\Acme\UserBundle\Entity\User", inversedBy="users")
     * @ORM\JoinTable(
     *     name="gallery_has_users",
     *     joinColumns={@ORM\JoinColumn(name="gallery_id", referencedColumnName="id", nullable=false)},
     *     inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)}
     * )
    private $users;

These are some key rules:

* The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute contains the name of the association-field on the inverse-side.

* The inverse side has to use the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. The mappedBy attribute contains the name of the association-field on the owning side.

* ManyToOne is always the owning side of a bidirectional association.

* OneToMany is always the inverse side of a bidirectional association.

* The owning side of a OneToOne association is the entity with the table containing the foreign key.

* You can pick the owning side of a many-to-many association yourself.

How do you decide on the owning side?

This sentence helps:

Entity A(Owning) has Entity B(inverse).
If I delete Entity A, I want to delete Entity B.

Doctrine will only check the owning side of an association for changes.

refer to doctrine site for more info.

Form by_reference = true

if you are extending an entity, most of the time, you can ignore the by_reference field option. This is because it is set to true by default and its usually what you want.

for example

$formMapper->add('owner', 'entity', array(
                'class' => 'YourBundle:User',
                'multiple' => false,
                'required' => true,
                'label' => 'Gallery Owner'

This form field uses the variable “owner” as defined in the user class. it will call setOwner in the user class when the form is submitted before saving into the db.

Setting by_reference to false forces the framework to call the setter on the parent object instead. Since you are extending the parent entity, most of the time, you don’t want to touch the parent entity.

Software developer survival skills

To have a career as a developer, you need to have some important basic skill sets. All (new) technologies are built on top or around some commonly used technologies. People always say, “WOW, this technology is really hot now, you should learn it!”. Yes, but to even know the basics, it will take years to learn the pre-requisites.

Take a modern framework – Symfony for example, to know the framework well, you will need to know HTTP protocols, MVC, Object oriented methodologies, HTML, CSS, Javascript, Forms, Database Relationships, PHP, Version control, Basic Linux…etc.

Technologies evolved so fast that most University curriculum could not catch up. Most of the time. people spend years learning something that you only use 1% in your real job. That is why practical training is so important.

If you are looking to become a developer, learn these skills and you can branch off to anywhere:

1. Git and Git flow
3. Javascript
4. MVC and Object Oriented thinking
5. Basic system administration (bash scripting)

The last most important skill is personality. Be a human and don’t be a robot.

You will need these skills irregardless whether you want to be a front or backend developer majoring in any framework like rails, angularjs, nodejs, laravel, django, symfony…etc.

Why Symfony?

Choosing the best framework for rapid development has been a topic that has been debated to death. Today, there is no longer “the best framework” because all modern day framework is scalable and robust, they learn from each other. They are all adapting very fast to new techniques of doing things.

At the time of writing, nodejs and rails continue to innovate with php chasing fast behind. They have all very similar development methodology, so knowing one framework well means you can jump between other modern day frameworks easily.

For php, its Laravel and Symfony. if you want to invest your time, I recommend symfony because it is more stable when compared to the rest. Symfony components have been used by many projects including Drupal and Laravel.

as for Django and Rails, I am leaning more towards rails.

Symfony 2 is a mature framework. A mature framework means information and libraries don’t get outdated quickly. Some framework moves so fast that the 3rd party libraries couldn’t catch up. Rails early days suffered from this. Nothing worked out of the box because googled information was often outdated. Choose a stable framework so that you don’t spend lots of time setting up the right environment and troubleshooting the libraries for example. You should be spending more productive time learning the software.

Symfony2: Installation

The official installation doc is

As a best practice, don’t install symfony in MAC or Windows, install in Linux and use Linux for development because you will be using Linux for hosting.

For consistency in development, develop in virtual machine. The spec of the virtual machine should be the same as the production server. The best way to do it is to use vagrant.

In this example, we will create a ficticious project called SongBird. First of all, we need to create a portable and consistent development environment so that we speak the same language. This is also important if you decide to outsource the development in the future. Next we need a tool like puppet to configure automate software configuration.

To get started, let’s get this symfony repo –¬†

git clone
vagrant up

This will take a long time… most of the alerts you see in the screen is harmless. this is a once off setup. Enter root password when prompted.

According to the doc, you can choose a number of ways to install symfony, most people chose the composer method. Don’t use composer or manually download, install the symfony command line instead.

vagrant ssh
# you are now in the virtual machine
sudo curl -LsS -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

To create a new project call “songbird” using the long term support version,

cd /vagrant
symfony new songbird lts
cd songbird
sed "s/''/'',''/g" -i web/app_dev.php
# check that all requirements met
php app/check.php
# you should see that the config has passed

In your parent host, we need to add get to be pointing to the vm. add this to your host file.

In your parent, Test your site by going to

If you see the word “Homepage”, everything is good.

Now let us commit everything before we proceed.

git init .
git add .
git commit -m"init commit"