Builds where conditions (using = operator) from an array of values indexed by fields names
@param mixed $primaryKey string | array indexed by PK fields in case of compound primary key
@throw exception if value(s) do not fit primary key definition
339510
protected function primaryKeyWhere($primaryKeyValue)
    {
        //value is not an array
        if(!is_array($primaryKeyValue)) {
            //turn value into array for first primary key field
            $primaryKeyValue = [
                $this->ORMParameters['primaryKey'][0] => $primaryKeyValue
            ];
        }
        //loop primary key fields
        foreach($this->ORMParameters['primaryKey'] as $field) {
            //check value
            if(!isset($primaryKeyValue[$field])) {
                throw new \Exception(sprintf('missing field % value in where clause for table %s compound primary key', $field, $this->table()));
            } else {
                $this->queryBuilder->where($field, $primaryKeyValue[$field]);
            }
        }
    }
671385
protected function fetch($value)
    {
        if (empty($value)) {
            if (true === $this->null) {
                return;
            }
            throw new Exception('Value in fetch method of PrimaryKeyField cannot be empty');
        }

        return $this->fetchModel($value);
    }
939098
public function wherePrimary($key)
	{
		$this->where($this->primaryKey . ' = ' . $this->primaryModifier, $key);
		return $this;
	}
1231277
public function findByKeys($primaryKeys)
    {
        $data = $this->newDataInstance(null, false);

        if (!is_array($primaryKeys)) {
            throw new \UnexpectedValueException(__METHOD__ . '|$primaryKeys must be an array !');
        }

        //~ Set values before cache
        foreach ($primaryKeys as $field => $value) {
            $this->setDataValue($data, $field, $value);
        }

        $data = $this->getCache($data);

        if ($data === false) {

            foreach ($primaryKeys as $field => $value) {
                $this->addWhere($field, $value);
            }

            $data = $this->selectOne();

            //~ Clear & set in cache
            $this->clear();
            $this->setCache($data);
        }

        return $data;
    }
870193
public static function _setValue ($pk, $name, $value, &$db = FALSE)
	{
		
		// Get database master for write

		if ($db === FALSE)
		{
			$db	=& self::_getDbMaster ();
		}
		
		if (!($db instanceof \PDO))
		{
			throw new Exception ('Invalid or no database resource set');
		}
		
		// Prepare query
		
		$query = $db->prepare ('
		UPDATE `' . static::$dbTable . '`
		SET ' . $name . ' = :val 
		WHERE ' . static::$pk . ' = :pk
		');
		
		// Bind values
		
		$query->bindValue (':val',	$value);
		$query->bindValue (':pk',	$pk);
		
		// Execute

		return $query->execute ();
		
	}
1277173
public function update(array $data, $primaryKey)
    {
        $this->normalizeDataColumns($data);

        $this->setSearchIndexableData($data);

        return $this->app['db']->update($this->getTable(), $data, [ $this->getPrimaryKey() => $primaryKey]);
    }
465685
public static function generateKey($condition)
    {
        if(is_string($condition))
        {
            return $condition;
        }
        else
        {
            $rule = ModelManager::getKeyRule(static::class);
            $result = $rule->rule;
            foreach($rule->paramNames as $paramName)
            {
                if(!isset($condition[$paramName]))
                {
                    throw new \Exception(sprintf('GenerateKey param %s does not exists', $paramName));
                }
                $result = str_replace('{' . $paramName . '}', $condition[$paramName], $result);
            }
            return $result;
        }
    }
66647
public function unique($criteria = null, array $values = array())
    {
        if (!$this->_properties['primary_key'] || (!$values && !$criteria))
            throw new Exception(sprintf(self::E_PrimaryKey, get_called_class()), 1);

        $that = clone $this;
        $values ? $that->find($criteria, $values) : $that->findByPK($criteria);

        return ($that->dry() || $this->pkCompare($that->pkValue()));
    }
265204
private function _fetch_primary_key()
    {
        if($this->primary_key == NULl)
        {
            $this->primary_key = $this->_database->query("SHOW KEYS FROM `".$this->_table."` WHERE Key_name = 'PRIMARY'")->row()->Column_name;
        }
    }
1239585
public function setPrimaryKey( $key )
	{
		if( in_array( $key, $this->fields ) )
		{
			$this->primaryKey = $key;
			return TRUE;
		}
		return FALSE;
	}
1197349
protected function hydrateModel($primaryKey)
    {
        try {
            $query = $this->prepareQuery();
            $this->model = $this->findPk($query, $primaryKey);
        } catch (\Exception $e) {
            Logger::log(get_class($this) . ': ' . $e->getMessage(), LOG_ERR);
        }
    }
1076374
protected function _getPrimaryKeyValues($useDirty = true)
    {
        $primary = array_flip($this->_table->getPrimaryKey());
        if ($useDirty) {
            $array = array_intersect_key($this->_data, $primary);
        } else {
            $array = array_intersect_key($this->_cleanData, $primary);
        }
        if (count($primary) != count($array)) {
            throw new Kwf_Exception("The specified Table does not have the same primary key as the Row");
        }
        return $array;
    }
89220
public static function updateRecord($primaryKey, $fieldValues)
    {
        if (!is_scalar($primaryKey)) {
            throw new InvalidArgumentException(['`:value` is not a valid primary key value', 'value' => $primaryKey]);
        }

        $instance = new static();
        return static::query()->whereEq($instance->getPrimaryKey(), $primaryKey)->update($fieldValues);
    }
1202546
public function listByPrimaryKey($primaryKey)
    {
        if (!isset($this->values[$primaryKey])) {
            throw new OutOfBoundsException(sprintf(
                'The key "%s" does not exist.',
                $primaryKey
            ));
        }

        return $this->values[$primaryKey];
    }
398725
protected function add_primary_keys_to_where($query)
	{
		$primary_key = static::primary_key();
		foreach ($primary_key as $pk)
		{
			$query->where($pk, '=', $this->_original[$pk]);
		}
	}
595551
function hasPrimaryKeyValues() {
		$pks = static::$_primaryKeys;
		if (!$pks)
			return false;

		foreach ($pks as &$pk)
			if ($this->$pk === null)
				return false;
		return true;
	}
617660
public function existsById($id) {
		$primaryKey = $this->getPrimaryKey();
		if (is_array($primaryKey)) {
			throw new RuntimeException('Not supported with multiple primary keys');
		}
		$conditions = [
			$primaryKey => $id
		];
		return parent::exists($conditions);
	}