Sokil\Mongo\Structure::append PHP Method

append() public method

If field exists and is not array - convert to array and append If field -s array - append
public append ( type $selector, type $value ) : Structure
$selector type
$value type
return Structure
    public function append($selector, $value)
    {
        $oldValue = $this->get($selector);
        if ($oldValue) {
            if (!is_array($oldValue)) {
                $oldValue = (array) $oldValue;
            }
            $oldValue[] = $value;
            $value = $oldValue;
        }
        $this->set($selector, $value);
        return $this;
    }

Usage Example

示例#1
0
 /**
  * If field not exist - set value.
  * If field exists and is not array - convert to array and append
  * If field -s array - append
  *
  * @param type $selector
  * @param type $value
  * @return \Sokil\Mongo\Structure
  */
 public function append($selector, $value)
 {
     parent::append($selector, $value);
     // if document saved - save through update
     if ($this->getId()) {
         $this->operator->set($selector, $this->get($selector));
     }
     return $this;
 }