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.

Original 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());
}

Original source

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

PHP + Elasticsearch: No alive nodes found in your cluster

When working with Elasticsearch and php (i.e. see elasticsearch for php) it might happen that you’ll get an „No alive nodes found in your cluster“ error when connecting to the cluster from your hoster while everything works fine locally.

The (main) reason might be that you’re using the given port 9243 and the hoster doesn’t allow your scripts to connect to that host. Just change the port to the standard ssl port 443 and everything should work as intended.

August 10th, 2017

New (free) resource for stock photos

Shopify launched a new project called „Burst“ that you can add as a new stock photo resource. The service provides „Free photography for entrepreneurs“ as it is described on Burst’s homepage.

The photos come along with a Creative Commons Zero license, which means you’re allowed to basically do everything with the photos (see details on Burst’s FAQ). Tho you’re still allowed to credit the author 😉

Original source: techcrunch.com

For everyone who didn’t find what he/she was looking for: whoishostingthis.com put together a list with (almost) 100 free and freemium sources of stock photos: wiht.co/freestockimageslist

April 4th, 2017

Mac Apache not starting because of PHP standard module

If you have a problem with Apache not starting although there’s no error log entry (and sudo apachectl configtest also doesn’t help), try
sudo apachectl -X
That way the debug mode is enabled and you get to see every problem that prevents Apache from starting. Even if it’s a PHP warning that is caused by the standard PHP module:
Warning: PHP Startup: Unable to load dynamic library '/usr/local/php5/lib/php/extensions/no-debug-non-zts-20160303/curl.so' - dlopen(/usr/local/php5/lib/php/extensions/no-debug-non-zts-20160303/curl.so, 9): Library not loaded: /usr/local/php5/lib/libcurl.4.dylib

If you encounter that warning / error, comment the php5_module line out (to find in /etc/apache2/httpd.conf):
#LoadModule php5_module libexec/apache2/libphp5.so

Februar 24th, 2017