Composite key type duplicate key check with ZF2 Doctrine

Their are instances when we need to add some composite keys in our DBs. Then we also need a check on application side too. This is also same as duplicate check on multiple fields.

In ZF2, we know about Zend\Validator\Db\RecordExists & Zend\Validator\Db\NoRecordExists combo for duplicate check on database side (Zend DB). Their are also respective relative in doctrine called DoctrineModule\Validator\ObjectExists & DoctrineModule\Validator\NoObjectExists. They do the same job as Zend DB counter part with one caveat.

They do not support multiple fields when work with InputFilters. You can pass array but that gave error "zf2 Provided values count is 1, while expected number of fields to be matched is 2". The solution are using your custom validator.

I tried here to write a Validator Plugin which extends DoctrineModule\Validator\NoObjectExists and have support for multiple fields. See implementation code .


        $inputFilter->add($factory->createInput(array(
            'name' => 'studentId',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'Student\Validate\NoObjectExists',
                    'options' => array(
                        'object_repository' => $this->getObjectManager()->getRepository('Student\Entity\Student'),
                        'fields' => array('studentId', 'class'),
                    )
                )
            ),
        )));
        

Validator Plugin Code are as :


    namespace Student\Validate;

    use DoctrineModule\Validator\NoObjectExists as DoctrineModuleNoObjectExists;

    class NoObjectExists extends DoctrineModuleNoObjectExists
    {
        protected $messageTemplates = array(
            self::ERROR_OBJECT_FOUND    => "An object matching combination of fields was found",
        );

        public function isValid($value, $context = null)
        {
            $valueArray = array(); 
            foreach($this->fields as $name => $val)
            {
                $valueArray[] = $context[$val];
            }    
            $value = $this->cleanSearchValue($valueArray);
            
            $match = $this->objectRepository->findOneBy($value);
            
            if (is_object($match)) {
                $this->error(self::ERROR_OBJECT_FOUND, $value);

                return false;
            }

            return true;
        }
    }
      

Further link can be found at : https://github.com/kuldeep-k/zf2doctrine/blob/master/module/Student/src/Student/Validate/NoObjectExists.php

PHP 5.4 new features

PHP have introduced lot of interesting features in last few major versions. Some are obviously inspired from latest programming trends & from other languages. But these features make php more elegant and promising to code. Now I will discuss some of features in php version 5.4 I liked in following sections.

  • Short Array Syntax
  • Binary Numbers
  • Class::{expr}() and (new foo)->bar()) syntax.
  • Traits
  • Array Dereferencing

Short Array Syntax

This is my personal favorite feature. PHP have long history for define array through php function like this

$single_level = array(1, 2, "a" => "bcd");
$multi_level = array('first' => array(1, 2), 'second' => array(3,4), 'third' => array('3f' => array(5,6), '3s' => array(7,8)));

Not we have short syntax which eliminate the using array keyword using more mainstream operator for this.

$single_level = [1, 2, "a" => "bcd"];
$multi_level = ['first' => [1, 2], 'second' => [3,4], 'third' => ['3f' => [5,6], '3s' => [7,8]]];

Binary Numbers

PHP have support for octal and hexdecimal numbers for long time. But same privilege is not available for binary numbers. functions like decbin & bindec is only deal with string representation for binary numbers. Now we have binary number support they can be defined like.

$bin = 0b11011;

See 0b prefix which define for binary number. We already have prefix 0 and 0x for octal and hexadecimal repectively.

Class::{expr}()

If we need to call dynamic method for a class in earlier versions we something similar code.

class A 
{
    public static function functA()
    {
        echo "call A".PHP_EOL;
    }
    public static function functB()
    {
        echo "call B".PHP_EOL;
    }
}

$vars = array('A, 'B');
foreach($vars as $var)
{
    $c = 'funct'.$var;
    A::$c();
}

Now in 5.4 version instead of variable for method name we can code like

(new foo)->bar())

If we need to call a method for a class in earlier versions we something similar code.

class A 
{
    public $var = 10;
    public function methd()
    {
        echo $this->var.PHP_EOL;
    }
}

$a = new A();
$a->methd();

Now in 5.4 version instead of variable for method name we can code like this

(new A)->methd();

Like previous example this one too eliminate the use of variable for just one method calling.

Traits

I write a separate blog for introduce trait at https://kuldeep15.wordpress.com/2015/03/17/php-traits/

Array De-referencing

PHP provide a way to avoid creating variable when we need to access a particular member of array, specially when we accessing data dirtecly returned from function.

In previous versions :

function foo
{
    return array("a" => "First", "b" => "Second");
} 

$a = foo();
echo $a[1];

Using array de-referencing

function foo
{
    return array("a" => "First", "b" => "Second");
} 

echo foo()[1];

You can also use it for some special cases like below.

$str = "A,B,C,D,E";
echo explode(',',$str)[2];

In further articles I will explore interesting features from 5.5 & 5.6

PHP Traits

Traits are a new phenomenon in languages which do not support multiple inheritance. PHP also not directly support multiple inheritance while interface can be used to simulate but due to their abstract behavior, they are less usefull when we need functionality from two or more parent class into child class and we want to through inheritance.

Here Traits are introduced. They do exactly what multiple inheritance do, And while take care of method collision.

See following example in PHP without trait.

class Phone
{
    public function ring()
    {
    }
    public function message()
    {
    }
} 

class Computer
{
    public function email()
    {
    }
    public function surf()
    {
    }
    public function chat()
    {
    }
} 

class Smartphone extends Phone
{

}

We can not inherit the functionality of Computer into Smartphone class here. As computer class is already defined and user in another world we can not convert it into interface. So we need to make Computer interface for Smartphone class need to reimplement which duplicate Computer class structure.

interface ComputerInterface
{
    public function email();
    public function surf();
    public function chat();
}

class SmartPhone  extends Phone implements ComputerInterface 
{
    public function email()
    {
        // Copy implement here from computer class
    }
    public function surf()
    {
        // Copy implement here from computer class
    }
    public function chat()
    {
        // Copy implement here from computer class
    }
}

As we see this is not efficient way some times. So we need to use design patterns to resolve this problem. But trait resolve this problem in language itself.

trait Phone
{
    public function ring()
    {
    }
    public function message()
    {
    }
    public function login()
    {
    }
    public function logout()
    {
    }
} 

trait Computer
{
    public function email()
    {
    }
    public function surf()
    {
    }
    public function chat()
    {
    }
    public function login()
    {
    }
    public function logout()
    {
    }
}

class SmartPhone
{
    use Phone, Computer{
        Phone::login insteadof Computer;
        Computer::logout insteadof Phone;
    }
}

$smart = new SmartPhone();
$smart->login();
$smart->email();
$smart->ring();
$smart->logout();

Here we also see how trait handle same method name collision. Their are more functionality similar to classes available in trait like inheritance and abstract methods.

See (http://php.net/manual/en/language.oop5.traits.php)

Programmer wannable attitude

Well, Grasshopper, or Unschooled Acolyte, or whatever your title of choice may be…

You did not hear this from me.

But most developers belong to the Church of Pain and we pride ourselves on our arcane talents, strange cryptic mumblings and most of all, the rewards due the High Priesthood to which we strive to belong.

Let me put it bluntly. Some of this very complicated logic is complicated because it’s very complicated. And pretty little tools would do both the complexity and us injustice, as high priests or priests-in-training of these magical codes.

One day we will embrace simple graphical tools. But only when we grow bored and decide to move on to higher pursuits of symbolic reasoning; then and not a moment before will we leave you to play in the heretofore unimaginable sandbox of graphical programming tools. Or maybe we’ll just design some special programs that can program on our behalf instead, and you can blurt out a few human-friendly (shiver) incantations, and watch them interpret and build your most likely imprecise instructions into most likely unworkable derivative codes. Or you can just take up LOGO like they told you to when you were but a school child in the… normal classes.

Does that answer your impertinent question?

Comes from http://ask.slashdot.org/comments.pl?sid=4766093&cid=46191783

procedural MVC in php

Well I am reading some question on http://programmers.stackexchange.com/questions/184664/mvc-pattern-on-procedural-php regarding procedural MVC in php. Then I am thinking why it should not be possible? After all MVC is about architecture, You can make good architecture also in procedural programming. You just need to design proper architecture. As I checkout on MVC Frameworks, their are some common terms like

  • Bootstrap script, which initializing system, core services, routers.
  • Controllers
  • Models
  • Views

Also library, helpers are available depend on project wise.

Now I am giving you some light on, How a MVC (very basic of course ) can be built on following style.

Architecture

Project Root
    config
        config.php
    lib
        core.php
    controllers 
        UserController.php
        BaseController.php
    models
        User.php
    views
        user 
            index.php
        base 
            welcome.php
    resources
        css
            style.css

config.php Sample Code :

define('MODEL_DIR', 'models');
define('VIEW_DIR', 'views');

$config = array (
    'default_module' => 'base', 
    'default_action' => 'index',
);

lib/core.php Sample Code :

function safe($param) {
    return addslashes($param);
}

function render($file, $data)
{
    $layout_file = VIEW_DIR.'/layouts/layout.php';
    ob_start();
    include_once($file);
    $content = ob_get_clean();
    include_once($layout_file);
}

Bootstrap index.php Sample Code :

Let Request url is like /index.php?module=user&action=index

require_once('config/config.php');
require_once('lib/core.php'); // For some core functions like render
//require_once('lib/db.php');   // For DB Abstraction [ Later Improvement ] 

$module = isset($_REQUEST['module'])?safe($_REQUEST['module']):$config['default_module'];
$action = isset($_REQUEST['action'])?safe($_REQUEST['action']):$config['default_action'];

$controller_file = 'controllers/'.ucfirst($module).'Controller.php';
if(!file_exists($controller_file))
{
    trigger_error('Invalid Controller');
    exit;
}
require_once($controller_file);
$function = strtolower($module).'_controller_'.$action;
if(!function_exists($function))
{
    trigger_error('Invalid Controller Action');
    exit;
}
call_user_func($function, $_REQUEST);

BaseController.php Sample Code :

function base_controller_index($request)
{
    render(VIEW_DIR.'/base/welcome.php', array());
}

UserController.php Sample Code :

function user_controller_index($request)
{
    require_once(MODEL_DIR.'/User.php');
    $data = user_model_list($request);
    render(VIEW_DIR.'/user/index.php', $data);
}

function user_controller_add($request)
{
}

function user_controller_edit($request)
{
}

function user_controller_view($request)
{
}

Model User.php Sample Code :

function user_model_list($request)
{
    return array(
        array('name' => 'test', 'sname' => 'test1', 'city' => 'testc'),
        array('name' => 'abc', 'sname' => 'xyz', 'city' => 'mno'),
    ); 
}

function user_model_add($request)
{
}

function user_model_edit($request)
{
}

function user_model_view($request)
{
}

Layout file views/layouts/layout.php Sample Code :

<html>
<head>
<link rel=’stylesheet’ href=’resources/css/style.css’ type=’text/css’ media=’all’ />
<head>
<body>
<div><h1>Logo</h1></div>
<div><h2>Heading</h2></div>
<div class=”content”>
<?php echo $content ?>
</div>
<body>
</html>

View views/base/welcome.php Sample Code :

<i>Welcome</i>
<p>
<a href=”?module=user&action=index”>User Listing Page</a>
</p>

View views/user/index.php Sample Code :

<table class=”listing”>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
</tr>
<thead>
<tbody>
<?php foreach($data as $row) { ?>
<tr>
<td><?php echo $row[‘name’] ?></td>
<td><?php echo $row[‘sname’] ?></td>
<td><?php echo $row[‘city’] ?></td>
</tr>
<?php } ?>
<tbody>
</table>

Sample code could be found at https://github.com/kuldeep-k/procMVC

chat box effects by css

It is something strange for CSS effects article from a programmer. But yes today I am sitting on PC and check out some CSS effects after some explore I get the cute box can be used for a chat box.

These is generated after some css3 special effects like shadow, radius, transform. The css can be applied like that.

<style>
    .box {
        width: 300px;
        min-height: 50;
        border-radius: 5px;
        box-shadow: 3px 3px 5px 6px #ccc;
        padding: 15px;
        margin: 10px;
        font-family: arial;
        font-size: 13px;
        transform: skew(-10deg);
        -webkit-transform: skew(-10deg);
              
    }

</style>

Screenshot for resulted box is 

Image

While all effects work on FF20 on without using -moz prefix, While for strange reason Chrome 25 still need -webkit prefix to apply transform effects.

You can check the demo from http://jsfiddle.net/RqQbp/

Some Quotes

I think he knows what Rome is. Rome is the mob. Conjure magic for them and they’ll be distracted. Take away their freedom and still they’ll roar. The beating heart of Rome is not the marble of the senate, it’s the sand of the coliseum. He’ll bring them death – and they will love him for it. (From Gladiator Movie, Ref Mob mentality)