Navigation Menu

Skip to content

Commit

Permalink
Fixed issue #09662
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Oct 16, 2015
1 parent 9b1fe2e commit 301c4e0
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 405 deletions.
2 changes: 1 addition & 1 deletion protected/controllers/SurveysController.php
Expand Up @@ -289,7 +289,7 @@ public function actionDelete($id) {
'entity_id' => $survey->primaryKey,
'crud' => 'delete'
])) {
$survey->deleteDependent();
$survey->deleteDependent(App()->db);
App()->user->setFlash('success', gT("Survey deleted"));
$this->redirect(['surveys/index']);
} else {
Expand Down
120 changes: 43 additions & 77 deletions protected/models/ActiveRecord.php
@@ -1,13 +1,16 @@
<?php
namespace ls\models;

use ls\interfaces\DependentRecordInterface;
use ls\traits\DependentRecordTrait;

/**
* Class ActiveRecord
* @package ls\models
*/
class ActiveRecord extends \CActiveRecord implements \JsonSerializable
class ActiveRecord extends \CActiveRecord implements \JsonSerializable, DependentRecordInterface
{

use DependentRecordTrait;
/**
* Lists the behaviors of this model
*
Expand All @@ -27,52 +30,6 @@ public function behaviors()
return $result;
}

/**
* Modified version that default to do the same as the original, but allows via a
* third parameter to retrieve the result as array instead of active records. This
* solves a joining problem. Usage via findAllAsArray method
*
* Performs the actual DB query and populates the AR objects with the query result.
* This method is mainly internally used by other AR query methods.
* @param CDbCriteria $criteria the query criteria
* @param boolean $all whether to return all data
* @return mixed the AR objects populated with the query result
* @since 1.1.7
*/
protected function query($criteria, $all = false, $asAR = true)
{
if ($asAR === true) {
return parent::query($criteria, $all);
} else {
$this->beforeFind();
$this->applyScopes($criteria);
if (!$all) {
$criteria->limit = 1;
}

$command = $this->getCommandBuilder()->createFindCommand($this->getTableSchema(), $criteria);
//For debug, this command will get you the generated sql:
//echo $command->getText();

return $all ? $command->queryAll() : $command->queryRow();
}
}

/**
* Finds all active records satisfying the specified condition but returns them as array
*
* See {@link find()} for detailed explanation about $condition and $params.
* @param mixed $condition query condition or criteria.
* @param array $params parameters to be bound to an SQL statement.
* @return array list of active records satisfying the specified condition. An empty array is returned if none is found.
*/
public function findAllAsArray($condition = '', $params = array())
{
Yii::trace(get_class($this) . '.findAll()', 'system.db.ar.CActiveRecord');
$criteria = $this->getCommandBuilder()->createCriteria($condition, $params);

return $this->query($criteria, true, false); //Notice the third parameter 'false'
}


/**
Expand All @@ -85,12 +42,10 @@ public function findAllAsArray($condition = '', $params = array())
* @param boolean $forceRefresh Don't use value from static cache but always requery the database
* @return false|int
*/
public function getMaxId($field = null, $forceRefresh = false)
public function getMaxId($field = null)
{
static $maxIds = array();

if (is_null($field)) {
$primaryKey = $this->getMetaData()->tableSchema->primaryKey;
$primaryKey = $this->primaryKey();
if (is_string($primaryKey)) {
$field = $primaryKey;
} else {
Expand All @@ -100,17 +55,12 @@ public function getMaxId($field = null, $forceRefresh = false)
}
}

if ($forceRefresh || !array_key_exists($field, $maxIds)) {
$maxId = $this->dbConnection->createCommand()
->select('MAX(' . $this->dbConnection->quoteColumnName($field) . ')')
->from($this->tableName())
->queryScalar();
return $this->dbConnection->createCommand()
->select('MAX(' . $this->dbConnection->quoteColumnName($field) . ')')
->from($this->tableName())
->queryScalar();

// Save so we can reuse in the same request
$maxIds[$field] = $maxId;
}

return $maxIds[$field];
}

/**
Expand Down Expand Up @@ -139,20 +89,6 @@ public function deleteAllByAttributes($attributes, $condition = '', $params = ar
return parent::deleteAllByAttributes(array(), $criteria, array());
}

/**
* @param null $class
* @return static
*/
public static function model($class = null)
{
if (!isset($class)) {
$class = get_called_class();
}

return parent::model($class);

}

/*
* Creates a (nested) array of this objects' attributes and relations.
*/
Expand Down Expand Up @@ -196,7 +132,7 @@ public function toArray($related = true, $exclude = [])
* @return void
* @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep
*/
function __wakeup()
public function __wakeup()
{
// Re-attach behaviors.
$this->attachBehaviors($this->behaviors());
Expand Down Expand Up @@ -236,8 +172,38 @@ public function __sleep()
* which is a value of any type other than a resource.
* @since 5.4.0
*/
function jsonSerialize()
public function jsonSerialize()
{
return $this->attributes;
}

/**
* @return array List of relation names that contain only dependent records.
*/
public function dependentRelations() {
return [];
}

/**
* Returns the static model of the specified AR class.
* The model returned is a static instance of the AR class.
* It is provided for invoking class-level methods (something similar to static class methods.)
*
* EVERY derived AR class must override this method as follows,
* <pre>
* public static function model($className=__CLASS__)
* {
* return parent::model($className);
* }
* </pre>
*
* @param string $className active record class name.
* @return static active record model instance.
*/
public static function model($className = null)
{
return parent::model(!isset($className) ? get_called_class() :$className);
}


}

0 comments on commit 301c4e0

Please sign in to comment.