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)

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

Basic Ejabberd install/setup on ubuntu (External Script Authentication)

In first article, I discussed the installation and normal chat with users registered internally. Now i am going to discuss the external authentication through a php script.

If you want to authenticate users via your database. their are two alternatives.
1. Using odbc, It have one restriction as its internal database have and that is password must be stored plain. (Latest version may not have this restriction.)
2. Using external authentication script. In this way you can authenticate user via a custom script. Here one good php script can be found there. https://github.com/deceze/ejabberd-extauth-PHP. Now download an extract in some location of your system (not necesarily on apache document root as it will working as cli script ).

Now first create a php script in same folder. I name it auth.php. This file should have similar code suggested at github page. I am updating it using some real query.



#!/usr/bin/php
require 'ejabberd_external_auth.php';
class Auth extends EjabberdExternalAuth {
    protected function authenticate($user, $server, $password) {
        $stmt = $this->db()->prepare("SELECT user_id FROM user WHERE username = ? AND password = ? ");
        $stmt->execute(array($user, password));
        if($stmt->rowCount() >= 0 )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected function exists($user, $server) {
        $stmt = $this->db()->prepare("SELECT user_id FROM user WHERE username = ? ");
        $stmt->execute(array($user));
        if($stmt->rowCount() >= 0 )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
$pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'user', 'password');
new Auth($pdo, 'auth.log');

I skipped $server argument in query check as If you need only users for one chat server. If you need to handle multiple servers then you may need to change code to reflect server too in query.

Now comment internal auth_method by

%%{auth_method, internal}.

And uncomment, change the line

{auth_method, external}.
{extauth_program, "/path/to/auth.php"}.

gave executing permission to auth.php by chmod 0755 auth.php.

NOTE – As this script executed by ejabberd user created at time of ejabberd installation. So this script need execute permission for ejabberd. You may add ejabberd to your user’s group and gave group to execute permission or gave other to execute permission. In both case it is advisable to check execute file after login through other user.

Add chat users in your user table. You will also need admin user to add into your DB with same password to run admin console.

Now restart ejabberd. and verify by adding user at you jabber client. if it going failed. Then verify log file var/log/ejabberd.log. If file have error to not connecting. check php script again for any error.

Ejabberd default enabled MUC (multi user chat or several other modules ), So you can verify/use also this functionality too.

That’s all for this article, In next article I will discuss message logging.

Basic Ejabberd install/setup on ubuntu

In social web appliactions, Jabber chat comes as popular choice for implementing self chat gateway/servers. While there are several chat server are available in market like ejabberd, jabberd2, openfire.
Ejabberd is chat server built in erlang. It provide various recommended features by core/plugins.

Now I am going to give some information for setup ejabberd chat server onto Ubuntu 12.04.

First you need to install ejabberd package. There are two ways.
1. Through ubunty repository. While this option require less work, But you may not get latest package for ejabberd.
2. Download ejabberd package and compile it. This option require more work, But you can get latest package havig more features.

We going through First easy approach.
Here we install ejabberd via

sudo apt-get install ejabberd ejabberd-tools

After installation completion, we will doing some intial setup and configuration.
We need to configure our first chat domain. Here we have config file names ejabberd.cfg at /etc/ejabberd folder.
Open it into your editor, and go for line look like below.

{hosts, ["localhost"]}.

Then change it to add your domain/ip-address as Chat server host.

{hosts, ["localhost","yourhost.com"]}.

Also setup your admin user via change config.

{acl, admin, {user, "", "localhost"}}.

to

{acl, admin, {user, "administrator", "localhost"}}.

Where administrator is admin-name chosen by you. (Ofcourse you can choose any name as admin.)

After save you should also register your admin via command run on shell.


ejabberdctl register administrator localhost password

Now you should restart ejabberd by executing commands

sudo /etc/init.d/ejabberd restart

Open Admin Console via the url – http;//localhost:5280/admin. It ask you the enter admin credentials. enter administrator name and its password, setup via ejabberctl.
Here you can add users via going to virtual hosts -> localhost[yourhost.com] ->  Users.

After register users at here you can start chat by your jabber client example pidgin or spark.

Ejabberd default using it internal database called mnesia to store all kind of information like users, room, logs etc.

In second article of series, I will discuss the external authentication and logging.

Assign role to user in doctrine

From start of days of symfony sfauth plugin play a special role in making developer’s life easy. It give structured way to assign roles and permissions to user.
So at the time of creation of user add/edit forms you can see the groups (roles) and permissions to choose. Also permissions can also direct work for whole group.
But if we need to set permissons/roles to user without using forms then how it can be implemented?
Well yesterday i am searching any workaround for this.
Normally we can use code like that
$sf_user = new sfGuardUser();
$sf_user->setUsername($username);
$sf_user->save();
But i need to set groups against user. Here are a problem, group have many to many relationship with user. Si how i can set the group using similiar code. First i tried following combinations but no one seems to work. Some gave errors some not.
Here $group is not a normal variable but a doctrine collection of group.
$sf_user->setSfUserGroup($group);
$sf_user->setGroup($group);
$sf_user->Groups = $group;

As user can have multiple groups so setting group also reflect the same way.
$sf_user->Groups[] = $group;
Here we can see we are not assigning but adding a group against a user. So above example works.