开发者

generic function to check data duplication in tables in zend

I want to create a some kind of data duplication check before insertion into the database.

//@param string $table
//@param Array $columnArray 
//ie.Array(firstColumnName=>$firstData, secondColumnName=>$seconddata )
//@return
function isRecordExist($table, $columnArray)
{


return true; // if record exist in mentioned columns

}

Its just dummy implementation. I am looking some generic implementation which I can place as global function and which can be accessible开发者_StackOverflow中文版 in all modules of the zend project.

As a expert, Could any of you guide me what is the best way to create this function and where so that it would be accessible everywhere.

I have read about Zend_Validate_Db_NoRecordExists, which can be used in elements of form decorator. but in above scenario how we can create class so we can validate the data (in multiple columns) before insertion.

I have create this class in php and used many years. Now when I am working in zend so I am looking for best solutions so that i can create these components to use in my projects.

Thanks for your help.


I did something like this using doctrine.

class mylib_Validate_isRecordExist
{

    public function isRecordExist($modelClass, $column = array()){

        if(empty($modelClass) || empty($column)){
           return true;
        }

        foreach ($column as $k=>$v){
            $where .= ((empty($where))? "" : " AND ") . $k . " = '" . $v . "'";
        }

       $q = Doctrine_Query::create()
            ->select('t.*')
            ->from($modelClass . ' t')
            ->where($where);
       $records =  $q->execute()->count();
       return ($records==0) ? false : true;

    }
}

and then access it using

$isRecordExist = mylib_Validate_isRecordExist::isRecordExist(
                            'campaignManagement_Model_registrant',
                            array('email'=>'test@test', 'postcode'=>'abcdddd')
                            );

I am sure there must be better way to integrate this in zend. but Don't know that could be...


Put it into a model which extends Zend_Db_Table_Abstract then basically run a select query on the table using a where clause with the data array. Not tested but:

$this->select()->from($table, array('id'));
foreach ($columnArray as $key => $value)
         $select->where($key.' = ?', $value);
$res = $select->limit(1)->query()->fetchAll();
return empty($res)?false:true;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜