0


I have a question - how can I get and show data from referenced model in Lister?
referenced table structure (model User):

id | login | password | realName | email


reference logic (Model Post):

$this->hasOne('User', 'author');

template:

Author: <?realName?><?/?>

But is empty, what I doing wrong and how can I fix it?

Rakshazi
  • 75
  • 1
  • 10

1 Answers1

0

Simplest way to do this probably is to:

  • set realName field as title field (quite logical) in User model
  • and simply use that title field in Lister (should be automatically)

So, in User Model you'll have to add:

public $title_field = 'realName';

And in Lister probably $lister->setModel($user_model) should be enough.

Edit: If you want to add field from related model (let's say e-mail) in your grid, then you can use leftJoin in your Post model, like this (not tested):

class Model_Post extends SQL_Model{

function init(){
    parent::init();
    // ... define your fields

    // add left join to related TABLE (not model)
    $join_users = $this->leftJoin('user', 'author_id');

    // add field from joined table (not model)
    $join_users->addField('email'); // it's not $this->addField(), but $join->addField() !!!
}
}

This will create field email in your Post model and it'll be filled with value from related table.

DarkSide
  • 3,670
  • 1
  • 26
  • 34