【发布时间】:2016-01-20 13:41:06
【问题描述】:
我正在使用 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>
我得到的错误是:
错误:在 null 上调用成员函数 find() 文件:/Applications/XAMPP/xamppfiles/htdocs/licensemanagementsite/app/Controller/CentresController.php 线路:87
我还在 CentresController 和 Centers 模型的顶部包括: App::uses('AppController', 'Controller', 'SoftwaresController');
您能给我的任何帮助将不胜感激。我办公室里没有人对 CakePHP 有任何了解(包括我,我猜!)。
谢谢!
【问题讨论】:
-
CentresController.php 的第 87 行有什么
-
在视图中使用 requestAction 方法。如果你想在那个地方,或者在控制器中。 $this->requestAction('控制器/动作); echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('named' => array('limit' => 3)) ); echo $this->requestAction(array('controller' => 'articles', 'action' => 'view'), array('pass' => array(5))
-
第 87 行:> $categories = $this->Software->Category->find ('list');
标签: php cakephp controller models