public function insert($data = null, bool $returnID = true)
	{
		$escape = null;

		$this->insertID = 0;

		if (empty($data))
		{
			$data           = $this->tempData['data'] ?? null;
			$escape         = $this->tempData['escape'] ?? null;
			$this->tempData = [];
		}

		if (empty($data))
		{
			throw DataException::forEmptyDataset('insert');
		}

		// If $data is using a custom class with public or protected
		// properties representing the table elements, we need to grab
		// them as an array.
		if (is_object($data) && ! $data instanceof stdClass)
		{
			$data = static::classToArray($data, $this->primaryKey, $this->dateFormat, false);
		}

		// If it's still a stdClass, go ahead and convert to
		// an array so doProtectFields and other model methods
		// don't have to do special checks.
		if (is_object($data))
		{
			$data = (array) $data;
		}

		// Validate data before saving.
		if ($this->skipValidation === false)
		{
			if ($this->validate($data) === false)
			{
				return false;
			}
		}

		// Save the original data so it can be passed to
		// any Model Event callbacks and not stripped
		// by doProtectFields
		$originalData = $data;

		// Must be called first so we don't
		// strip out created_at values.
		$data = $this->doProtectFields($data);

		// Set created_at and updated_at with same time
		$date = $this->setDate();

		if ($this->useTimestamps && ! empty($this->createdField) && ! array_key_exists($this->createdField, $data))
		{
			$data[$this->createdField] = $date;
		}

		if ($this->useTimestamps && ! empty($this->updatedField) && ! array_key_exists($this->updatedField, $data))
		{
			$data[$this->updatedField] = $date;
		}

		$data = $this->trigger('beforeInsert', ['data' => $data]);

		// Must use the set() method to ensure objects get converted to arrays
		$result = $this->builder()
				->set($data['data'], '', $escape)
				->insert();

		$this->trigger('afterInsert', ['data' => $originalData, 'result' => $result]);

		// If insertion failed, get our of here
		if (! $result)
		{
			return $result;
		}

		$this->insertID = $this->db->insertID();

		// otherwise return the insertID, if requested.
		return $returnID ? $this->insertID : $result;
	}
public function insert($data = null, bool $returnID = true)
	{
		$escape = null;

		$this->insertID = 0;

		if (empty($data))
		{
			$data           = $this->tempData['data'] ?? null;
			$escape         = $this->tempData['escape'] ?? null;
			$this->tempData = [];
		}

		if (empty($data))
		{
			throw DataException::forEmptyDataset('insert');
		}

		// If $data is using a custom class with public or protected
		// properties representing the table elements, we need to grab
		// them as an array.
		if (is_object($data) && ! $data instanceof stdClass)
		{
			$data = static::classToArray($data, $this->primaryKey, $this->dateFormat, false);
		}

		// If it's still a stdClass, go ahead and convert to
		// an array so doProtectFields and other model methods
		// don't have to do special checks.
		if (is_object($data))
		{
			$data = (array) $data;
		}

		// Validate data before saving.
		if ($this->skipValidation === false)
		{
			if ($this->validate($data) === false)
			{
				return false;
			}
		}

		// Save the original data so it can be passed to
		// any Model Event callbacks and not stripped
		// by doProtectFields
		$originalData = $data;

		// Must be called first so we don't
		// strip out created_at values.
		$data = $this->doProtectFields($data);

		// Set created_at and updated_at with same time
		$date = $this->setDate();

		if ($this->useTimestamps && ! empty($this->createdField) && ! array_key_exists($this->createdField, $data))
		{
			$data[$this->createdField] = $date;
		}

		if ($this->useTimestamps && ! empty($this->updatedField) && ! array_key_exists($this->updatedField, $data))
		{
			$data[$this->updatedField] = $date;
		}

		$data = $this->trigger('beforeInsert', ['data' => $data]);

		// Must use the set() method to ensure objects get converted to arrays
		$result = $this->builder()
				->set($data['data'], '', $escape)
				->insert();

		$this->trigger('afterInsert', ['data' => $originalData, 'result' => $result]);

		// If insertion failed, get our of here
		if (! $result)
		{
			return $result;
		}

		$this->insertID = $this->db->insertID();

		// otherwise return the insertID, if requested.
		return $returnID ? $this->insertID : $result;
	}
public function insert ( $ data = null , bool $ returnID = true ) { $ escape = null ; $ this -> insertID = 0 ; if ( empty ( $ data ) ) { $ data = $ this -> tempData [ 'data' ] ?? null ; $ escape = $ this -> tempData [ 'escape' ] ?? null ; $ this -> tempData = [ ] ; } if ( empty ( $ data ) ) { throw DataException :: forEmptyDataset ( 'insert' ) ; } // If $data is using a custom class with public or protected // properties representing the table elements, we need to grab // them as an array. if ( is_object ( $ data ) && ! $ data instanceof stdClass ) { $ data = static :: classToArray ( $ data , $ this -> primaryKey , $ this -> dateFormat , false ) ; } // If it's still a stdClass, go ahead and convert to // an array so doProtectFields and other model methods // don't have to do special checks. if ( is_object ( $ data ) ) { $ data = ( array ) $ data ; } // Validate data before saving. if ( $ this -> skipValidation === false ) { if ( $ this -> validate ( $ data ) === false ) { return false ; } } // Save the original data so it can be passed to // any Model Event callbacks and not stripped // by doProtectFields $ originalData = $ data ; // Must be called first so we don't // strip out created_at values. $ data = $ this -> doProtectFields ( $ data ) ; // Set created_at and updated_at with same time $ date = $ this -> setDate ( ) ; if ( $ this -> useTimestamps && ! empty ( $ this -> createdField ) && ! array_key_exists ( $ this -> createdField , $ data ) ) { $ data [ $ this -> createdField ] = $ date ; } if ( $ this -> useTimestamps && ! empty ( $ this -> updatedField ) && ! array_key_exists ( $ this -> updatedField , $ data ) ) { $ data [ $ this -> updatedField ] = $ date ; } $ data = $ this -> trigger ( 'beforeInsert' , [ 'data' => $ data ] ) ; // Must use the set() method to ensure objects get converted to arrays $ result = $ this -> builder ( ) -> set ( $ data [ 'data' ] , '' , $ escape ) -> insert ( ) ; $ this -> trigger ( 'afterInsert' , [ 'data' => $ originalData , 'result' => $ result ] ) ; // If insertion failed, get our of here if ( ! $ result ) { return $ result ; } $ this -> insertID = $ this -> db -> insertID ( ) ; // otherwise return the insertID, if requested. return $ returnID ? $ this -> insertID : $ result ; }
Inserts data into the current table. If an object is provided,
it will attempt to convert it to an array.

@param array|object $data
@param boolean      $returnID Whether insert ID should be returned or not.

@return integer|string|boolean
@throws \ReflectionException
Inserts data into the current table . If an object is provided it will attempt to convert it to an array .