Managing Tags in ManyToMany association with Symfony

Symfony: v3.1.3
Doctrine: v2.5.4

One of the things I found challenging when first working with Symfony was form collections. The Symfony documentation on how to embed a collection of forms as well as how to work with Doctrine associations / relations is a great place to get started. However, checking out other use cases people shared online really helped as well, so I’m going to share another example here which I hope might help others.

For the following example, I will be setting up a Product entity, and a Tag entity. One Product can have multiple Tags, and one Tag can have multiple Products (Many To Many). Here’s how I want it to work:

  • New Tags can be added when adding/editing a Product.
  • No duplicate Tags should be added to the tag table.
  • If this Product is deleted later, any Tag which was added with it should remain in the database.
  • Use the jQuery Tag-it plugin to add the tags to our form for submission.

Ok, let’s get started with our Product entity:

Then, the Tag entity:

A quick note about this line in my Product.php file:

@ORM\ManyToMany(targetEntity="Tag", inversedBy="products", cascade={"persist"})

I decided to pick Product as the owning side in this association, as Tags will only be created when a Product is added or edited. As you can see in the Doctrine documentation you can choose the owning side in a ManyToMany association. You do so by setting “inversedBy” to it, while setting “mappedBy” to the inverse side.

targetEntity  is the class which is your target: Tag

inversedBy  is the property used in the Tag entity for Products (line 27 in Tag.php): products

cascade={"persist"}  will automatically persist any Tags associated to this Product.

As far as the Tag.php file goes, you set  Product  as its targetEntity , and since Tag will be the inverse side in this association, you set  mappedBy  to the property in the Product entity used for Tags (line 27 in Product.php): tags

With that setup, you can run this to generate your getters and setters:

$ php bin/console doctrine:generate:entities AppBundle

The final result of both entities should look like this:

Product.php

Tag.php

Note that both the $tags  and $products  properties need to be set as an ArrayCollection in the constructor. This is explained in the Symfony documentation linked to in the beginning of this article on form collections.

Last but not least, we need to update our database by running:

$ php bin/console doctrine:schema:update –force

If all goes well, then you should see a friendly little message like this afterwards:

Doctrine schema update

You should now have 3 tables added to your database:

phpMyAdmin screenshot

We will setup our forms in the next article: Setting up Form Collection for Tags in Symfony

Can’t customize FOSUserBundle error messages (How to customize Symfony validation messages)

Symfony: v2.7.15

Using the FOSUserBundle, I needed to override the invalid current password error message given when a user edits their profile.

To customize FOSUserBundle’s English messages, you can override these files:

vendor\friendsofsymfony\user-bundle\Resources\translations\FOSUserBundle.en.yml
vendor\friendsofsymfony\user-bundle\Resources\translations\validators.en.yml

…by putting new ones here, and editing them as you see fit:

app\Resources\FOSUserBundle\translations\FOSUserBundle.en.yml
app\Resources\FOSUserBundle\translations\validators.en.yml

Checking in FOSUserBundle’s validators.en.yml, I saw this:

It looked like I might be able to edit the invalid current password message here, but when using the Edit Profile form (edit_content.html.twig), I got a different error message:

“This value should be the user’s current password.”

I found that error existing in this Symfony file:

vendor\symfony\symfony\src\Symfony\Component\Validator\Resources\translations\validators.en.xlf

So, how to override it? Credit goes to Geert Van Damme for providing the information I needed.

Create a validators.en.yml file in your app folder like so:

app\Resources\translations\validators.en.yml

Then, override the message by adding its exact text as the key in your YAML file. For example, to override the password error message mentioned above, you could add this to your validators.en.yml file:

cURL error 60: SSL certificate problem: when creating new Symfony project – WAMP

PHP: v5.5.12
WampServer: v2.5
Symfony: v3.1.3

I got an error recently when attempting to create a new Symfony project using the following command:

php symfony new project

The error was:

[GuzzleHttp\Exception\RequestException]
cURL error 60: SSL certificate problem: unable to get local issuer certificate

The fix is to download a cacert.pem file which you can find here: https://gist.github.com/VersatilityWerks/5719158/download

Or you can copy and create a file from the curl site here: https://curl.haxx.se/ca/cacert.pem

For WAMP, you can move the cacert.pem file to the ssl folder here: C:\wamp\bin\php\php5.5.12\extras\ssl (Your exact folder path may differ)

Then, you need to open your php.ini file and add the path to your cacert.pem file. NOTE: If you’re using WAMP, do NOT open the php.ini file in your \apache folder from the wampserver icon in your system tray like this:

WampServer php.ini

Instead, you should open the php.ini in your \php folder. For example:

C:\wamp\bin\php\php5.5.12\php.ini

Open that file, and search for the line with: curl.cainfo
You may see something like this:

;curl.cainfo =

Uncomment that line, and give it the path to your cacert.pem file. For example:

curl.cainfo = “c:/wamp/bin/php/php5.5.12/extras/ssl/cacert.pem”

Save your php.ini and your Symfony projects should be created now with no problem.

Sources:

  1. cURL error 60: ssl certification issue when attempting to use symfony
  2. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  3. Symfony framework installation error WIN | curl error 60: ssl certificate problem resolved – YouTube

Fix: PHPUnit not working with Git Bash in Windows

If you installed PHPUnit to a Windows machine, but are seeing something like this when running phpunit in Git Bash:

$ phpunit ––version
bash: /c/bin/phpunit: No such file or directory

Then make a duplicate copy of your phpunit.phar file:

C:\bin\phpunit.phar

And rename the copy to:

C:\bin\phpunit

PHPUnit should work fine in Git Bash after that, as well as in Windows cmd or PowerShell.

Note: If you don’t care about running it in Windows cmd or PowerShell, then just rename the original phpunit.phar file to phpunit rather than making a copy.

Source: http://superuser.com/a/944992