Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:37:09+00:00 2026-06-12T19:37:09+00:00

I’ve been working on the Student enrollment example/tutorial for ATK and am stuck on

  • 0

I’ve been working on the Student enrollment example/tutorial for ATK and am stuck on the use of ref()’s via linking tables.

A basic ER of the system is below:

[Student] ||---|< [class_has_student] >|---|| [class]  >|----|| [subject]

Where a Subject stores the subjects name, subject code, which semester(s) it can run in and a description.
A class is the lecture/tutorial that is offered to student’s for a given Subject over a given period. A student can enrol in a Class and the relation is stored in the class_has_student table.

For the basic example I wish to show a particular students details and show the classes (with subject name) they are enrolled in. I’m using a view template to display the student details (name, etc)
and any classes (with subject name) they are enrolled in using the Lister class. The main problem is when trying to list the subjects a particular student is enrolled in using:

$student_detail= $student->ref('ClassHasStudent2')->ref('class_idclass')

Rather than retrieving all subjects the student is enrolled in, its retrieving all subjects from the DB.

Please see below for the Page, Models and template code for this example, and the modified Table.php class to get it to work (sorry for the length).

Thanks for your help.

Models

“class” table

+-------------------+---------+------+-----+---------+----------------+
| Field             | Type    | Null | Key | Default | Extra          |
+-------------------+---------+------+-----+---------+----------------+
| idclass           | int(11) | NO   | PRI | NULL    | auto_increment |
| subject_idsubject | int(11) | NO   | MUL | NULL    |                |
| date_start        | date    | YES  |     | NULL    |                |
| date_end          | date    | YES  |     | NULL    |                |
| max_students      | int(11) | YES  |     | NULL    |                |
+-------------------+---------+------+-----+---------+----------------+

select * from class;
+---------+-------------------+------------+------------+--------------+
| idclass | subject_idsubject | date_start | date_end   | max_students |
+---------+-------------------+------------+------------+--------------+
|       1 |                 2 | 2012-09-10 | 2012-09-30 |           10 |
|       2 |                 1 | 2012-09-08 | 2012-09-30 |           30 |
|       3 |                 3 | 2012-10-01 | 2012-10-31 |           35 |
|       4 |                 2 | 2012-09-19 | 2012-09-29 |           10 |
+---------+-------------------+------------+------------+--------------+

class.php

<?php

class Model_Class extends Model_Table {
    public $table='class';
    public $entity_code='class';
public $id_field='idclass';

    function init(){
        parent::init();
    $this->hasOne('Subject','subject_idsubject','name', 'subject_name');
    $this->addField('date_start')->type('date')->caption('Start');
    $this->addField('date_end')->type('date')->caption('End');
    $this->addField('max_students')->type('int');

    $this->hasMany('ClassHasStudent','class_idclass', 'idclass');
    }
}

“student” table

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| idstudent  | int(11)     | NO   | PRI | NULL    | auto_increment |
| student_id | varchar(45) | NO   |     | NULL    |                |
| name       | varchar(45) | NO   |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

select * from student;
+-----------+------------+--------+
| idstudent | student_id | name   |
+-----------+------------+--------+
|         1 | T123       | Tom    |
|         2 | L222       | Lizley |
|         3 | B123       | Betty  |
|         4 | H33        | Homer  |
|         5 | N42        | Nick   |
+-----------+------------+--------+

student.php

  <?php
  class Model_Student extends Model_Table {
      public $table='student';
      public $entity_code='student';
      public $id_field='idstudent';
      public $title_field='name';

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

          $this->addField('student_id')->caption('Student ID');
          $this->addField('name')->caption('Name');
          $this->hasMany('ClassHasStudent', 'student_idstudent', 'idstudent');
      }
  }

Mysql “class_has_student” table

  +---------------------+-------------+------+-----+---------+----------------+
  | Field               | Type        | Null | Key | Default | Extra          |
  +---------------------+-------------+------+-----+---------+----------------+
  | idclass_has_student | int(11)     | NO   | PRI | NULL    | auto_increment |
  | class_idclass       | int(11)     | NO   | MUL | NULL    |                |
  | student_idstudent   | int(11)     | NO   | MUL | NULL    |                |
  | date_enrolled       | varchar(45) | YES  |     | NULL    |                |
  | grade               | double      | YES  |     | NULL    |                |
  +---------------------+-------------+------+-----+---------+----------------+

  select * from class_has_student;
  +---------------------+---------------+-------------------+---------------+-------+
  | idclass_has_student | class_idclass | student_idstudent | date_enrolled | grade |
  +---------------------+---------------+-------------------+---------------+-------+
  |                   1 |             1 |                 1 | 2012-09-04    |     0 |
  |                   2 |             1 |                 2 | 2012-09-11    |     0 |
  |                   3 |             2 |                 1 | 2012-09-14    |     0 |
  +---------------------+---------------+-------------------+---------------+-------+

classhasstudent.php

  <?php
  class Model_ClassHasStudent extends Model_Table {
      public $table='class_has_student';
      public $entity_code='class_has_student';
      public $id_field='idclass_has_student';

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

      $this->hasOne('Class','class_idclass','idclass', 'class_name');
          $this->hasOne('Student', 'student_idstudent', 'name', 'student_name');
      $this->addField('date_enrolled')->type('date');
      $this->addField('grade');

      }

  }

mysql “subject” table

  +--------------+-------------+------+-----+---------+----------------+
  | Field        | Type        | Null | Key | Default | Extra          |
  +--------------+-------------+------+-----+---------+----------------+
  | idsubject    | int(11)     | NO   | PRI | NULL    | auto_increment |
  | name         | varchar(45) | YES  |     | NULL    |                |
  | subject_code | varchar(45) | YES  |     | NULL    |                |
  | semester     | varchar(45) | YES  |     | NULL    |                |
  | description  | blob        | YES  |     | NULL    |                |
  +--------------+-------------+------+-----+---------+----------------+

  select * from subject;
  +-----------+-----------+--------------+----------+--------------------------+
  | idsubject | name      | subject_code | semester | description              |
  +-----------+-----------+--------------+----------+--------------------------+
  |         1 | Maths     | mat101       | 1        | Mathematics and numbers. |
  |         2 | English   | enh101       | 1        | English and languages.   |
  |         3 | Chemistry | chm101       | 2        | Inorganic chemistry.     |
  +-----------+-----------+--------------+----------+--------------------------+

subject.php

  <?php

  class Model_Subject extends Model_Table {
      public $entity_code='subject';
  public $id_field='idsubject';


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

      $this->addField('name');
      $this->addField('subject_code');
      $this->addField('semester');
      $this->addField('description');

      }

  }

Lister

  <?php
  // Lister class for displaying classes that a student attends.
  class StudentClasses extends Lister {

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

      // Override defaultTemplate function.
      function defaultTemplate(){
          return array('view/class_info');
      }
  }

Templates

/templates/default/view/student_details.html

  <div id="<?$_name?>" class="projectinfo">

      <table class="student">
      <tr><td id="property">Student Name</td><td id="value"> <?$name?> </td></tr>
      <tr><td id="property">Student ID</td> <td id="value"><?$student_id?></td></tr>
      </table>


  </div>

  <h2>Enrolled classes</h2>
  <?ClassList?>
  <?/?>

/templates/default/view/class_info.html

  <dl id="<?$_name?>" class="<?$class?>">
<dt><?$subject_name?></dt>
<dd><?$date_start?></dd><dd><?$date_start?></dd>
  </dl>

Pages

<?php

class page_TestClasses extends Page {
    function init() {
        parent::init();

        $this->api->auth->check();
        $this->api->stickyGET('id');

    }

    // Called only once.
    function initMainPage(){


        $view=$this->add('View',null,null,array('view/student_details'));

//      $student = $this->add('Model_Student')->loadAny();


        $student = $this->add('Model_Student')->loadAny();

//      $student_detail= $student->ref('ClassHasStudent')->debug();
//      select `idclass_has_student`,`class_idclass`,(select `idclass` from `class` where `class_has_student`.`class_idclass` = `class`.`idclass` ) `class_idclass_2`,`student_idstudent`,(select `name` from `student` where `class_has_student`.`student_idstudent` = `student`.`idstudent` ) `student_name`,`date_enrolled`,`grade` from `class_has_student` where `class_has_student`.`student_idstudent` = "1"
+---------------------+---------------+-----------------+-------------------+--------------+---------------+-------+
| idclass_has_student | class_idclass | class_idclass_2 | student_idstudent | student_name | date_enrolled | grade |
+---------------------+---------------+-----------------+-------------------+--------------+---------------+-------+
|                   1 |             1 |               1 |                 1 | Tom          | 2012-09-04    |     0 |
|                   3 |             2 |               2 |                 1 | Tom          | 2012-09-14    |     0 |
+---------------------+---------------+-----------------+-------------------+--------------+---------------+-------+

        // USING $this->hasOne('Class','class_idclass','idclass', 'class_name'); in ClassHasStudent
//      select `idclass_has_student`,`class_idclass`,(select `idclass` from `class` where `class_has_student`.`class_idclass` = `class`.`idclass` ) `class_name`,`student_idstudent`,(select `name` from `student` where `class_has_student`.`student_idstudent` = `student`.`idstudent` ) `student_name`,`date_enrolled`,`grade` from `class_has_student` where `class_has_student`.`student_idstudent` = "1";
+---------------------+---------------+------------+-------------------+--------------+---------------+-------+
| idclass_has_student | class_idclass | class_name | student_idstudent | student_name | date_enrolled | grade |
+---------------------+---------------+------------+-------------------+--------------+---------------+-------+
|                   1 |             1 |          1 |                 1 | Tom          | 2012-09-04    |     0 |
|                   3 |             2 |          2 |                 1 | Tom          | 2012-09-14    |     0 |
+---------------------+---------------+------------+-------------------+--------------+---------------+-------+

        $student_detail= $student->ref('ClassHasStudent')->ref('class_idclass')->debug();
//      select `idclass`,`subject_idsubject`,(select `name` from `subject` where `class`.`subject_idsubject` = `subject`.`idsubject` ) `subject_idsubject_2`,`date_start`,`date_end`,`max_students` from `class`
+---------+-------------------+---------------------+------------+------------+--------------+
| idclass | subject_idsubject | subject_idsubject_2 | date_start | date_end   | max_students |
+---------+-------------------+---------------------+------------+------------+--------------+
|       1 |                 2 | English             | 2012-09-10 | 2012-09-30 |           10 |
|       2 |                 1 | Maths               | 2012-09-08 | 2012-09-30 |           30 |
|       3 |                 3 | Chemistry           | 2012-10-01 | 2012-10-31 |           35 |
|       4 |                 2 | English             | 2012-09-19 | 2012-09-29 |           10 |
+---------+-------------------+---------------------+------------+------------+--------------+


        // Add Lister object to view, using LibraryList template spot.
        $ClassList = $view->add('StudentClasses', null, 'ClassList');

        $view->setModel( $student );
        $ClassList->setModel( $student_detail );

    }
}

I also modified Table::hasOne() to the following, where hasOne can set the dereferenced_field variable.
/atk4/lib/Model/Table.php

/** Defines one to many association */
function hasOne($model,$our_field=null,$display_field=null,$deref_field_name=null){
    if(!$our_field){
        if(!is_object($model)){
            $tmp=preg_replace('|^(.*/)?(.*)$|','\1Model_\2',$model);
            $tmp=new $tmp; // avoid recursion
        }else $tmp=$model;
        $our_field=($tmp->table).'_id';
    }
    $r=$this->add('Field_Reference',$our_field);
    $r->dereferenced_field=$deref_field_name;
    $r->setModel($model,$display_field);
    return $r;
}

Update 1

Changing the above example to the design specified in the video produces the code below:

student.php

  <?php
  class Model_Student extends Model_Table {
      public $table='student';
      public $entity_code='student';
      public $id_field='idstudent';
      public $title_field='name';

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

          $this->addField('student_id')->caption('Student ID');
          $this->addField('name')->caption('Name');
          $this->hasMany('ClassJoinClassHasStudent');
      }
  }

ClassJoinClassHasStudent.php

class Model_ClassJoinClassHasStudent extends Model_Class {

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

        $chs = $this->join('class_has_student.class_idclass','idclass');
        // Import fields from ClassHasStudent
        $chs->addField('date_enrolled');
        $chs->addField('grade');

        $chs->hasOne('Class'); // use id_class from CHS table.
    }
}

However, I get the following error related to setting the master field in ClassJoinClassHasStudent. I’ve tried calling hasMany(‘ClassJoinClassHasStudent’, ‘student_idstudent’, ‘idstudent’), but the error is the same except it complains about student_idstudent instead of student_id.

BaseException

Child element not found

Additional information:

    Raised by object: Object Model_ClassJoinClassHasStudent(studentenrol_testclasses_model_student_ClassJoinClassHasStudent_model_classjoinclasshasstudent)
    element: student_id

Possible Actions:

    Debug this Model

/home/user1/public_html/studentenrol/atk4/lib/BaseException.php:38
Stack trace:
/home/user1/public_html/studentenrol/atk4/lib/BaseException.php :38     BaseException   BaseException->collectBasicData(Null, 1, 0)
/home/user1/public_html/studentenrol/atk4/lib/AbstractObject.php    :292    BaseException   BaseException->__construct("Child element not found")
/   :   studentenrol_testclasses_model_student_ClassJoinClassHasStudent_model_classjoinclasshasstudent  Model_ClassJoinClassHasStudent->exception("Child element not found")
/home/user1/public_html/studentenrol/atk4/lib/Model/Table.php   :87         Loggercall_user_func_array(Array(2), Array(1))
/home/user1/public_html/studentenrol/atk4/lib/AbstractObject.php    :202    studentenrol_testclasses_model_student_ClassJoinClassHasStudent_model_classjoinclasshasstudent  Model_ClassJoinClassHasStudent->exception("Child element not found")
/home/user1/public_html/studentenrol/atk4/lib/Model/Table.php   :259    studentenrol_testclasses_model_student_ClassJoinClassHasStudent_model_classjoinclasshasstudent  Model_ClassJoinClassHasStudent->getElement("student_id")
/home/user1/public_html/studentenrol/atk4/lib/Model/Table.php   :321    studentenrol_testclasses_model_student_ClassJoinClassHasStudent_model_classjoinclasshasstudent  Model_ClassJoinClassHasStudent->addCondition("student_id", "1")
/home/user1/public_html/studentenrol/atk4/lib/SQL/Many.php  :48     studentenrol_testclasses_model_student_ClassJoinClassHasStudent_model_classjoinclasshasstudent  Model_ClassJoinClassHasStudent->setMasterField("student_id", "1")
/home/user1/public_html/studentenrol/atk4/lib/Model/Table.php   :243    studentenrol_testclasses_model_student_ClassJoinClassHasStudent SQL_Many->ref(Null)
/home/user1/public_html/studentenrol/page/testclasses.php   :93     studentenrol_testclasses_model_student  Model_Student->ref("ClassJoinClassHasStudent")
/home/user1/public_html/studentenrol/atk4/lib/ApiFrontend.php   :93     studentenrol_testclasses    page_TestClasses->initMainPage()
/home/user1/public_html/studentenrol/atk4/lib/ApiWeb.php    :332    studentenrol    Frontend->layout_Content()
/home/user1/public_html/studentenrol/atk4/lib/ApiFrontend.php   :33     studentenrol    Frontend->addLayout("Content")
/home/user1/public_html/studentenrol/atk4/lib/ApiWeb.php    :208    studentenrol    Frontend->initLayout()
/home/user1/public_html/studentenrol/index.php  :24     studentenrol    Frontend->main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:37:11+00:00Added an answer on June 12, 2026 at 7:37 pm

    I have recreated your example and changed the code like this:

    $student = $this->add('Model_Student')->loadAny();
    $student_detail= $this->add('Model_Class');
    $student_detail->join('class_has_student.class_idclass','idclass')->addField('student_idstudent');
    $student_detail->addCondition('student_idstudent',$student->id);
    $ClassList = $view->add('StudentClasses', null, 'ClassList');
    
    $view->setModel( $student );
    $ClassList->setModel( $student_detail->debug() );
    

    Basically you must start with the model you are willing to display. Because you want to list classes, you should go for Model_Class. Then you join the model with the many-to-many table and define the field from the other end (student_idstudent). Next you can use it in condition.

    enter image description here

    I also had to patch Model_Table to allow 4th argument for hasOne() to define dereferenced fields, so please update to the latest ATK4.

    https://github.com/atk4/atk4-testsuite/commit/ccaef2c5e5841bb265001f00d832c3583a4cf722

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm parsing an XML file, the creators of it stuck in a bunch social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.