tags:

views:

224

answers:

1

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;
+3  A: 

I sometimes wish developers to pay more attention to details.

The notice you're getting is coming from this line:

print_r($subject_cat['Subjectscat']['Subjects'])

If you replace that line with this one you will see why:

debug($subject_cat)

"Subjects" key does not exist in your array, all models are singular, remember? What you need to do there is a foreach loop:

foreach ($subject_cat['Subject'] as $subject)
{
    // output stuff you need from $subject
}

Remember the debug() function, it is a great tool. Another way to debug your stuff is the excellent debug_kit plugin; very easy to set up and very, very useful.

dr Hannibal Lecter