Create root node if multiple-root tree mode. Update node if it's not new.

@param array $fields

@throws \Exception

@return bool whether the saving succeeds
172050
public function save(array $fields = [])
    {
        if ($this->getIsNewRecord()) {
            if ($this->parent) {
                $this->appendTo($this->parent);
            } else {
                $this->makeRoot();
            }

            return parent::save($fields);
        }

        if (in_array('parent_id', $this->getDirtyAttributes())) {
            $saved = parent::save($fields);
            if ($saved) {
                if ($this->parent) {
                    $this->moveAsLast($this->parent);
                } elseif ($this->isRoot()) {
                    $this->moveAsRoot();
                }

                /** @var array $parent */
                $parent = $this->objects()->asArray()->get(['pk' => $this->pk]);
                if (null !== $parent) {
                    $this->setAttributes($parent);
                    $this->setIsNewRecord(false);
                }

                return $saved;
            }

            return $saved;
        }

        return parent::save($fields);
    }
1328816
public function setFields(array $fields)
    {
        foreach ($fields as $name => $value) {
            $this->setField($name, $value);
        }

        $this->prepareQuery();

        return $this;
    }
406814
public function setMultiFields(array $fields = [])
    {
        if (empty($fields)) {
            unset($this->query['multi-fields']);
        } else {
            $this->query['multi-fields'] = $fields;
        }
        return $this;
    }
1249626
protected function initialize(array $fields)
    {
        $this->content = new Content;
        $this->revision = new Revision;

        foreach ($fields as $field => $settings) {
            $this->data[$field] = $this->initializeField($field, $settings);
        }
    }
1338608
public function addField(Field ...$fields)
    {
        collect($fields)->map(function ($field) {
            $this->group['fields']->put($field->key(), $field->get());
        });

        return $this;
    }
534421
public function insert(array $fields = [])
    {
        $connection = $this->getConnection();

        $this->beforeInsertInternal();

        $connection->beginTransaction();
        try {
            if (($inserted = $this->insertInternal($fields))) {
                $connection->commit();
            } else {
                $connection->rollBack();
            }
        } catch (\Exception $e) {
            $connection->rollBack();
            throw $e;
        }

        $this->afterInsertInternal();

        if ($inserted) {
            $this->setIsNewRecord(false);
            $this->updateRelated();
            $this->attributes->resetOldAttributes();
        }

        return $inserted;
    }
672741
public function saveNode( MW_Tree_Node_Interface $node )
	{
		if( $node->getId() === null ) {
			throw new MW_Tree_Exception( sprintf( 'Unable to save newly created nodes, use insert method instead' ) );
		}

		if( $node->isModified() === false ) {
			return;
		}

		$conn = $this->_dbm->acquire( $this->_dbname );

		try
		{
			$stmt = $conn->create( $this->_config['update'] );
			$stmt->bind( 1, $node->getLabel() );
			$stmt->bind( 2, $node->getCode() );
			$stmt->bind( 3, $node->getStatus() );
			$stmt->bind( 4, $node->getId() );
			$stmt->execute()->finish();

			$this->_dbm->release( $conn, $this->_dbname );
		}
		catch( Exception $e )
		{
			$this->_dbm->release( $conn, $this->_dbname );
			throw $e;
		}
	}
422914
public function addFields(array $fields)
    {
        foreach ($fields as $field)
        {
            $name = $field->getName();
            $addFields[$name] = $field; # we key by name to prevent duplicates!
        }
        
        $keysString = "";
        $fieldStrings = array();
        
        foreach ($addFields as $fieldName => $field)
        {
            $fieldStrings[] = $field->getFieldString();
                
            /* @var $field DatabaseField */
            if ($field->isPrimaryKey())
            {
                $errMsg = 'Do not set field to be primary key when adding fields. ' .
                          'Instead, use the changePrimaryKey method';
                throw new \Exception($errMsg);
            }
            elseif ($field->isKey())
            {
                if ($keysString !== "")
                {
                    $keysString .= ", ";
                }
                
                if ($field->isUnique())
                {
                    $keysString .= "UNIQUE ";
                }
                
                $keysString .= "KEY (`" . $fieldName . "`) ";
            }
        }
        
        $fieldsString = implode(", ", $fieldStrings);
        
        $fieldsString .= $keysString;
        
        $query = 
            "ALTER TABLE " . 
            "`" . $this->m_name . "` " . 
            "ADD " .
            "(" . $fieldsString . ") ";
        
        $result = $this->m_mysqliConn->query($query);
        
        if ($result !== TRUE)
        {
            throw new \Exception('Error creating table with query: ' . $query);
        }
    }
428052
public function fields($fields)
    {
        if (!is_array($fields)) {
            $fields = func_get_args();
        }

        $this->fields = array_merge($this->fields, $fields);

        return $this;
    }
821048
public function setFields(array $fields,array $values)
        {
                $count = count($fields);
                if($count!=count($values)) {
                        throw new \Exception('fields num != values num');
                }
                $params = [];
                for ($i=0; $i<$count; $i++) {
                        $params[] = $fields[$i];
                        $params[] = $values[$i];
                }
                return $this->_redis->hmset($this->_key, $params);
        }
627968
protected function sqlupdate(array $fields, array $nums = null, array $bools = null) {
		$db = $this->db;

		$idfield = $this->idfield();

		$nums != null or $nums = [];
		$bools != null or $bools = [];

		$keys = array_diff(array_keys($fields), [ $idfield ]);
		$strs = array_diff(array_keys($fields), $bools, $nums);

		$setters = \hlin\Arr::join(",\n\t\t\t\t\t       ", $keys, function ($_, $val) {
			return "`$val` = :$val";
		});

		$db->prepare
			(
				"	UPDATE `[table]`
					   SET $setters
					 WHERE `$idfield` = :$idfield
				",
				[ 'table' => $this->constants()['table'] ]
			)
			->strs($fields, $strs)
			->bools($fields, $bools)
			->nums($fields, $nums)
			->execute();

		return $fields[$idfield];
	}
983191
public function update(array $fields = [])
    {
        $connection = $this->getConnection();

        $this->beforeUpdateInternal();

        $connection->beginTransaction();
        try {
            if ($updated = $this->updateInternal($fields)) {
                $connection->commit();
            } else {
                $connection->rollBack();
            }
        } catch (Exception $e) {
            $connection->rollBack();
            throw $e;
        }

        $this->afterUpdateInternal();

        if ($updated) {
            $this->updateRelated();
            $this->attributes->resetOldAttributes();
        }

        return $updated;
    }
721112
public function addFields(array $fields)
    {
        $fields = Validate::arrToLower($fields);
        Validate::types(array_values($fields));
        $fields = array_diff_assoc($fields, $this->schema());
        if (!empty($fields))
        {
            $config         = $this->getConfig();
            $config->schema = array_merge($this->schema(), $fields);
            $data = $this->getData();
            foreach ($data as $key => $object)
            {
                foreach($fields as $name => $type)
                    $data[$key]->{$name} = (Validate::isNumeric($type)) ? 0 : null;
            }
            $this->databaseDocument->put($data);
            $this->databaseConfig->put($config);
        }
        return true;
    }
733454
public function setFields($fields)
    {
        if (\is_array($fields)) {
            $fields = \implode(',', $fields);
        }

        return $this->setParam('fields', (string) $fields);
    }
1353626
public function create($timestamps = true, $softDeletes = false, $fields=[])
    {
        $path = $this->getpath($this->name);

        if ($this->files->exists($path)) {
            throw new \Exception("Factory [$this->name] already exists!");
        }

        $stub = $this->files->get($this->getStub());

        $stub = $this->replaceClass($stub, $this->name)
            ->replaceNamespace($stub, $this->name)
            ->replaceFakerArray($stub, $fields, $timestamps, $softDeletes)
            ->replaceSpace($stub);

        $this->files->put($path, $stub);

        return $path;
    }
1212972
public function setFieldsFromArray(Array $fields) {
    foreach ($fields as $field => $value) {
      $this->set($field, $value);
    }
    return $this;
  }
1237902
public function createChild($fields)
    {
    	if (!isset($fields['alias'])) throw new Exception\Form(Exception\CMS::INVALID_PARAMS, 'alias');
    	if (!isset($fields['name'])) throw new Exception\Form(Exception\CMS::INVALID_PARAMS, 'name');
    	if (!isset($fields['typ'])) throw new Exception\Form(Exception\CMS::INVALID_PARAMS, 'typ'); //  тип материалов
    	
    	if (!isset($fields['server'])) $fields['server'] = 0;
    	
        try {
            $c = $this->getChildByAlias($fields['alias']);
        } catch (\Exception $e) {
            $c = false;
        }
    	if ($c) throw new Exception\Form(Exception\CMS::CAT_EXISTS, 'alias',$fields['alias']);
    	
    	if ($this->id == 0 && file_exists(DOCROOT.$fields['alias'])) 
            throw new Exception\Form(Exception\CMS::CAT_PHYSICAL_EXISTS, 'alias', $fields['alias']);
    	   	
      $type = 0;
    	
    	if ($fields['link']) {
          $type = $type | Catalog::LINKED;
          if (!$fields['name'] || !$fields['alias']) {
              $c = Catalog::getById((int)$fields['typ']);
              if (!$fields['name']) $fields['name'] = $c->name;
              if (!$fields['alias']) $fields['alias'] = $c->alias;
          }
      } else {
          $type = $type | Catalog::INHERIT;
      }
         
      if ($fields['autoalias']) {
          $type = $type | Catalog::AUTOALIAS | $fields['autoalias'];          
      }
	  if (isset($fields['autoalias'])) unset($fields['autoalias']);
    	
	  $conn = self::getDbConnection();	
	
      $tag = $conn->fetchColumn("SELECT MAX(A.tag)+1 
            	        FROM dir_data A, dir_structure B, dir_structure C
                        WHERE A.id=B.data_id and C.data_id=? and B.lft BETWEEN C.lft and C.rght and B.level=C.level+1", array($this->id));
      if (!$tag) $tag = 1;
      
      $sql="INSERT INTO dir_data 
			SET tag=$tag,
				name=".$conn->quote($fields['name']).",
				tablename=".$conn->quote($fields['alias']).",
				type=$type,
				typ=".(int)$fields['typ'].",
				dat=NOW(),
				is_server=".(int)$fields['server'];
				
      if ((int)$fields['id']) {
          $sql .= ',id='.(int)$fields['id'];
          unset($fields['id']);
      }	  
				
      $conn->executeQuery($sql);
      $id = $conn->lastInsertId();
    	
    	$p = array();
    	foreach ($fields as $name => $value)
    		if ($name!='alias'&&$name!='server'&&$name!='name'&&$name!='typ'&&$name!='link')
    			$p[] = "$name='".addslashes($value)."'";
    	if (sizeof($p) && $id)
    		$conn->executeQuery('UPDATE dir_data SET '.implode(',', $p).' WHERE id='.$id);
    
        $tree = new CDBTree('dir_structure');
        $prnt = $conn->fetchColumn('select id from dir_structure where data_id=?', array($this->id));
        $tree->insert($prnt, array('data_id'=>$id));
        	   
        if ($fields['server']) {
        		$conn->executeQuery("delete from server_aliases where id=$id");
        		$conn->executeQuery("insert into server_aliases (id,name) values ($id,'".$fields['alias']."')");
        		$slot = new Cache\Slot\ServerByDomain($fields['alias']);
        		$slot->remove();
        }
        
        $this->updateCache();
        
    	return $id;

    }