PHP code example of pianosolo / counter-bundle

1. Go to this page and download the library: Download pianosolo/counter-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pianosolo / counter-bundle example snippets



// app/AppKernel.php
	
public function registerBundles()
{
    $bundles = array(
        // ...
        new PianoSolo\CounterBundle\PianoSoloCounterBundle(),
    );
}



namespace MyBundle\Entity;

use PianoSolo\Traits\CounterTrait;

class MyEntity
{
    use CounterTrait;
    
    //...
}


    $myEntity = new MyEntity();
    $entityManager->persist($myEntity);
    $entityManager->flush();



namespace MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function showEntityAction()
    {
        //...
        
        $myEntity = $myEntityRepository->findOneBy(array('id' => $id));
        $myEntity->getCounter()->addClick(5); // Default value of parameter is 1
        
        $entityManager->persist($myEntity);
        $entityManager->flush();
    }
}


    $count = $myEntity->getCounter()->getCount();

    // Example initial count values of entity
    $count = $myEntity->getCounter()->getCount(); // return 10
    $realCount = $myEntity->getCounter()->getRealCount(); // return 10

    // Adding fake count
    $myEntity->getCounter()->setCorrectionCount(100);
    $entityManager->persist($myEntity);
    $entityManager->flush();

    // Keeping real count
    $count = $myEntity->getCounter()->getCount(); // return 110
    $realCount = $myEntity->getCounter()->getRealCount(); // return 10

    // Delete fake count
    $myEntity->getCounter()->setCorrectionCount(0):
    $entityManager->persist($myEntity);
    $entityManager->flush();