Posts

Featured Post

Filtering and Sorting a Doctrine Arraycollection

ArrayCollection object has so many useful in-build methods, that makes ArrayCollection superior comparable to normal PHP array. This post is about how to filter and sort an ArrayCollection so output of elements can be in certain order or filtered. Filter To filter we can use ArrayColleciton in-build filter method , which takes a predicate and returns all the elements satisfied by the predicate provided, keeping the order of the elements intact. A code example would be nice. Code Example : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php /** * @var \Doctrine\Common\Collections\Collection */ $locations ; // ArrayCollection() variable that has $location object in its element. // To filter active locations. $activeLocations = $locations -> filter ( function ( $location ) { return $location -> getStatus (); // filter based on status. }); // Now $activeLocations holds all locaitons from $locations those have an active locaiton. ?> Sort Arra

Drupal 8 - Theme building tips.

To remove existing css from the theme. (eg : css from core) In THEME_NAME_info.yml stylesheets-remove : - core/modules/system/css/system.module.css - core/modules/views/css/views.module.css   Add some JS / CSS only for front-page :  First create a new library in your THEME_NAME_library.yml front-page-library: js: - js/bx-slider.js Then add that library to page through a pre-processor (THEME_NAME.theme) : <?php function THEME_NAME_preprocess_page ( & $variables ) { if ( $variables [ 'is_front' ]) { $variables [ '#attached' ][ 'library' ][] = 'THEME_NAME/front-page-library' ; } } The Home page should have the required ` bx-slider.js ` now. More tips  coming up...

Symfony - How to avoid browser caching assets even after production deployment.

All Browsers have some caching mechanism, they cache most of the css, javascript and images those are loaded frequently from an website. However, this might be a headache for a developer when they made changes to some of the JS, CSS but the changes are not reflected in website, unless you clear the local cache. The global standard is to change the file path name or add a query parameter to your asset as such : 1 example.com / assets / css / common.css ? v = 234 So to implement this Symfony framework supplies a straight forward solution in its asset configuration. It's called assset version . Just add below configuration to your config.yml and rest will be taken care by Symfony framework when it loads an asset to the template. 1 2 3 4 # config.yml framework: assets: version : 'v1451' The above settings is not limited to twog templates, If your application serves APIs and your response is in JSON or XML format, the above settings will als

Javascript - Local Storage with expiry options.

Most of the time, a local storage in HTML5 browsers is helpful to cache content from server for a certain period, Be it a dynamic menu, a big json data or a complex javascript object. All it need is to store in local storage for a certain period, collect data from local storage without hitting server again and again. But when the caching time lapses, the content should be retrieved fresh from the server. Below is a Javascript object which should be pretty much helpful to store and retrieve content in local storage with an optional time limit, expiring which, the returned value will be null instead, prompting us to get fresh data and store again. Moreover, it saves javascript objects to localstorage, with a certain prefix to identify object ( "_|_obj_|_" ), so that it can parse the value at the time of storing and retrieving the object var localStorageObj = { init : function () { this .$timeSuffix = "_lu" ; this .$expiryDuration = 1

Symfony 2 How to keep cloned entity properties unchanged on change of main entity.

Often while we clone an entity, we find the property of new cloned entity gets changed based on changes to the main entity. Mostly when the change occurs on $form->handleRequest($request); Below magic function restricts the properties to keep their value unchanged by cloning deeper state of relationships. Code Example : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?php /** * clone */ public function __clone () { if ( $this -> id ) { // Remove orm reference to DB. $this -> id = null ; // Collect books to clone so that any further change to books in // main entity won't affect the cloned entity. $this -> books = clone $this -> books ; } } ?>

Doctrine mapping (ORM) with Database (DAL), Unidirectional vs Bidirectional.

Creating relationships inside a database is a very common thing for an web developer. Respecting to to RDBMS concept, we can create any sort of relationship inside a database. Things get changed when we have to use Doctrine for the mapping with database. Initially this made pretty much confused to me. Only after live working examples and few docs, I could able to get rid of it somehow. This post is about Doctrine mapping and difference between Unidirectional and Bidirectional relationship. What is Doctrine ? Doctrine is a set of PHP libraries that provides persistence services that is helpful managing PHP objects with Database layer. That means it creates a bridge between PHP light weight mapped objects and Database Objects. So Doctrine provides how to map PHP objects internally by its ORM (Object Relational Mapper) and its DB layer (DAL : Database Abstraction Layer) provides a unique object based query language(DQL : Doctrine Query Language) to write db queries independent of data

How to pass default value for a parameter in Symfony routing.

Sometimes we need to pass a default to any of the passed parameter in a Symfony Routing. Thankfully Symfony Routing has this option as to set in 2nd parameter. Below is how to do this : While registering a router : 1 2 3 acme_read_company: path: /company_read/{id} defaults: { _controller : "AcmeDemoBundle:Default:readCompany" , id : 0 }