fields() public method

Usage: to add a field $query->fields('created'); to add several fields $query->fields(array('title','body','modified')); to reset fields to none $query->fields(false); should be followed by a 2nd call to fields with required fields
public fields ( mixed $fields = null, boolean $overwrite = false ) : array
$fields mixed string, array or `false`
$overwrite boolean If `true`, existing fields will be removed before adding `$fields`.
return array Returns an array containing all fields added to the query.
Example #1
0
	public function testFields() {
		$query = new Query($this->_queryArr);

		$expected = array('id','author_id','title');
		$result = $query->fields();
		$this->assertEqual($expected, $result);

		$query->fields('content');

		$expected = array('id','author_id','title','content');
		$result = $query->fields();
		$this->assertEqual($expected, $result);

		$query->fields(array('updated','created'));

		$expected = array('id','author_id','title','content','updated','created');
		$result = $query->fields();
		$this->assertEqual($expected, $result);

		$query->fields(false);
		$query->fields(array('id', 'title'));

		$expected = array('id','title');
		$result = $query->fields();
		$this->assertEqual($expected, $result);
	}
Example #2
0
 public function testFieldsWithArray()
 {
     $query = new Query(array('model' => 'lithium\\tests\\mocks\\data\\model\\MockQueryPost', 'type' => 'read', 'with' => 'MockQueryComment'));
     $query->fields(array('MockQueryPost', 'MockQueryPost' => array('id')));
     $result = $query->fields();
     $expected = array('MockQueryPost', 'MockQueryPost.id');
     $this->assertEqual($expected, $result);
     $query->fields(false);
     $query->fields(array('MockQueryPost' => array('id'), 'title', 'MockQueryComment' => array('comment', 'title'), 'MockQueryComment'));
     $result = $query->fields();
     $expected = array('MockQueryPost.id', 'title', 'MockQueryComment.comment', 'MockQueryComment.title', 'MockQueryComment');
     $this->assertEqual($expected, $result);
 }