Many to many model

35 views
Skip to first unread message

Daniel Tosello

unread,
Oct 9, 2014, 3:18:49 AM10/9/14
to agile-too...@googlegroups.com
Hey all,

I'm trying to get a many to many model working for a simple CRM style system using atk4.3. So far i have the following:

class Model_Project extends Model_Table {

       
public $table = 'project';
       
function init(){
                parent
::init();

                $this
->addField('Title');
                $this
->addField('School');

                $this
->hasMany('mProjectContacts');
                $this
->hasMany('mProjectFunds');
                $this
->hasMany('Notes');
}


Class Model_Contact extends Model_Table {

       
public $table = 'contact';

       
function init() {
                parent
::init();

                $this
->addField('name');
                $this
->addField('title');
                $this
->addField('email');
                $this
->addField('phone');

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

}

Class Model_Fund extends Model_Table {

       
public $table = 'fund';

       
function init() {
                parent
::init();

                $this
->addField('funder');
                $this
->addField('fund_type');
                $this
->addField('duration');

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

}

//Linked by the following:

class Model_mProjectContacts extends Model_Table {

       
public $table = 'm_project_contacts';

       
function init(){
                parent
::init();

                $j
= $this->join('contact');
                $j
->hasOne('Project');
       
}
}

class Model_mProjectFunds extends Model_Table {

       
public $table='m_project_funds';

       
function init(){
                parent
::init();

                $j
= $this->join('Project');
                $j
->hasOne('Fund');
       
}
}


...Which all seems like it should work based on the docs i could find and my limited reading of the code... however i get weird errors from it. e.g:

Application Error: Database Query Failed


Exception_DB, code: 0

Additional information:

  • pdo_error: SQLSTATE[42S22]: Column not found: 1247 Reference 'project_id' not supported (forward reference in item list)
  • mode: select
  • params:
    • :a: Record #
    • :a_2: 1
  • query:
     
    select  `m_project_contacts`.`contact_id`,(select  concat(:a,
    `project`.`id`) from `project`  where `_c`.`project_id` = `project`.`id`
       
    ) `project`,`m_project_contacts`.`id`,`_c`.`project_id`,`_c`.`id`
    `_c` from `m_project_contacts` inner join `contact` as `_c` on `_c`.`id`
     
    = `m_project_contacts`.`id` where `_c`.`project_id` = :a_2    

I have specified the $their_field that should be used ('_c'.'id', as opposed to '_c'.'contact_id', via Model_mProjectContacts if i'm understanding it correctly) but it appears to be ignored. Reading the code hasn't revealed anything obvious to me, but i was rushed and don't have enough time to look at it properly at the moment. Has anyone got any idea what is going on here?

Thanks all,
--Daniel
Message has been deleted

Daniel Tosello

unread,
Oct 9, 2014, 3:23:05 AM10/9/14
to agile-too...@googlegroups.com

Oops! I meant 'project_id' rather than 'contact_id'... that was probably pretty obvious.

Thanks again,
--Daniel

Daniel Tosello

unread,
Oct 9, 2014, 9:42:26 PM10/9/14
to agile-too...@googlegroups.com
Hey all,

I resolved this by changing my M:M link tables to use hasOne in both directions.

If there's a problem with ATK4 it's that there's too much incomplete documentation lying around the web... Thanks for all your work, Romans et al.

--Daniel

Romans Malinovskis

unread,
Oct 10, 2014, 10:30:05 AM10/10/14
to agile-too...@googlegroups.com
Hi Daniel

All the documentation is at http://book.agiletoolkit.org/ - If you find that anything is missing, please let me know. The rest of documentation in other places needs to be removed.

Romans

Daniel Tosello

unread,
Oct 13, 2014, 11:07:33 PM10/13/14
to agile-too...@googlegroups.com
Thanks Romans :)

--Daniel
Reply all
Reply to author
Forward
0 new messages