标签:php laravel
<?php namespace Illuminate\Validation; use Illuminate\Support\Str; use Illuminate\Database\ConnectionResolverInterface; // ok,just namespace class DatabasePresenceVerifier implements PresenceVerifierInterface {// like Database Presence Verifier can implements Presence Verifier Interface /** * The database connection instance. * * @var \Illuminate\Database\ConnectionResolverInterface */ protected $db;// database connection instance. /** * The database connection to use. * * @var string */ protected $connection = null;// connection to use as what /** * Create a new database presence verifier. * * @param \Illuminate\Database\ConnectionResolverInterface $db * @return void */ public function __construct(ConnectionResolverInterface $db) { $this->db = $db;//Connection Resolver Interface $db is a instance about this, just a class }// Create a new database presence verifier /** * Count the number of objects in a collection having the given value. * * @param string $collection * @param string $column * @param string $value * @param int $excludeId * @param string $idColumn * @param array $extra * @return int */ public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) { $query = $this->table($collection)->where($column, ‘=‘, $value);// get the query sql // first get Table // and get the where condition if (! is_null($excludeId) && $excludeId != ‘NULL‘) { $query->where($idColumn ?: ‘id‘, ‘<>‘, $excludeId); }// and set the ID filed field range foreach ($extra as $key => $extraValue) { $this->addWhere($query, $key, $extraValue);// }// and use this way to add other condition. return $query->count();// get result }// get Count about collection having the given value /** * Count the number of objects in a collection with the given values. * * @param string $collection * @param string $column * @param array $values * @param array $extra * @return int */ public function getMultiCount($collection, $column, array $values, array $extra = []) { $query = $this->table($collection)->whereIn($column, $values);//first get query foreach ($extra as $key => $extraValue) { $this->addWhere($query, $key, $extraValue); }// second ,get this extra condition return $query->count();//get result }//Count the number of objects in a collection with the given values. /** * Add a "where" clause to the given query. * * @param \Illuminate\Database\Query\Builder $query * @param string $key * @param string $extraValue * @return void */ protected function addWhere($query, $key, $extraValue) { if ($extraValue === ‘NULL‘) {// if NULL $query->whereNull($key); } elseif ($extraValue === ‘NOT_NULL‘) {// if NOT NULL $query->whereNotNull($key); } elseif (Str::startsWith($extraValue, ‘!‘)) {// if it start with ! $query->where($key, ‘!=‘, mb_substr($extraValue, 1)); //like != not equal } else { $query->where($key, $extraValue);// the normal condition } }// add a where clause[looks like list ] to the given query /** * Get a query builder for the given table. * * @param string $table * @return \Illuminate\Database\Query\Builder */ protected function table($table) { return $this->db->connection($this->connection)->table($table); }//Get a query builder for the given table // connection table /** * Set the connection to be used. * * @param string $connection * @return void */ public function setConnection($connection) { $this->connection = $connection; }// set connection }
本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1829520
[李景山php]每天laravel-20160926|DatabasePresenceVerifier.php
标签:php laravel
原文地址:http://jingshanls.blog.51cto.com/3357095/1829520