<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class TidansDashboardActiveRecord extends CActiveRecord {

    /**
     * Inserts a row into the table based on this active record attributes.
     * If the table's primary key is auto-incremental and is null before insertion,
     * it will be populated with the actual value after insertion.
     * Note, validation is not performed in this method. You may call {@link validate} to perform the validation.
     * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false,
     * and its {@link scenario} property will be set to be 'update'.
     * @param array $attributes list of attributes that need to be saved. Defaults to null,
     * meaning all attributes that are loaded from DB will be saved.
     * @return boolean whether the attributes are valid and the record is inserted successfully.
     * @throws CDbException if the record is not new
     */
    public function insert($attributes=null) {
	if (!$this->getIsNewRecord())
	    throw new CDbException(Yii::t('yii', 'The active record cannot be inserted to database because it is not new.'));
	if ($this->beforeSave()) {
	    Yii::trace(get_class($this) . '.insert()', 'system.db.ar.CActiveRecord');
	    $builder = $this->getCommandBuilder();
	    $table = $this->getMetaData()->tableSchema;
	    $cdb = new CommandBuilder($this->getDbConnection()->getSchema());
	    //$data = $cdb->createFindCommand($this->getTableSchema(), $criteria, $this->getTableAlias())->queryAll();
	    $command = $cdb->createInsertCommand($table, $this->getAttributes($attributes));
	    if ($command->execute()) {
		$primaryKey = $table->primaryKey;
		if ($table->sequenceName !== null) {
		    if (is_string($primaryKey) && $this->$primaryKey === null)
			$this->$primaryKey = $builder->getLastInsertID($table);
		    elseif (is_array($primaryKey)) {
			foreach ($primaryKey as $pk) {
			    if ($this->$pk === null) {
				$this->$pk = $builder->getLastInsertID($table);
				break;
			    }
			}
		    }
		}
		$this->_pk = $this->getPrimaryKey();
		$this->afterSave();
		$this->setIsNewRecord(false);
		$this->setScenario('update');
		return true;
	    }
	}
	return false;
    }

    /**
     * Updates records with the specified primary key(s).
     * See {@link find()} for detailed explanation about $condition and $params.
     * Note, the attributes are not checked for safety and validation is NOT performed.
     * @param mixed $pk primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value).
     * @param array $attributes list of attributes (name=>$value) to be updated
     * @param mixed $condition query condition or criteria.
     * @param array $params parameters to be bound to an SQL statement.
     * @return integer the number of rows being updated
     */
    public function updateByPk($pk, $attributes, $condition='', $params=array()) {
	Yii::trace(get_class($this) . '.updateByPk()', 'system.db.ar.CActiveRecord');
	$builder = $this->getCommandBuilder();
	$table = $this->getTableSchema();
	$criteria = $builder->createPkCriteria($table, $pk, $condition, $params);
	$cdb = new CommandBuilder($this->getDbConnection()->getSchema());	
	$command = $cdb->createUpdateCommand($table, $attributes, $criteria);
	return $command->execute();
    }

}
?>
