reference field on form

2019-05-25 07:48发布

I think I'm not understanding the reference field.

I have a simple form

<?php

class page_prueba extends Page {
  function init(){
    parent::init();
    $p=$this;
    $f=$p->add('Form');
    $f->setSource('ticket');
    $f->addField('line','texto')->validateNotNull();
    $f->addField('text','detalle')->validateNotNull();
    $c=$p->add('Model_Usuario');
    $f->addField('reference','usuario')->setValueList($c)->validateNotNull();
  }
}

And I have a User Model

<?php
  class Model_Usuario extends Model_Table {
    public $entity_code='usuario';
    public $table_alias='u';
    function defineFields(){
      parent::defineFields();           
      $this->addField('nombre');
      $this->addField('password');
      $this->addField('email');
      $this->addField('telefono');
      $this->addField('descripcion');
      $this->addField('interno');
      $this->addField('esadmin');
    }
  }
?>

When I run the example page I get on the dropdown (option values) displayed the id values (primary key) but what I want to see on that dropdowns is the name (nombre) field.

Maybe I'm missing something.

Any help would be appreciate.

thanks Alejandro

标签: php atk4
1条回答
We Are One
2楼-- · 2019-05-25 07:59

By default model displays "name" field. There are few ways to customize that

  1. define name field as calculated
  2. redefine Model_Usuario::toStringSQL()
  3. since 4.1 you can also define field name through a property.

you should probably go after 2, here is example:

public function toStringSQL($source_field, $dest_fieldname, $expr = 'name') {
    // return parent::tostringSQL($source_field,$dest_fieldname, 'date')

    return 'concat(name," ",surname) as ' . $dest_fieldname;
}
查看更多
登录 后发表回答