Doctrine\MongoDB\Query\Expr::push PHP Method

push() public method

If the field does not exist, it will be set to an array containing the value(s) in the argument. If the field is not an array, the query will yield an error. Multiple values may be specified by providing an Expr object and using {@link Expr::each()}. {@link Expr::slice()} and {@link Expr::sort()} may also be used to limit and order array elements, respectively.
See also: Builder::push()
See also: http://docs.mongodb.org/manual/reference/operator/push/
See also: http://docs.mongodb.org/manual/reference/operator/each/
See also: http://docs.mongodb.org/manual/reference/operator/slice/
See also: http://docs.mongodb.org/manual/reference/operator/sort/
public push ( mixed | Expr $valueOrExpression )
$valueOrExpression mixed | Expr
    public function push($valueOrExpression)
    {
        if ($valueOrExpression instanceof Expr) {
            $valueOrExpression = array_merge(['$each' => []], $valueOrExpression->getQuery());
        }
        $this->requiresCurrentField();
        $this->newObj['$push'][$this->currentField] = $valueOrExpression;
        return $this;
    }

Usage Example

示例#1
0
 /**
  * Append one or more values to the current array field.
  *
  * If the field does not exist, it will be set to an array containing the
  * value(s) in the argument. If the field is not an array, the query
  * will yield an error.
  *
  * Multiple values may be specified by providing an Expr object and using
  * {@link Expr::each()}. {@link Expr::slice()} and {@link Expr::sort()} may
  * also be used to limit and order array elements, respectively.
  *
  * @see Expr::push()
  * @see http://docs.mongodb.org/manual/reference/operator/push/
  * @see http://docs.mongodb.org/manual/reference/operator/each/
  * @see http://docs.mongodb.org/manual/reference/operator/slice/
  * @see http://docs.mongodb.org/manual/reference/operator/sort/
  * @param mixed|Expr $valueOrExpression
  * @return self
  */
 public function push($valueOrExpression)
 {
     $this->expr->push($valueOrExpression);
     return $this;
 }