Skip to content

Commit

Permalink
Merge 2101901 into be33b7b
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmorandi committed Apr 10, 2019
2 parents be33b7b + 2101901 commit f6a5b6f
Show file tree
Hide file tree
Showing 29 changed files with 880 additions and 476 deletions.
8 changes: 4 additions & 4 deletions src/ActiveDataProvider.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
/**
* @link https://github.com/chrmorandi/yii2-ldap for the source repository
* @package yii2-ldap
*
* @author Christopher Mota <chrmorandi@gmail.com>
* @license MIT License - view the LICENSE file that was distributed with this source code.
*
* @since 1.0.0
*/

Expand Down Expand Up @@ -45,19 +46,18 @@
* // get the posts in the current page
* $posts = $provider->getModels();
* ```
*
*/
class ActiveDataProvider extends \yii\data\ActiveDataProvider
{
/**
* @inheritdoc
* {@inheritdoc}
*/
protected function prepareTotalCount()
{
if (!$this->query instanceof QueryInterface) {
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.');
}

return (int) $this->query->limit($this->getPagination()->getLimit())->offset(-1)->orderBy([])->count('*', $this->db);
}

}
26 changes: 15 additions & 11 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
/**
* @link https://github.com/chrmorandi/yii2-ldap for the source repository
* @package yii2-ldap
*
* @author Christopher Mota <chrmorandi@gmail.com>
* @license MIT License - view the LICENSE file that was distributed with this source code.
*
* @since 1.0.0
*/

namespace chrmorandi\ldap;

use chrmorandi\ldap\ActiveRecord;
use chrmorandi\ldap\Connection;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationTrait;
Expand Down Expand Up @@ -69,11 +68,11 @@
* is to be added to relational query join condition.
*
* @author Christopher Mota <chrmorandi@gmail.com>
*
* @since 1.0.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface
{

use ActiveQueryTrait;
use ActiveRelationTrait;
/**
Expand All @@ -83,8 +82,9 @@ class ActiveQuery extends Query implements ActiveQueryInterface

/**
* Constructor.
*
* @param string $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
* @param array $config configurations to be applied to the newly created query object
*/
public function __construct($modelClass, $config = [])
{
Expand All @@ -106,8 +106,10 @@ public function init()

/**
* Executes query and returns all results as an array.
*
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* If null, the DB connection returned by [[modelClass]] will be used.
*
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
*/
public function all($db = null)
Expand All @@ -116,7 +118,7 @@ public function all($db = null)
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function populate($rows)
{
Expand All @@ -140,20 +142,22 @@ public function populate($rows)

/**
* Executes the query and returns a single row of result.
*
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
* If this parameter is not given, the `db` application component will be used.
*
* @return array|bool the first row (in terms of an array) of the query result. False is returned if the query
* results in nothing.
* results in nothing.
*/
public function one($db = null)
{
$row = parent::one($db);
if ($row !== false) {
$models = $this->populate($row);

return reset($models) ?: null;
} else {
return null;
return;
}
}

}
55 changes: 31 additions & 24 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php
/**
* @link https://github.com/chrmorandi/yii2-ldap for the source repository
* @package yii2-ldap
*
* @author Christopher Mota <chrmorandi@gmail.com>
* @license MIT License - view the LICENSE file that was distributed with this source code.
*/

namespace chrmorandi\ldap;

use chrmorandi\ldap\ActiveQuery;
use chrmorandi\ldap\Connection;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\BaseActiveRecord;
Expand Down Expand Up @@ -56,7 +54,8 @@ public static function getDb()
}

/**
* @inheritdoc
* {@inheritdoc}
*
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
public static function find()
Expand All @@ -80,6 +79,7 @@ public static function primaryKey()
/**
* Returns the list of attribute names.
* You must override this method to define avaliable attributes.
*
* @return array list of attribute names.
*/
public function attributes()
Expand Down Expand Up @@ -119,18 +119,20 @@ public function attributes()
* $customer->insert();
* ```
*
* @param bool $runValidation whether to perform validation (calling [[validate()]])
* before saving the record. Defaults to `true`. If the validation fails, the record
* will not be saved to the LDAP and this method will return `false`.
* @param string[]|null $attributes list of attributes that need to be saved.
* Defaults to null, meaning all attributes that are loaded from LDAP will be saved.
* meaning all attributes that are loaded from DB will be saved.
* @param bool $runValidation whether to perform validation (calling [[validate()]])
* before saving the record. Defaults to `true`. If the validation fails, the record
* will not be saved to the LDAP and this method will return `false`.
* @param string[]|null $attributes list of attributes that need to be saved.
* Defaults to null, meaning all attributes that are loaded from LDAP will be saved.
* meaning all attributes that are loaded from DB will be saved.
*
* @return bool whether the attributes are valid and the record is inserted successfully.
*/
public function insert($runValidation = true, $attributes = null)
{
if ($runValidation && !$this->validate($attributes)) {
Yii::info('Model not inserted due to validation error.', __METHOD__);

return false;
}

Expand All @@ -140,8 +142,9 @@ public function insert($runValidation = true, $attributes = null)
/**
* Inserts an ActiveRecord into LDAP without.
*
* @param string[]|null $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded will be saved.
* @param string[]|null $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded will be saved.
*
* @return bool whether the record is inserted successfully.
*/
protected function insertInternal($attributes = null)
Expand All @@ -151,8 +154,8 @@ protected function insertInternal($attributes = null)
}

$primaryKey = static::primaryKey();
$values = $this->getDirtyAttributes($attributes);
$dn = $values[$primaryKey[0]];
$values = $this->getDirtyAttributes($attributes);
$dn = $values[$primaryKey[0]];
unset($values[$primaryKey[0]]);

static::getDb()->open();
Expand All @@ -174,7 +177,9 @@ protected function insertInternal($attributes = null)

/**
* @see update()
*
* @param string[]|null $attributes the names of the attributes to update.
*
* @return int|false number of rows updated
*/
protected function updateInternal($attributes = null)
Expand Down Expand Up @@ -212,6 +217,7 @@ protected function updateInternal($attributes = null)

if (empty($attributes)) {
$this->afterSave(false, $attributes);

return 0;
}

Expand All @@ -231,16 +237,17 @@ protected function updateInternal($attributes = null)

/**
* Updates the whole table using the provided attribute values and conditions.
* For example, to change the status to be 1 for all customers whose status is 2:
* For example, to change the status to be 1 for all customers whose status is 2:.
*
* ```php
* Customer::updateAll(['status' => 1], 'status = 2');
* ```
*
* @param string[] $attributes attribute values (name-value pairs) to be saved into the table
* @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter.
* @return integer the number of rows updated
* @param string[] $attributes attribute values (name-value pairs) to be saved into the table
* @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter.
*
* @return int the number of rows updated
*/
public static function updateAll($attributes, $condition = '')
{
Expand All @@ -265,14 +272,15 @@ public static function updateAll($attributes, $condition = '')
* Customer::deleteAll('status = 3');
* ```
*
* @param string|array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter.
* @return integer the number of rows deleted
* @param string|array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter.
*
* @return int the number of rows deleted
*/
public static function deleteAll($condition = '')
{
$entries = (new Query())->select(self::primaryKey())->where($condition)->execute()->toArray();
$count = 0;
$count = 0;

static::getDb()->open();
foreach ($entries as $entry) {
Expand All @@ -284,5 +292,4 @@ public static function deleteAll($condition = '')

return $count;
}

}

0 comments on commit f6a5b6f

Please sign in to comment.