Monday, December 10, 2012

How to found out extra fields in Symfony2 form?


If you encounter this error message while working with Symfony2 forms, follow my advice.
This form should not contain extra fields.
This way to find out the fields that are considered "extra" is very simple. I use the code quite frequently. Here it is:
$data = $request->request->all();

print("REQUEST DATA<br/>");
foreach ($data as $k => $d) {
    print("$k: <pre>"); print_r($d); print("</pre>");
}

$children = $form->all();

print("<br/>FORM CHILDREN<br/>");
foreach ($children as $ch) {
    print($ch->getName() . "<br/>");
}

$data = array_diff_key($data, $children);
//$data contains now extra fields

print("<br/>DIFF DATA<br/>");
foreach ($data as $k => $d) {
    print("$k: <pre>"); print_r($d); print("</pre>");
}
...
$form->bind($data);
Have a nice day!

Saturday, November 24, 2012

How to install Oracle Java 7 in Ubuntu 12.04

First of all remove JDK packages if they were installed:
sudo apt-get purge openjdk* 
After that:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
Thanks for this article. It helped a lot.

Saturday, November 3, 2012

PHP can be inconsistent

PHP can be inconsistent. Good PHP-programmers know its wayward temper. It is the payment for the seeming simplicity at the beginning. A naive beginner can be captivated by the absence of restraints. But experienced programmers know that the absolute liberty can cause a chaos.

In honor of Halloween some blogs published interesting scary examples of the PHP behavior. I liked this one and want to share:

Spooky Scary PHP

Let's read and remember: there is no perfection in our world and so let's use our sense of humor not to conclude bad things about the language. PHP has also the good features that make it one of the most used programming languages.

Friday, September 14, 2012

How to find files in a directory hierarchy that contain a string in Ubuntu

find . | grep 'string'
Search for files that contain string "string".

Wednesday, September 12, 2012

How to get the url of the current repository

git config --get remote.origin.url

Monday, August 13, 2012

How to get a form name from the twig-template in Symfony2?


It's pretty easy.
{{ form.vars.name }}

Friday, August 3, 2012

Relational Algebra Operations

How important is it to understand the relational algebra?

In the first place, the relational algebra is a part of computer science and so it should be useful thing ;)

Secondly, although the relational algebra is a formal model, it's the basis for database query languages. Widely used SQL is based on it. The study of the relational algebra is supposed to make the comprehension of SQL easier.

Foundations

First of all the relational algebra is a procedural query language. The word "procedural" means that the language consists of operations. The operations are performed on relations (tables or parts of them).

Relational Algebra Operations:

Wednesday, August 1, 2012

Using CASE in UPDATE statements

I believe you know the general way to write update statements. But let's revise and take a look at Figure 1.

If we want to update salary of the persons with salary over $50000, the query will look like this:

UPDATE Persons
SET salary = salary*2
WHERE salary > 50000;

We have just doubled salary for these persons!

Let's see a more interesting example. Suppose we want to increase salary for persons with salary over $60000 by 3 percent, whereas all others receive a 5% raise. We want to do that by executing only one(!) query.

Try to write the query by yourself before you look at the solution.

We can do it using case statement:

Right use of GROUP BY in SQL-queries

This article is about the right use of grouping in SQL-queries. I wrote the article, because this subject is very important. More than that it is not too much complicated and suits to the format of short useful articles, that my blog is for.

Job interviews like questions concerning grouping (using GROUP BY in SQL-queries). If you don't use SQL-queries every day or don't know them well, refresh your memory before your interview.

Let's take an example. Figure 1 shows a table Persons, which stores information about persons. The table has four column headers: ID, name, dept_name, salary.

First of all we use GROUP BY with aggregation.

Aggregate functions are functions that take a collection of values as input and return a single value. SQL offers five built-in aggregate functions:
  • Average: avg
  • Minimum: min
  • Maximum: max
  • Total: sum
  • Count: count

Consider the query "Find the average salary of persons." Try to write the query by yourself before you look at the solution. We write query as follows:

Tuesday, July 31, 2012

Design Patterns for beginners

I started studying Design Patterns with "Design Patterns: Elements of Reusable Object-Oriented Software" by famous Gang of four: Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. It was my mistake.

This book is great, but it is not for beginners. I spent a lot of hours trying to understand patterns. But it was not useful for me, because I was a beginner. I stopped reading and was disappointed with myself.

If you are a really tough programmer you might know this book very well and you don't need my advice to read it. But if you are a beginner I have an advice for you. Please, don't begin to study design patterns with the book of Gang of four.

"Head First Design Patterns" by Elisabeth Freeman, Eric Freeman, Bert Bates, Kathy Sierra, Elisabeth Robson is written for beginners.

One day I found the book "Head First Design Patterns" by Elisabeth Freeman, Eric Freeman, Bert Bates, Kathy Sierra, Elisabeth Robson. Authors use JAVA to explain design patterns.

It was indispensable for me and I believe it will be very useful for every beginner.

In the beginning of the book there is an interesting chapter about brain and thinking. It contains tips how to learn more efficiently.

More than that this book is rather funny. Check it out.


What is the difference between CHAR and VARCHAR data types?

CHAR and VARCHAR are the basic built-in types that the SQL standart supports. This short article is about the difference between them.

Difference between CHAR and VARCHAR
CHAR(n)VARCHAR(n)
A fixed-length character string with user-specified length n. A variable-length character string with user-specified maximum length n.

VARCHAR is a short version of "character varying".

The CHAR data type stores fixed length strings. And we should be mindful of this.

For example, if we store a string "test" in the field of type CHAR(10), 6 spaces are appended to the string to make it 10 characters long. In contrast, if we were store the string in the field of type VARCHAR(10), no spaces would be added.

When comparing two values of type CHAR, if they are of different lengths extra spaces are automatically added to the shorter one to make them the same size.

When comparing a CHAR type with a VARCHAR type, extra spaces may not be added (it depends on the database system).

As result, if even the same value "test" is stored in the fields of different types (CHAR and VARCHAR), a comparison of these fields may return false.

Authors of the book "Database System Concepts" Abraham Silberschatz, Henry F. Korth, S. Sudarshan recommend to use the VARCHAR type instead of the CHAR type to avoid these problems.

I used this great book to write the article. The authors of the book are really great men. They did a lot of work writing 1349 pages about database system concepts. I really admire them for doing that.. and enjoy reading the book. Many things become clear.

I'm sure that I'll be better programmer after reading it.

Wednesday, February 29, 2012

How to enable MySQL query log

I have found it useful to enable the general query log on my local computer to explore mysql queries.
If you want to follow the advice, "Be aware that this log type is a performance killer".

Steps:

  1. Open the /etc/mysql/my.cnf file.
  2. Activate general_log_file and general_log options in the Logging and Replication section:
general_log_file        = /var/log/mysql/mysql.log
general_log             = 1
The logs are available in the /var/log/mysql/mysql.log file.

Tuesday, January 24, 2012

Method preg_match() for Cyrillic characters in PHP

Sometimes I encounter the problem with preg_match() method, because it doesn't treat Cyrillic characters the proper way. The problem also corresponds to other non-latin characters.
There is a solution for the search in the string that contains non-latin characters.
<?php 
  preg_match("/^[a-zA-Z\p{Cyrillic}]+$/u", "AбВгд");
?>
Do not forget about the Pattern Modifier - u
u (PCRE_UTF8)

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8.

This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.