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 6858957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:28+00:00 2026-05-27T02:13:28+00:00

I have a database with 3 tables. It’s a simple n-m relationship. Student, Course

  • 0

I have a database with 3 tables. It’s a simple n-m relationship. Student, Course and StudentHasCourse to handle n-m relationship. I post the schema.yml for reference, but it would not be really necessary.

Course:
  connection: doctrine
  tableName: course
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    name:
      type: string(45)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
  relations:
    StudentHasCourse:
      local: id
      foreign: course_id
      type: many

Student:
  connection: doctrine
  tableName: student
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    registration_details:
      type: string(45)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    name:
      type: string(30)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
  relations:
    StudentHasCourse:
      local: id
      foreign: student_id
      type: many

StudentHasCourse:
  connection: doctrine
  tableName: student_has_course
  columns:
    student_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    course_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: false
    result:
      type: string(1)
      fixed: true
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
  relations:
    Course:
      local: course_id
      foreign: id
      type: one
    Student:
      local: student_id
      foreign: id
      type: one

Then, I get data from tables in executeIndex() from the following query.

  $q_info = Doctrine_Query::create()
   ->select('s.*, shc.*, c.*')
   ->from('Student s')
   ->leftJoin('s.StudentHasCourse shc')
   ->leftJoin('shc.Course c')
   ->where('c.id = 1');
  $this->infos = $q_info->execute();

Then I access data by looping through in indexSuccess.php. But, in indexSuccess I can only access data from the table Student.

<?php foreach ($infos as $info): ?>
  <?php echo $info->getId(); ?>
  <?php echo $info->getName(); ?>
<?php endforeach; ?>

I expected, that I could access StudentHasCourse data and Course data like the following.
But, it generates an error.

<?php echo $info->getStudentHasCourse()->getResult()?>
<?php echo $info->getStudentHasCourse()->getCourse()->getName()?>

The first statement gives a warning;

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class ‘Doctrine_Collection’ does not have a method ‘getCourse’ in D:\wamp\bin\php\php5.3.5\PEAR\pear\symfony\escaper\sfOutputEscaperObjectDecorator.class.php on line 64

And the second statement gives the above warning and the following error;

Fatal error: Call to a member function getName() on a non-object in D:\wamp\www\sam\test_doc_1\apps\frontend\modules\registration\templates\indexSuccess.php on line 5

When I check the query from the Debug toolbar it appears as following and it gives all data I want.

SELECT s.id AS s__id, s.registration_details AS s__registration_details, s.name AS s__name, s2.student_id AS s2__student_id, s2.course_id AS s2__course_id, s2.result AS s2__result, c.id AS c__id, c.name AS c__name 
FROM student s LEFT JOIN student_has_course s2 ON s.id = s2.student_id LEFT JOIN course c ON s2.course_id = c.id 
WHERE (c.id = 1)

Though the question is short, as all the information mentioned it became so long. It’s highly appreciated if someone can help me out to solve this. What I require is to access the data from StudentHasCourse and Course. If those data cannot be accessed by this design and this query, any other methodology is also appreciated.

  • 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-05-27T02:13:28+00:00Added an answer on May 27, 2026 at 2:13 am

    The problem lies in the fact that you didn’t define an n-m relationship, but two 1-n relationships. This is quite equivalent from a database point of view, but from the application point of view, you are getting 4 methods that do not have much semantic value, when you could get two easy-to-use methods : Student::getCourses() and Course::getStudents()

    Read this § of the doctrine1.2 documentation to learn how to achieve this. StudentHasCourse should be used as your refClass

    UPDATE
    I just understood you were storing the result in StudentHasCourse
    To get what you want, try something like this in your controller:

    $this->courses = CourseTable::getInstance()
      ->createQuery('c')
        ->innerJoin('c.StudentHasCourse shc')
            ->where('shc.student_id = ?', $student_id)
            ->execute();
    

    This means you still need the StudentHasCourse relation in the Course class. Recover it if you deleted it.

    Now you should be able to do something like this in your template

    <ul>
    <?php foreach($courses as $course): ?>
      <li><?php echo $course //implement __toString in Course ?> : <?php echo $course->getStudentHasCourse()->getFirst()->getResult(); ?></li>
    <?php endforeach;?>
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two database tables that have a many to many relationship: People and
I have two database tables with a one-to-many relationship. Is it possible to select
I have designed database tables (normalised, on an MS SQL server) and created a
I have two database tables with the following structure: actions: action_id int(11) primary key
I have 3 database tables, all of them have the same 5 columns. They
I have several database tables that just contain a single column and very few
I have three database tables: users emails invitations Emails are linked to users by
I have database with many tables. In the first table, I have a field
I have a database two tables and a linking table that I need a
I have a database with two tables ( Table1 and Table2 ). They both

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.