0

I'm a beginner and searched documentation, but can't find this how to do this:

I have two tables, admin and application. Admin can have many applications.

ADMIN:

class Model_Admin extends Model_Table {
    public $entity_code='admin';
    function init(){
        parent::init(); 

        $this->addField('name');
        $this->addField('email');
        $this->addField('password')->type('password');
        $this->addField('active')->type('boolean')->system(true);
        $this->addField('super')->type('boolean')->system(true);
        $this->addField('created')->type('timestamp')->defaultValue($this->dsql()->expr('now()'))->system(true);
        $this->addField('updated')->type('timestamp')->system(true);
        
        $this->hasMany('Application','admin_id');
        //$this->hasOne('Application');
        
        $this->addHook('beforeSave',function($m){
                 $m['updated']=$m->dsql()->expr('now()');
                });
    }
} 

APPLICATION:

class Model_Application extends Model_Table {
    public $entity_code='application';
    function init(){
        parent::init(); 

        $this->addField('name');
        $this->addField('fbid');
        $this->addField('fbsecret');
        $this->addField('active')->type('boolean')->system(true);
        $this->addField('created')->type('timestamp')->system(true);
        $this->addField('updated')->type('timestamp')->system(true);
    }
}

First question, when I generate SQL code (/generate.html) it doesn't produce anything for one to many relationship. Second, on a page I add CRUD:

$this->add('CRUD')->setModel('Admin');

But there is no hint for any one to many. I would expect it on the add button form, but also there is nothing?

What I want is, that I can add admin, and select which applications belong to it?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Peter
  • 728
  • 3
  • 16
  • 34

1 Answers1

2

in Model_Application

  $this->hasOne('Admin');

on Page

   $this->add('CRUD')->setModel('Application');

In Edit form of CRUD you will see dropdown with all admins and you'll be able to set admin to each application

Vadym
  • 749
  • 3
  • 15
  • Hi, thanks for your answer, but this is only for 1:1 solution. I created new question on M:M here: http://stackoverflow.com/questions/21045491/how-to-implement-crud-with-manymany-relationship – Peter Jan 10 '14 at 13:25
  • No, this is one to many connection, because one admin can have multiple applications. For many to many connection you need to use proxi table – Vadym Jan 10 '14 at 14:57