0

Bear with me please, I'm still learning.

I have 4 Models as so:

class Model_Users extends Model_Table {
    public $table="users";
    function init(){
        parent::init();
        $this->addField('name')->mandatory('Enter Name');
        $this->addField('email')->mandatory('Enter E-Mail');
        $this->addField('phone')->mandatory('Enter Phone');
        $this->addField('password')->type('password')->mandatory('Enter Password');
        $this->addField('is_superadmin')->type('boolean');
        $this->addField('is_employee')->type('boolean');
        $this->addField('is_manager')->type('boolean');

        $this->hasMany('companies');
    }


}

class Model_areas extends Model_Table {
    public $entity_code='areas';
    function init(){
        parent::init();
        $this->addField('name');
        $this->addField('description')->type('text');
        //$this->addField('companies_id')->refModel('Model_companies');

        $this->hasOne('companies','companies_id','name')->mandatory(true);
        $this->hasMany('sites');
    }
}

class Model_areas extends Model_Table {
    public $entity_code='areas';
    function init(){
        parent::init();
        $this->addField('name');
        $this->addField('description')->type('text');
        //$this->addField('companies_id')->refModel('Model_companies');

        $this->hasOne('companies','companies_id','name')->mandatory(true);
        $this->hasMany('sites');
    }
}

class Model_sites extends Model_Table {
    public $entity_code='sites';
    function init(){
        parent::init();
        $this->addField('name');
        $this->addField('description')->type('text');
        $this->addField('qrcode');
        //$this->addField('Company Name','areas_id')->refModel('Model_companies','name');

        $this->hasOne('areas','areas_id','name');
    }
}

I have the "sites" model in a simple crud. It is successfully pulling the relevant hasOne record from "areas". I have two questions:

1) How do I change the column names for the joined areas column? It just says "Areas", whereas I want it to be "Area Name"

2) And the more complex one: How can I perform something like a grid->addColumn to the resulting CRUD (or would it have to be a grid?) that would pull the company name linked to the area in areas_id? Its all 1 to many relationships down the line. Company has multiple areas. Areas has multiple sites. I want to add the company name to the CRUD view of Sites.

You can see in the commented lines some of my minor attempts at accomplishing this. Then I realized I'm missing something big. I should be able to keep this model simple and simply traverse the relationships..

Thank you for your help.

Back to these tutorial videos.

Edit: OK the column name I figured out. ->caption('Blah'). Still can't figure out the traversal :(

Dariusz
  • 21,561
  • 9
  • 74
  • 114
Sputnikk23
  • 63
  • 9
  • Please fix your source code in this question. It now doesn't have Model_companies at all and remove unneeded commented lines (debug) and php tags. All of that just makes this question harder to overview and understand. – DarkSide Jul 22 '13 at 10:13
  • I'll keep that in mind. Sorry, first post on overflow. Thanks to whomever edited it. Is Darius another one of your accounts? – Sputnikk23 Jul 22 '13 at 14:34

1 Answers1

0

1) Try:

$this->hasOne('areas','areas_id','name')->caption('Area Name');

2) Slightly simplified your models and here it is. I admit - I didn't test this, but it should work:

<?php
class Model_Company extends Model_Table {
    public $table = 'company';
    function init(){
        parent::init();
        $this->addField('name');
        $this->addField('description');
        $this->hasMany('Area');
    }
}

class Model_Area extends Model_Table {
    public $table = 'area';
    function init(){
        parent::init();
        $this->addField('name');
        $this->addField('description');
        $this->hasOne('Company', 'company_id', 'name');
        $this->hasMany('Site');
    }
}

class Model_Site extends Model_Table {
    public $table = 'site';
    function init(){
        parent::init();
        $this->addField('name');
        $this->addField('description');
        $this->hasOne('Area', 'area_id', 'name');

        // join area and company tables
        $j_area = $this->leftJoin('area', 'area_id');
        $j_company = $j_area->leftJoin('company', 'company_id');

        // add fields from joined tables to this model
        $j_area->addField('area_name', 'name');
        $j_company->addField('company_name', 'name');
    }
}

Basic idea - use joins. ->leftJoin will not create joined records, but ->join will create them. Just for reporting (grid, crud etc.) you're fine with leftJoin.

P.S. Define some kind of coding rules for yourself. For example, when you use uppercase, lowercase, camel-case etc for class names, filenames. Otherwise you'll sooner or later run into problems when moving on *NIX systems. Good example is to name all classnames with first letter in upper case. Don't forget that your respective file names should exactly match your classname - also letter case. Page classnames I like to name all in lowercase, because their name is used in URLs and then it looks better lowercased. But that's just me :)

One more. If you're working on new project or are just learning ATK, then stick to newest development version (available on github). There are quite many things which have changed since recording of tutorials. For example $entity_code is deprecated - use $table instead. refModel is obsolete too I guess etc. If you want to be successful with ATK, then you have to look regularly into ATK source code to understand it better. Also there are some useful comments :)

Good luck!

DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • Thank you DarkSide. I'll mess with this when I get home. I see next to Roman himself, you are very active with ATK. Expect lots of questions. :) – Sputnikk23 Jul 22 '13 at 14:25