i am very new in cakephp, actually i want to show the all the list of subjects which is belong to one subject category ,
for this one i have made a subject model whic h code is below:
subjects.php
<?php
class Subject extends AppModel
{
var $name='Subject';
var $belongsTo = 'Subjectscat';
}
?>
and subject category model is:
subjectscats.php
<?php
class Subjectscat extends AppModel
{
var $name='Subjectscat';
var $hasMany=array('Subject');
}
?>
subjectscats_controller.php
<?php
class SubjectscatsController extends AppController
{
var $name='Subjectscats';
var $helpers = array('Html', 'Javascript', 'Ajax');
var $components = array('Auth');
var $uses=array('Subject', 'User','Subjectscat');
function index()
{
$this->layout = 'internal';
$session_id=$this->Session->read('User.id');
$username = $this->User->find('all', array('conditions' => array('User.id' =>$session_id)));
$this->set('session_id',$username);
$this->Subjectscat->recursive =1;
$cat = $this->Subjectscat->find('all');
$this->set('subjectcategory',$cat);
}
function isAuthorized() {
return true;
}
}
?>
and the view which is located such that:
views\Subjectscats\index.ctp
<table><tr><td colspan="3">View Feedback</tr>
<?php debug($subjectcategory) ?> <?php echo $html->link('Add Feedback', '/feedbacks/add', array('class'=>'button')); ?>
<?php print_r($subjectcategory); ?>
<?php foreach($subjectcategory as $subject_cat)
{
?>
<tr>
<td><?php echo $subject_cat['Subjectscat']['cat_name'];?></td>
<?php print_r($subject_cat['Subjectscat']['Subjects']); ?>
</tr>
<?php }
?>
</tr>
</table>
and it give the error is:
Notice (8): Undefined index: Subjects [APP\views\subjectscats\index.ctp, line 10]
-- Table structure for table `subjects`
--
CREATE TABLE IF NOT EXISTS `subjects` (
`id` bigint(4) NOT NULL auto_increment,
`subjectscats_id` bigint(4) NOT NULL,
`name` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `subjectscats_id` (`subjectscats_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `subjects`
--
INSERT INTO `subjects` (`id`, `subjectscats_id`, `name`) VALUES
(1, 1, 'English'),
(2, 1, 'Hindi');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `subjects`
--
ALTER TABLE `subjects`
ADD CONSTRAINT `subjects_ibfk_1` FOREIGN KEY (`subjectscats_id`) REFERENCES `subjectscats` (`id`) ON UPDATE CASCADE;