Dumping Object/Array Stacks in Symfony

Submitted by Peter Majmesku on Mon, 07/10/2017 - 13:54

I prefer using XDebug for debugging PHP code. But sometimes there's a huge nesting in the project, which makes the application very slow. Then XDebug can become pain. So there's an alternative for inspecting the contents of objects:

\Doctrine\Common\Util\Debug::dump($user);

It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.

Debugging eMails in Symfony 2/3 and Drupal 7/8

Submitted by Peter Majmesku on Fri, 06/30/2017 - 22:33

The best tool for debugging eMails locally in my opinion is Mailcatcher. It catches all mails being sent via a specific port and shows them to you via an neat interface in your webbrowser. Firstly install Mailcatcher. It's programmed in Ruby and I could install it easily on Ubuntu and Mac. The instructions can be found at https://mailcatcher.me/.

Symfony 2/3

In the full stack edition it comes with symfony/swiftmailer-bundle. Make sure these settings are in your parameters.yml + parameters.yml.dist file.

mailer_transport: smtp
mailer_host: 127.0.0.1:1025
mailer_user: ~
mailer_password: ~

Afterwards you can send a test mail via the console:

bin/console swiftmailer:email:send

The mails will be shown to you like in the screenshot below.

The Mailcatcher Interface in Webbrowser

Drupal 7/8

Beside Drupal 8 install the "SMTP Authentication Support". You can find it's project webpage at https://www.drupal.org/project/smtp. Then visit the following page in your Drupal 8 instance: http://your-neat-webpage.dev/admin/config/system/smtp. Make sure the following settings are set in the config form (there's also a possibility to set them in the settings.php file).

SMTP server: 127.0.0.1
SMTP backup server: LEAVE BLANK
SMTP port: 1025
Use encrypted protocol: No

SMTP AUTHENTICATION: LEAVE USERNAME + PASSWORD BLANK

E-MAIL OPTIONS: ANY EMAIL AND FROM NAME

SEND TEST E-MAIL: ANY E-MAIL ADDRESS

Check "Enable debugging".

First part of settings form in Drupal 8
Second part of settings form in Drupal 8

Doctrine Query Builder: Filter Entity Types Based on Discriminator Column

Submitted by Peter Majmesku on Wed, 06/14/2017 - 09:36
<?php $queryBuilder $this->entityManager->createQueryBuilder()
            ->
select('mbt')
            ->
from('Bundle:MyBaseType''mbt');

        
$queryBuilder->where('mbt INSTANCE OF Bundle:SomeType'); 

MySQL: Write All Executed Queries Into a Log File

Submitted by Peter Majmesku on Tue, 05/23/2017 - 13:54
  1. Open the MySQL config file, which is located at /etc/mysql/my.cnf on Linux systems.
  2. Then write the following lines at the end of that file.

[mysqld]
general_log_file = /var/log/mysql/general_mysql.log
general_log = on

Restart your MySQL server (e.g. by executing "sudo service mysql restart" on Ubuntu Linux systems). Now you can follow all new queries as they are executed by this command:

tail -f /var/log/mysql/general_mysql.log

You can leave this program in command line by pressing CTRL+C.

For disabling, just set "general_log" to "off" or comment the out the previous lines in /etc/mysql/my.cnf by writing the # character at the beginng of the lines, save and restart the MySQL server again.

Update: Alternative setting by "general_log" variable

Recently the approach from above was not possible on my MariaDB server in Linux Mint 18.1. The log file was just not written. Even if I have created it. Then I could enable the general_log by variable. For this login into your MariaDB/MySQL server via command line. Then enable the general_log:

SET GLOBAL general_log = On;

Check all your variables which have something to do with "log" by this command:

show variables like '%log%';

The filename of the general_log is stored in that variable: "general_log_file". The default general log folder on Ubuntu/Debian based Linux systems (like Linux Mint) is: 

/var/lib/mysql

Tags

Show Free Disk Space on Linux

Submitted by Peter Majmesku on Tue, 05/23/2017 - 13:17

df

The "disk free" program shows the free space on a storage device via command line.

  • Show free space for the entire filesystem:
    •  df -h
  • Show free space for the local filesystem:
    • df -hl
  • Show more documentation regarding the "df" program:
    • man df

du

The "disk usage" program shows the used, not free, space on a storage device via command line.

  • Show the used space amount on the entire file system:
    • du -sh /
  • Show the used space of the folder named "media" by excluding ".bmp"-files:
    • du --exclude="*.bmp*" -sh media/ 
  • Show the used space of the current directory:
    • du -sh ./
  • Sort the folders in the current directory (recursion depth 1) by their disk usage:
    • du -h --max-depth 1 ./ | sort -h
  • Show more documentation regarding the "du" program:
    • man du
Tags

MySQL: Fixing Foreign Key Constraint Errors

Submitted by Peter Majmesku on Wed, 05/17/2017 - 19:12

Login to your MySQL server via command line. Then execute the following statement:

SHOW ENGINE INNODB STATUS;

You will get a error message like:

Foreign key constraint fails for table `my-table`.`JobExchange`:
,
  CONSTRAINT `FK_42760B1EE7A1254A` FOREIGN KEY (`contact_id`) REFERENCES `contact` (`id`) ON DELETE SET NULL
Trying to add to index IDX_42760B1EE7A1254A tuple:
DATA TUPLE: 2 fields;
 0: len 4; hex 80000001; asc     ;;
 1: len 4; hex 8000000d; asc     ;;

But the parent table `my-table`.`contact`
or its .ibd file does not currently exist!
 

This shows you, that MySQL cannot find the corresponding database table. This can be due to lowercase-uppercase mismatch of the table names. Check your MySQL server settings:

show variables like 'lower%';

This will show something like this:

+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
+------------------------+-------+
2 rows in set (0,01 sec)

Show detailed information about the primary-foreign key references

mysql> SELECT
-> TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
-> FROM
-> INFORMATION_SCHEMA.KEY_COLUMN_USAGE
-> WHERE
-> REFERENCED_TABLE_SCHEMA = 'my-table' AND
-> REFERENCED_TABLE_NAME = 'JobExchange';


+-----------------------------------+----------------+---------------------+-----------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+-----------------------------------+----------------+---------------------+-----------------------+------------------------+
| JobExchangePdf | jobexchange_id | FK_7A971E966193B1C5 | jobexchange | id |
| jobexchange_jobemploymentcategory | jobexchange_id | FK_F5E32C9C6193B1C5 | jobexchange | id |
+-----------------------------------+----------------+---------------------+-----------------------+------------------------+
2 rows in set (3,59 sec)

MySQL Workbench Vs. PhpStorm

MySQL Workbench shows the lower-case named table names correctly. PhpStorm does not. PhpStorm is guessing the right table name.

Possible fix

Drop the foreign key constraint and create a new one. F.e. with MySQL Workbench. You can drop the foreign key with the following command:

ALTER TABLE `my-table`.`JobExchange`
DROP FOREIGN KEY `FK_42760B1E6C7B4691`;
ALTER TABLE `my-table`.`JobExchange`
DROP INDEX `IDX_42760B1E6C7B4691` ;

The foreign key will be added like by this command:

ALTER TABLE JobExchange ADD CONSTRAINT FK_42760B1E6C7B4691 FOREIGN KEY (jobtype_id) REFERENCES JobExchangeCategory (id) ON DELETE SET NULL;
CREATE INDEX IDX_42760B1E6C7B4691 ON JobExchange (jobtype_id);
Tags

Ubuntu 16.04: Install NPM, Yarn and Gulp Frontend Tools

Submitted by Peter Majmesku on Sun, 05/14/2017 - 14:36
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install -y yarn nodejs
sudo npm install --global gulp-cli
Tags