Ben aslında bir konu kategoriye ait olduğu konuların tüm listesini göstermek istiyorum, cakephp
ile yeni duyuyorum. Ben aşağıda kod bir konu modeli yaptık:
subjects.php
<?php
class Subject extends AppModel
{
var $name='Subject';
var $belongsTo = 'Subjectscat';
}
?>
ve konu kategorisi model:
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;
}
}
?>
ve bulunduğu görünümü şekilde:
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>
ve bu hatayı verir:
Bildirimi (8): Undefined index: Konular [APP \ Defa \ subjectscats \ index.ctp, hat 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;