How to add TINYINT MySQL type to Doctrine in Symfony 2.8
Hello! A few days ago I needed to define an entity with a TINYINT type column to hold a rating value between 1 and 5. I was doing this in a Symfony 2.8 project and to my surprise, Doctrine was unable to handle this type of data out of the box (it uses it for booleans but it has no TINYINT standalone type).
So I searched the internet for a solution and I found some but nothing complete.
The solution (TINYINT vs Doctrine):
First you need to define the new Doctrine type class like this:
<?php namespace AppBundle\Types; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Platforms\AbstractPlatform; class TinyintType extends Type { const TINYINT = 'tinyint'; public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { return 'TINYINT'; } public function convertToPHPValue($value, AbstractPlatform $platform) { return $value; } public function convertToDatabaseValue($value, AbstractPlatform $platform) { return $value; } public function getName() { return self::TINYINT; } }
Then, to register it, I used the boot function of the AppBundle:
<?php namespace AppBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; use Doctrine\DBAL\Types\Type; class AppBundle extends Bundle { public function boot() { parent::boot(); $em = $this->container->get('doctrine.orm.default_entity_manager'); // types registration if(!Type::hasType('tinyint')) { try { Type::addType('tinyint', 'AppBundle\Types\TinyintType'); $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('TINYINT', 'tinyint'); } catch (\Exception $e) { } } } }
I had to use the try / catch block to avoid an error when running symfony console commands having no database defined:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1049] Unknown database 'rating'' in vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
Now you can use the new type in your entities:
/** * @ORM\Column(name="rating", type="tinyint") * @Assert\NotBlank() * @Assert\GreaterThanOrEqual( * value = 1 * ) * @Assert\LessThanOrEqual( * value = 5 * ) */ protected $rating;
Learn how to setup Docker machine for Symfony development.
Hope this helps someone :)
Have a great day!
Start writing tests in Ruby: useful gems
Being a QA engineer is a continuous struggle in finding the right resources in order to get the job done easier and more efficiently. If you are planning to write automated tests in RSpec (Ruby's testing framework), then you should take a look over these gems. Please notice that I am most of the time automating backend tests only, so the libraries I am using are for this purpose mainly.
1. RestClient
gem install rest-client
If you want to make API calls on RESTful endpoints this should definitely be your choice. This library is easy to use and the response includes code, cookies, headers and body.
Let me show you how to make some calls (GET, PUT, POST, DELETE):
response = RestClient.get(url, header){|response, request, result | response} response = RestClient.put(url, payload, header){|response, request, result | response} response = RestClient.post(url, payload, header){|response, request, result | response} response = RestClient.delete(url, header){|response, request, result | response}
Now you simply use this response for your purposes (response.code, response.body, etc.).
2. JSON
gem install json
If I told you about RestClient, then the next one should be json. RESTful services will return JSON format in body most of the times so you should parse that response to be easier to work with.
response = RestClient.post(url, payload, header){|response, request, result | response} parsed_response = JSON.parse(response.body) expect(parsed_response['errors'][0]['message']).to eq "Not Found"
See how simple this is? You only JSON.parse that response and that's all!
Since we are talking about JSON, let me show you how to build one:
payload_hash = { :key1 => :value1, :key2 => :value2 } payload_json = payload_hash.to_json
3. Nokogiri
JSON and XML are the most used formats in web development. So you probably guessed that now I will show you some tricks on how to use XML in your awesome tests.
gem install nokogiri
When I have installed this gem on my ubuntu (v14.04) virtual machine, I have had the following error:
ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension. /usr/bin/ruby1.9.1 extconf.rb /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError) from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from extconf.rb:4:in `'
But this was quickly fixed after installing ruby-dev and ruby1.9.1-dev:
sudo apt-get install ruby-dev
sudo apt-get install ruby1.9.1-dev
Now let's say you have the following XML:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Body> <Login> <username>username</name> <password>secret_password</password> </Login> </Body> </Envelope>
If you want to access the values for username and password, simply do this:
your_file = Nokogiri::XML(your_XML_file) your_file.remove_namespaces! puts your_file.css('username').text puts your_file.css('password').text
Also, you can use xpath instead of css.
Let me show you how to build the previous XML file using Nokogiri:
builder = Nokogiri::XML::Builder.new do |xml| xml.Envelope { xml.Body { xml.Login { xml.username "username" xml.password "secret_password" } } } end puts builder.to_xml
4. Sinatra
This gem is used to mock endpoints. See more about it here.
5. Dotenv
gem install dotenv
It is recommended to keep environment variables and stuff like usernames, passwords and URLs in a .env file. In order to load those variables in your tests, you must use this gem.
Dotenv.load login_url = ENV['VAR_NAME'] signup_url = ENV['VAR_NAME']
First you load the .env file, then use those variables in your tests.
6. Mysql
gem install mysql
The name itself says what this is used for. See below how to open a connection to a MySql database and do a simple operation:
con = Mysql.new(db_host, db_user, db_pass, db_schema, db_port) rs = con.query("UPDATE table_references SET col_name1= ... WHERE where_condition") con.close
I will update this post when I will use some new awesome Ruby library. What gems are you using?
How to Store Latitude and Longitude in MySQL
As we know, a latitude value can be between -90 and +90 degrees, whereas a longitude value can be between -180 and +180 degrees. To get accuracy to within a meter, only six decimal places are needed, which is sufficient for most cases. But you can go even further by using eight places which will give you more than centimeter-level accuracy.Read more
The Chain Design Pattern
Today's article is meant to show you how to go from plain-text tasks to a collection of classes that are meant to work together.Read more