Invalid Argument Supplied For Foreach()

hello im new here… and it is my first post!

I’m trying to build an information system.

I want to show grid which draws data from several different tables.

I tried it with lazy loding

This is part of my database:


CREATE TABLE IF NOT EXISTS `tbl_activity` (

  `activity_code` int(11) NOT NULL AUTO_INCREMENT,

  `timetable_code` int(11) NOT NULL,

  `activity_name` varchar(30) NOT NULL,

  `date_start` date NOT NULL,

  `date_end` date DEFAULT NULL,

  `activity_type` varchar(11) NOT NULL,

  `year_of_study` varchar(30) NOT NULL,

  `notes` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`activity_code`),

  KEY `timetable_code` (`timetable_code`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;


--

-- Dumping data for table `tbl_activity`

--


INSERT INTO `tbl_activity` (`activity_code`, `timetable_code`, `activity_name`, `date_start`, `date_end`, `activity_type`, `year_of_study`, `notes`) VALUES

(1, 1, 'math', '2018-12-14', '0000-00-00', 'reg', '2014', ''),

(2, 2, 'english', '0000-00-00', '0000-00-00', 'reg', '2014', ''),

(3, 1, 'vera', '2019-02-14', '0000-00-00', 'spe', '2014', ''),

(4, 3, 'jutr', '2014-02-20', '0000-00-00', 'spe', '2014', ''),

(5, 2, 'plain', '2021-02-14', '0000-00-00', 'spe', '2014', ''),

(6, 2, 'bord', '0000-00-00', '0000-00-00', 'reg', '2014', '');


-- --------------------------------------------------------




-- --------------------------------------------------------




--

-- Table structure for table `tbl_student`

--


CREATE TABLE IF NOT EXISTS `tbl_student` (

  `stu_code` int(11) NOT NULL AUTO_INCREMENT,

  `stu_first_name` varchar(30) NOT NULL,

  `stu_last_name` varchar(30) NOT NULL,

  `stu_nickname` varchar(30) NOT NULL,

  PRIMARY KEY (`stu_code`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;


--

-- Dumping data for table `tbl_student`

--


INSERT INTO `tbl_student` (`stu_code`, `stu_first_name`, `stu_last_name`, `stu_nickname`) VALUES

(1, 'avi', 'a', 'avi a'),

(2, 'men', 's', 'men s.'),

(3, 'dany', 'd', 'dany d'),

(4, 'yael', 'f', 'yael f'),

(5, 'jo', 'g', 'jo g'),

(6, 'bel', 'g', 'bel g');


-- --------------------------------------------------------


--

-- Table structure for table `tbl_student_to_activity`

--


CREATE TABLE IF NOT EXISTS `tbl_student_to_activity` (

  `stu_code` int(11) NOT NULL,

  `activity_code` int(11) NOT NULL,

  KEY `stu_code` (`stu_code`),

  KEY `activity_code` (`activity_code`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


--

-- Dumping data for table `tbl_student_to_activity`

--


INSERT INTO `tbl_student_to_activity` (`stu_code`, `activity_code`) VALUES

(1, 1),

(2, 1),

(3, 3),

(4, 2),

(5, 5),

(6, 6),

(3, 4);


-- --------------------------------------------------------


--

-- Constraints for dumped tables

--


--

-- Constraints for table `tbl_activity`

--

ALTER TABLE `tbl_activity`

  ADD CONSTRAINT `tbl_activity_ibfk_1` FOREIGN KEY (`timetable_code`) REFERENCES `tbl_timetable` (`timetable_code`);


--

-- Constraints for table `tbl_student_to_activity`

--

ALTER TABLE `tbl_student_to_activity`

  ADD CONSTRAINT `tbl_student_to_activity_ibfk_2` FOREIGN KEY (`activity_code`) REFERENCES `tbl_activity` (`activity_code`) ON DELETE CASCADE ON UPDATE CASCADE,

  ADD CONSTRAINT `tbl_student_to_activity_ibfk_1` FOREIGN KEY (`stu_code`) REFERENCES `tbl_student` (`stu_code`) ON DELETE CASCADE ON UPDATE CASCADE;



the relaitions are:


at Avtivity model:

public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'timetableCode' => array(self::BELONGS_TO, 'Timetable', 'timetable_code'),

			'studentToActivities' => array(self::HAS_MANY, 'StudentToActivity', 'activity_code'),

			

		);

	}

	

	at Student model:

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'studentToActivities' => array(self::HAS_MANY, 'StudentToActivity', 'stu_code'),

			'studentToClasses' => array(self::HAS_MANY, 'StudentToClass', 'stu_code'),

			'studentToTempactivities' => array(self::HAS_MANY, 'StudentToTempactivity', 'stu_code'),

		);

	}

And that what i have at the viwe:


<?php

 $activitys = Activity::model()->findAll();  

foreach($activitys as $activity) {

    echo "<h2>activity : " . $activity->activity_name . "</h2>";

    echo "<ul>";

    foreach($activity->studentToActivities as $StudentToActivity) { 

        echo "<li>" . $StudentToActivity->stu_code . "</li>";

   }

    echo "</ul>";

}


        ?>

And I get this error: Invalid argument supplied for foreach()

help me please to find the proper way to do it

I think you should read this document… it will give you some idea

http://www.yiiframework.com/wiki/285/accessing-data-in-a-join-table-with-the-related-models/

follow this…

http://www.yiiframew…related-models/

thenks! very much