Posts

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 ; } } ?>

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...

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 }

Drupal 7 - How to make dynamic select box through Drupal #ajax framework

In Drupal 7 we often need few select boxes to be dependent themselves that means the child select box options are dependent on the parent field's selected option. A very nice example could be Country, State, City those normally has dependency. Though the trick behind Drupal 7 ajax framework is quite simple but it takes quite a time for a first timer as usual ;) . In this post, I have explained a similar example that does the same with no JS required (Drupal 7 does it behind the screen). The Example: I have a Drupal 7 vanilla instance, that has article content type by default. Article content type has 2 select box field depending on each other, Parent field called : Datatype (field_data_type) and child field called as Subdata(field_sub_data). Apart form the Durpal 7 content type config, I am using a custom module named as 'common' and only a hook_form_alter to modify the form. So, Lets start by hooking the form : The form alter: 1 2 3 4 5 6 7 8 9 10 ...