我在CakePHP中构建了一个应用程序(我在一个新工作中使用了这个工作了几个月,并从前一个员工接管了这个工程,所以我的理解是有限的)。如何在一个视图中引用多个控制器(CakePHP)
此应用程序有几个数据库表,涉及这个问题的是中心,软件和类别。
为了给你一个简短的帽子,我需要的是当用户访问'中心'视图时,能够获得下面所有软件的列表,并勾选哪些中心有权访问。
要查看的软件列表,我SoftwaresController使用功能:
public function view() {
$categories = $this->Software->Category->find ('list');
array_unshift ($categories, array('any' => 'Any'));
$this->set ('categories', $categories);
$this->Software->hasMany['Version']['limit'] = 1;
$this->Software->hasMany['Version']['fields'] = array('Version.id', 'Version.version_number', 'Version.created');
if ($this->request->is ('post') && $this->request->data) {
$conditions = array();
$filters = $this->request->data['Software'];
if (!empty($filters['name'])) {
$conditions['Software.name'] = $filters['name'];
}
if ($filters['category'] > 0) {
$conditions['Category.id'] = $filters['category'];
}
$this->paginate = array(
'conditions' => $conditions,
'order' => array(
'Software.latest_released' => 'desc',
'Software.name' => 'asc'
)
);
$this->set ('softwares', $this->paginate());
$this->set ('paging', false);
} else {
$this->paginate = array(
'order' => array(
'Software.latest_released' => 'desc',
'Software.name' => 'asc'
)
);
$this->set ('paging', true);
$this->set ('softwares', $this->paginate());
}
// This allows the user software page to show.
}
在CentresController功能,我希望能够同时查看数据(如上提取),看起来是这样的:
public function admin_view($id = null)
{
/* Centres */
$this->Centre->recursive = 0;
$this->Centre->id = $id;
if (!$this->Centre->exists()) {
throw new NotFoundException(__('This centre ID does not exist.'));
}
$centre = $this->Centre->read(null, $id);
$this->request->data = $centre;
$this->set('centre', $centre);
}
我试图通过将view()内部添加到admin_view()中来结合上述两个函数。对于admin_view()的看法是这样的:
指定软件访问
<div class="row-fluid">
<div class="span12">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Version Number</th>
<th>Created</th>
<th>Modified</th>
<th>Released</th>
<th>Grant Permission</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($software['Version'] as $version):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="even"';
} else {
$class = ' class="odd"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $version['version_number']; ?> </td>
<td>
<?php
echo $this->Time->format (Configure::read ('Format.time'), $version['created']);
?>
</td>
<td>
<?php
echo $this->Time->format (Configure::read ('Format.time'), $version['modified']);
?>
</td>
<td>
<?php
echo $this->Time->format (Configure::read ('Format.time'), $version['released']);
?>
</td>
<td class="actions" style="text-align: center;">
<?php
echo $this->Html->link (
'<i class="fa fa-cloud-download"></i> Download',
array(
'controller' => 'versions',
'action' => 'view',
$version['id']
),
array('escape' => false)
);
?>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- /.table-responsive -->
</div>
</fieldset>
<div class="col-sm-12 col-md-5 col-lg-4 padding-right-0">
<div class="panel panel-default">
<div class="panel-heading"><span class="fa fa-cog"></span> Actions</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-4 padding-bottom-10">
<?php
echo $this->Form->button(
'<i class="fa fa-pencil"></i> Update',
array(
'type' => 'submit',
'class' => 'btn btn-success btn-sm btn-block',
'escape' => false
)
);
echo $this->Form->end();
?>
</div>
<!-- /.col-sm-4 padding-bottom-10 -->
<div class="col-sm-4 padding-bottom-10">
<a class="btn btn-danger btn-sm btn-block" href="#modal-dialog"
data-toggle="modal"
data-target="#modal-dialog"><i class="fa fa-trash"></i> Delete</a>
</div>
<!-- /.col-sm-4 padding-bottom-10 -->
<div class="col-sm-4 padding-bottom-10">
<?php
echo $this->Html->link(
"<span class='fa fa-times'></span> Cancel",
array(
'action' => 'index'
),
array(
'escape' => false,
'class' => 'btn btn-default btn-sm btn-block'
)
);
?>
</div>
<!-- /.col-sm-4 padding-bottom-10 -->
</div>
<!-- /.row -->
</div>
我得到的错误是:
Error: Call to a member function find() on null File: /Applications/XAMPP/xamppfiles/htdocs/licensemanagementsite/app/Controller/CentresController.php Line: 87
我也包括在CentresController的顶部和中心模型: App :: uses('AppController','Controller','SoftwaresController');
任何援助,你可以给我会非常感激。我的办公室里没有人知道CakePHP的任何信息(包括我,我想!)。
谢谢!
CentresController.php 87行中有什么 –
在视图内部使用requestAction方法。或者在控制器中,如果你想在那个地方。 $ this-> requestAction('controller/action); array('named'=> array('limit'=> 3) echo $ this-> requestAction( array('controller'=>'articles','action'=>'featured')) ) ; array $('pass'=> array(5)) – Sudhir
line 87:> $(0)$ this-> requestAction( array('controller'=>'articles','action'=>'view'), array categories = $ this-> Software-> Category-> find('list'); – dplatt