atk4\data\Model::addFields PHP Method

addFields() public method

Adds multiple fields into model.
public addFields ( array $fields = [], array $defaults = [] )
$fields array
$defaults array
    public function addFields($fields = [], $defaults = [])
    {
        foreach ($fields as $field) {
            if (is_string($field)) {
                $this->addField($field, $defaults);
                continue;
            }
            if (is_array($field) && isset($field[0])) {
                $name = $field[0];
                unset($field[0]);
                $this->addField($name, $field);
                continue;
            }
        }
        return $this;
    }

Usage Example

Beispiel #1
0
 public function testExpressionJoin()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2], 3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+123 smiths'], 2 => ['id' => 2, 'contact_phone' => '+321 sues']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'user');
     $m->addFields(['name', 'gender', 'surname']);
     $m->join('contact')->addField('contact_phone');
     $m->tryLoad(1);
     $this->assertEquals('John', $m['name']);
     $this->assertEquals('+123 smiths', $m['contact_phone']);
     $m->tryLoad(2);
     $this->assertEquals('Sue', $m['name']);
     $this->assertEquals('+321 sues', $m['contact_phone']);
     $m->tryLoad(3);
     $this->assertEquals('Peter', $m['name']);
     $this->assertEquals('+123 smiths', $m['contact_phone']);
     $mm = clone $m;
     $mm->addCondition($mm->expr('[name] = [surname]'));
     $mm->tryLoad(1);
     $this->assertEquals(false, $mm->loaded());
     $mm->tryLoad(2);
     $this->assertEquals('Sue', $mm['name']);
     $this->assertEquals('+321 sues', $mm['contact_phone']);
     $mm->tryLoad(3);
     $this->assertEquals(false, $mm->loaded());
     $mm = clone $m;
     $mm->addCondition($mm->expr('"+123 smiths" = [contact_phone]'));
     $mm->tryLoad(1);
     $this->assertEquals('John', $mm['name']);
     $this->assertEquals('+123 smiths', $mm['contact_phone']);
     $mm->tryLoad(2);
     $this->assertEquals(null, $mm['name']);
     $this->assertEquals(null, $mm['contact_phone']);
     $mm->tryLoad(3);
     $this->assertEquals('Peter', $mm['name']);
     $this->assertEquals('+123 smiths', $mm['contact_phone']);
 }
All Usage Examples Of atk4\data\Model::addFields