Drop all mysql tables without permission to delete and create new database

In mysql, the simplest way to flush all tables is to delete the db and create a new one. However, one might not have permission to do that. So we need a script to list all tables and delete it.

#!/bin/bash

USER=xxx
PASS=xxx
HOST=localhost
DB=xxx

mysqldump -u$USER -p$PASS -h$HOST --no-data --add-drop-table $DB | grep ^DROP | mysql -v -u$USER -p$PASS -h$HOST $DB

Debugging GTM in IOS devices

Let’s face it, debugging gtm in devices other than desktop is hard. Debugging in IOS is harder because gtm debugger toolbar doesn’t work on it.

The tip to help debugging IOS is to be able to debug it from desktop

Let us use the ios webkit proxy debugger

https://github.com/google/ios-webkit-debug-proxy

Follow the installation instructions. However this debugger doesn’t let you inject js when debugging.

We will need another tool like https://github.com/artygus/webkit-webinspector – just follow the installation instructions. then go to

http://localhost:8080/Main.html?ws=localhost:9222/devtools/page/1 and you can start debugging as if you have the dev tools.

Resize image upload into proportional dimensions using GD library

sometimes you want to manipulate images uploaded. A good way to do it is to resize the image to the max width or height, fill the background with transparency and yet keeping the same original width to height ratio. lets say we want a 300 x 300 pixel image.

$canvas = imagecreatetruecolor(300, 300);
            imagesavealpha($canvas, true);
            $alpha_channel = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
            imagecolortransparent($canvas, $alpha_channel);
            imagefill($canvas, 0, 0, $alpha_channel);
            // process image width and height
            $old_x = imageSX($image);
            $old_y = imageSY($image);
            $thumb_x = 0;
            $thumb_y = 0;
            // say max width and height is 300px.
            if($old_x > $old_y)
            {
                $thumb_w =   300;
                $thumb_h =   $old_y*($thumb_w/$old_x);
                $thumb_y = abs(300 - $thumb_h) / 2;
            }
            if($old_x < $old_y)
            {
                $thumb_h =   300;
                $thumb_w =   $old_x*($thumb_h/$old_y);
                $thumb_x = abs(300 - $thumb_w) / 2;
            }
            if($old_x == $old_y)
            {
                $thumb_w    =   300;
                $thumb_h    =   300;
            }
            imagecopyresampled($canvas, $image, $thumb_x, $thumb_y, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
            imagesavealpha($canvas,true);

            // Move the file to the directory where logos are stored
            $logosDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/logos/';
            // write to file
            $fileName = md5(uniqid()).'.'.$file->guessExtension();
            imagepng($canvas, $logosDir.$fileName);
            imagedestroy($canvas);

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,
                array(
                    'label' => false,
                    'attr' => array(
                        'accept' => "image/jpeg, image/png"
                    ),
                    'constraints' => [
                        new File([
                            'maxSize' => '2M',
                            'mimeTypes' => [
                                'image/jpeg',
                                'image/png',
                            ],
                            'mimeTypesMessage' => 'Please upload a JPG or PNG',
                        ])
                    ]
                )
            )

git push origin feature refspec error

If you try to

git push -u origin feature/branch_name

and you get this error “error: src refspec feature/branch_name does not match any”, it means that your local branch are not named correctly. It should be named

feature/branch_name

as well, not just branch_name. Do a “git branch” to confirm yourself.

Tips for working with dates in PHP

Converting from different date format can be challenging. All the php and mysql date functions can be confusing. Just store the date as yearmmdd format, eg 20160220 means year 2016, 02 month and 20 day. This format works well with strtotime function. As per unix timestamp, you can also use basic arithmetic to compare the dates in this format.

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.

http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

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.