我是 cakePhp 的新手,在调用公共变量以显示在我的 add.ctp 中时遇到问题
下面是模型文件夹中的我的province.php:
<?php
App::uses('AppModel', 'Model');
App::uses('Sanitize', 'Utility');
/**
* Place Model
*
*/
class Province extends AppModel {
/**
* Display field
*
* @var string
*/
public $continent = array(
'AF' => 'Africa',
'AS' => 'Asia',
'EU' => 'Europe',
'NA' => 'North America',
'SA' => 'South America',
'OC' => 'Oceania',
'AN' => 'Antartica'
);
public $countries = array(
'Asia' => array(
'AF' => 'Afghanistan',
'AM' => 'Armenia',
'AZ' => 'Azerbaijan',
'BH' => 'Bahrain',
'BD' => 'Bangladesh',
'BT' => 'Bhutan',
'BN' => 'Brunei',
'KH' => 'Cambodia',
'CN' => 'China',
'CX' => 'Christmas Island',
'CC' => 'Cocos Islands',
'IO' => 'Diego Garcia',
'GE' => 'Georgia',
'HK' => 'Hong Kong',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran',
'IQ' => 'Iraq',
'IL' => 'Israel',
'JP' => 'Japan',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KP' => 'North Korea',
'KR' => 'South Korea',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Laos',
'LB' => 'Lebanon',
'MO' => 'Macau',
'MY' => 'Malaysia',
'MV' => 'Maldives',
'MN' => 'Mongolia',
'MM' => 'Myanmar',
'NP' => 'Nepal',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PS' => 'Palestine',
'PH' => 'Philippines',
'QA' => 'Qatar',
'SA' => 'Saudi Arabia',
'SG' => 'Singapore',
'LK' => 'Sri Lanka',
'SY' => 'Syria',
'TW' => 'Taiwan',
'TJ' => 'Tajikistan',
'TH' => 'Thailand',
'TR' => 'Turkey',
'TM' => 'Turkmenistan',
'AE' => 'United Arab Emirates',
'UZ' => 'Uzbekistan',
'VN' => 'Vietnam',
'YE' => 'Yemen',
)
);
public $belongsTo = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'image_id'
),
);
}
这是我的省控制器代码:
<?php
App::uses('AppController', 'Controller');
App::uses('Sanitize', 'Utility');
/**
* Static content controller
*
* Override this controller by placing a copy in controllers directory of an application
*
* @package app.Controller
* @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
*/
class ProvincesController extends AppController {
var $components = array('RequestHandler');
public $helpers = array('Html', 'Form');
public $defaultFields = array(
'Province.id'
);
public function add(){
$this->_form();
}
public function edit($id = null) {
$this->Province->id = $id;
if(!$this->Province->exists()) {
throw new NotFoundException(__('Invalid Province'));
}
$data = $this->Province->read(null, $id);
$this->_form($id, $data);
if($this->request->is('post') || $this->request->is('put')) {
} else {
$this->request->data = $data;
}
$this->render('add');
} // end function edit
public function _form($id = null, $old_data = false) {
/*if(!$this->is_allowed_crud()) {
$this->redirect('login?return=provinces/add');
}*/
if (!UserPerm::is_admin()) {
throw new NotFoundException(__('Invalid request'));
}
$s3province = new S3Lib('province');
$this->set('s3', $s3province);
if($this->request->is('post') || $this->request->is('put')) {
$err = false;
$place_id = false;
$is_new = false;
//debug($this->Province->id); exit;
if($id) {
$province_id = $this->Province->id = $id;
} else {
$this->Province->create();
$is_new = true;
}
$file = $this->request->data['Province']['img'];
if($file['tmp_name']) {
if($file['error'] === UPLOAD_ERR_OK) {
$new_file = $s3province->upload($file['tmp_name']);
$image_data = array(
'filename' => $new_file,
'metadata' => '',
'title' => $this->request->data['Province']['img_title'],
'description' => $this->request->data['Province']['img_desc'],
'thumb_ready' => 1
);
if(UserPerm::provinces_attribute()) { // base on places_attribute search for this
$image_data['source_url'] = $this->request->data['Province']['img_source'];
$image_data['source_name'] = $this->request->data['Province']['img_source_name'];
}
App::uses('Image', 'Model');
$Image = new Image();
$is_new_file = false;
if(isset($old_data['Image']['id']) && $old_data['Image']['id']) {
$image_data['id'] = $old_data['Image']['id'];
$Image->id = $old_data['Image']['id'];
} else {
$Image->create();
$is_new_file = true;
}
if($Image->save($image_data)) {
if($is_new_file) {
$this->request->data['Province']['image_id'] = $Image->getLastInsertID();
} else {
$this->request->data['Province']['image_id'] = $old_data['Image']['id'];
}
// Delete old image in S3
if(isset($old_data['Image']['id']) && $old_data['Image']['id']) {
$s3province->del($old_data['Image']['filename']);
}
} else {
$this->Session->setFlash(__('The image could not be saved. Please try again.') . $err, 'flash_error');
return;
}
} else {
$this->Session->setFlash(__('The image could not be uploaded. Please try again.') . $err, 'flash_error');
return;
}
}
$this->request->data['Province']['name'] = Sanitize::clean($this->request->data['Province']['name']);
$this->request->data['Province']['name'] = ucwords($this->request->data['Province']['name']);
/*$this->request->data['Province']['country'] = ucwords($this->request->data['Province']['country']);
$this->request->data['Province']['region'] = ucwords($this->request->data['Province']['region']);*/
$role_id = $this->Session->read('Auth.User.user_group_id');
//this will make a custommade id
$province_id = $this->request->data['Province']['name'];
if($this->Province->findByName($province_id)) {
$this->Session->setFlash(__('This province already exist on the Database. Please, try another place.'), 'flash_error');
return;
}
$province_id = substr($province_id,0,3);
$province_id = strtoupper($province_id);
if($this->Province->findById($province_id)) {
$province_id = str_shuffle($province_id);
$province_id = $this->request->data['Province']['id'] = $province_id;
} else {
$province_id = $this->request->data['Province']['id'] = $province_id;
} // end of first check for id duplicate
if($this->Province->save($this->request->data)) {
if(!$province_id)
$province_id = $this->Province->getLastInsertID();
} else {
if(is_array($this->Province->validationErrors)) {
$err .= '<ul>';
foreach($this->Province->validationErrors as $err) {
$err .= '<li>' . $err[0] . '</li>';
}
$err .= '</ul>';
}
}
if($province_id && !$err) {
$this->Session->setFlash(__('The province has been saved'), 'flash_success');
$this->redirect(array('action' => 'edit', $province_id));
} else {
$this->Session->setFlash(__('This province could not be saved. Please, try again.') . $err, 'flash_error');
}
}
} // end function _form
}
这是我的观点add.ctp:
<div class="places form box-rounded">
<div class="pull-right">
<?php echo $this->Html->link('View', 'view/' .$this->request->data['Province']['id']) ?>
</div>
<?php if(!empty($this->request->data)): ?>
<h2><?php echo __('Edit Province'); ?></h2>
<?php else: ?>
<h2><?php echo __('New Province'); ?></h2>
<?php endif; ?>
<?php
echo $this->Form->create('Province', array('type' => 'file', 'inputDefaults' => array('class' => 'form-control', 'div' => array('class' => 'col-sm-3'))));
echo "<div class='form-group row'>" . $this->Form->input('name') . "</div>";
echo "<div class='form-group row'>" . $this->Form->input('country', array('options' => $countries,
'label' => _('Country'),
)) . "</div>";
debug($countries); exit;
echo "<div class='form-group row'>" . $this->Form->input('region') . "</div>";
?>
<fieldset>
<legend>Image</legend>
<?php
echo '<div class="row"><div class="col-sm-12">Current: ';
if (isset($this->request->data['Province']['image_id']) && $this->request->data['Province']['image_id']) {
$url = $s3->get_url($this->request->data['Image']['filename'], 'preview');
echo $this->Html->image($url, array("class" => 'img-rounded'));
}
echo "</div></div>";
?>
<?php
echo "<div class='form-group row'>" . $this->Form->input('img', array(
'label' => __('Upload'),
'type' => 'file',
'class' => ''
)) . "</div>";
echo "<div class='form-group row'>" . $this->Form->input('img_title', array(
'label' => __('Image Title'),
'value' => @$this->request->data['Image']['title']
)) . "</div>";
echo "<div class='form-group row'>" . $this->Form->input('img_desc', array(
'label' => __('Describe'),
'value' => @$this->request->data['Image']['description']
)) . "</div>";
?>
<fieldset>
<legend>Image Attribution</legend>
<?php
echo "<div class='form-group row'>" . $this->Form->input('img_source', array(
'label' => __('Source URL'),
'value' => @$this->request->data['Image']['source_url']
)) . "</div>";
echo "<div class='form-group row'>" . $this->Form->input('img_source_name', array(
'label' => __('Source Name'),
'value' => @$this->request->data['Image']['source_name']
)) . "</div>";
?>
</fielset>
</fielset>
<?php
$options = array(
'label' => 'Save',
'escape' => false,
'class' => 'btn btn-success'
);
?>
<?php echo $this->Form->end($options); ?>
这就是我得到的:
'<div class="col-sm-3"><label for="ProvinceCountry">Country</label><select name="data[Province][country]" class="form-control" maxlenght="15" id="ProvinceCountry">
'
选项没有出现。我在这里错过了什么吗?我想在我的选择框中显示模型中的 $countries 变量作为选项。谢谢你。