Monitor changes on cli live

If you want to watch or monitor a certain process on the cli (to check if changes happen) you can use the „watch“ command. For example if you want to check if a ffmpeg process is going to start or quit use the following command:

watch -n 3 „ps afx | grep ffmpeg“

In this example the command „ps afx | grep ffmpeg“ will be refreshed every 3 seconds on the cli.

Juli 6th, 2018

Javascript: onclick and onblur priority issues

Imagine you have search input field which also opens a suggest container as soon as you begin to type. Also imagine you want the suggest container to vanish as soon as you leave the search input field. You put an onblur() event on your search input field. Now you also want to click on a value of the suggest container to execute a search. The problem now is, if you use an onclick() event for triggering the search via suggest container, the onclick() event will never be triggered. The onblur() of the search input is executed before the onclick() of the suggest container. To solve this issue just swap your onclick() with a onmousedown() event.  That solution let’s the browser automatically re-order the priorities of these event handlers.

Juni 15th, 2018

Query elasticsearch with uri and get parameters

If you want to query elastic search directly via uri you can use the source parameter. This parameter helps you to fire requests like this:

curl -XGET "http://localhost:9200/employee/_search?search_type=count&pretty" -d '{
  "aggregations": {
    "profile": {
      "terms": {
        "field": "_type"
      },
      "aggs": {
        "hits": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}'

 

directly via URL/GET:

curl -XGET 'http://localhost:9200/employee/_search?search_type=count&pretty&source={"aggregations":{"profile":{"terms":{"field":"_type"},"aggs":{"hits":{"top_hits":{"size":1}}}}}}'

 

Source: stackoverflow

Juni 14th, 2018

How to replace master branch in git from another branch

If you happen to come into the situation that your master branch is screwed (e.g. by falsely merging and reverting other/old branches into the master) you might need to replace your master with another branch (like your develop branch). That is actually pretty easy, all you need to do is executing the following git commands:

git checkout develop
git merge -s ours master
git checkout master
git merge develop

 

After you executed the commands your master branch is completely up-to-date with your develop branch.

Source: stackoverflow

April 7th, 2018

„insert ignore on duplicate entries“ in Doctrine / Symfony

As it’s not possible to do insert or update ignore in doctrine, those who still want to ignore entities when they are already in the database might find this snippet useful:

$doctrine = $this->getContainer()->get('doctrine');
  foreach ($entities as $entity) {
    try {
      $em = $doctrine->getManager();
      $entity->setUniqueValueThatCausesAUniqueConstraintViolation(42);
      $em->persist($entity)
      $em->flush();
    } catch (UniqueConstraintViolationException $e) {
      $doctrine->resetManager();
    }
}

 

Calling $doctrine->resetManager(); is necessary as the entity manager will be closed otherwise and you will not be able to use it anymore.

Please be aware that the disadvantage of that solution is a huge performance decrease as we are flushing the entity manager every round.

Februar 16th, 2018

MacBook Camera(s) not working

If the build-in camera in your MacBook suddenly stopped working and/or it doesn’t recognize any external camera devices, hit the following bash command:
sudo killall VDCAssistant

Februar 1st, 2018

Composer: Not enough memory for installing Symphony Bundle

In case you’re getting the annoying memory message „PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 20480 bytes) …
Try this one: php -d memory_limit=-1 /usr/local/bin/composer require component

Januar 5th, 2018

Symfony Sonata Admin Bundle: Not saving foreign ID in a OneToMany relationship

If you’re setting up a one-to-many relationship between two entities, don’t forget to set the parent for your child entity as well. Otherwise you’ll end up saving the data without the foreign (parent) id in your database.

In your parent entity class:

public function setChildren($children)
{
  if (count($children) > 0) {
    foreach ($children as $child) {
      $this->addChild($child);
    }
  }

  return $this;
}

public function addChild(Child $child)
{
  $child->setParent($this);
  $this->children->add($child);
}

 

The „$child->setParent($this);“ part is something that can be easily overseen …

For finally setting the children in the parent entity, you’ll have to add/modify your parent entity listener:

public function prePersist($parent)
{
  $this->preUpdate($parent);
}

public function preUpdate($parent)
{
  $parent->setChildren($parent->getChildren());
}

 

Source: stackoverflow

Dezember 24th, 2017

Symfony: ManyToMany relation with self reference

Creating a many to many relation with self reference is straight forward (see docs). However I had to edit the „getFriendsWithMe“ method to get friends for ALL users (the original user where the friends are added and vice versa: The „new“ friends of the user should also display the original user as their friend):

public function getFriendsWithMe()
{
  return new ArrayCollection(
    array_merge(array_merge($this->friendsWithMe->toArray(), $this->myFriends->toArray()))
  );
}

Without the editing, I only got the original user to show his new friends but the new friends never showed the original user as their friend.

November 17th, 2017

Using Twig 2 (within Symfony) with PHP 5.xx …

… causes the following error message: „Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given (…)“. Just update your PHP version and all should be good.
The Twig version you are currently using in your Symfony can be seen within your composer.lock file.

Oktober 23rd, 2017