atk4\data\Model::get PHP Method

get() public method

If no field is passed, then returns array of all field values.
public get ( mixed $field = null ) : mixed
$field mixed
return mixed
    public function get($field = null)
    {
        if ($field === null) {
            // Collect list of eligible fields
            $data = [];
            if ($this->only_fields) {
                // collect data for actual fields
                foreach ($this->only_fields as $field) {
                    $data[$field] = $this->get($field);
                }
            } else {
                // get all field-elements
                foreach ($this->elements as $field => $f) {
                    if ($f instanceof Field) {
                        $data[$field] = $this->get($field);
                    }
                }
            }
            return $data;
        }
        $field = $this->normalizeFieldName($field);
        if (array_key_exists($field, $this->data)) {
            return $this->data[$field];
        }
        $f = $this->hasElement($field);
        return $f ? $f->default : null;
    }

Usage Example

Ejemplo n.º 1
0
 public function testJoinLoading()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'Joe', 'contact_id' => 2]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+123'], 2 => ['id' => 2, 'contact_phone' => '+321']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m_u = new Model($db, 'user');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $m_u->load(1);
     $this->assertEquals(['name' => 'John', 'contact_id' => 1, 'contact_phone' => '+123', 'id' => 1], $m_u->get());
     $m_u->load(3);
     $this->assertEquals(['name' => 'Joe', 'contact_id' => 2, 'contact_phone' => '+321', 'id' => 3], $m_u->get());
     $m_u->tryLoad(4);
     $this->assertEquals(['name' => null, 'contact_id' => null, 'contact_phone' => null, 'id' => null], $m_u->get());
 }
All Usage Examples Of atk4\data\Model::get