我正在用CakePHP製作一個PHP論壇我很煩惱獲取所有成員的數組然後在視圖中回顯它們,下面是我的代碼。CakePHP模型「成員」與模型「成員」沒有關聯
<?php
App::uses('AppModel', 'Model');
class Member extends AppModel {
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty')
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty')
),
),
'email' => array(
'email' => array(
'rule' => array('email')
),
),
);
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
這就是我在這裏的成員模型是我MembersController
<?php
App::uses('Controller', 'AppController');
class MembersController extends AppController {
public $components = array('Paginator');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('profile','login');
}
public function index(){
$this->Paginator->settings['contain'] = array('Member');
$this->set('members', $this->Paginator->paginate());
}
public function profile($id=null) {}
public function login() {
if($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
這裏是我的索引視圖
<div class="row">
<?php foreach ($members as $member): ?>
<?php echo $user[name]; ?>
<?php endforeach; ?>
</div>
當我訪問example.com/members我得到一個錯誤說型號「成員」與模型「成員」沒有關聯[CORE/Cake/Model/Behavior/ContainableBehavior.php,line 342]
個你問之前,我已經取得AppModels ACTAS中可容納
class AppModel extends Model {
public $actsAs = array('Containable');
}
有了burzum的大量幫助,我已經修復了它,並且我還發現,不管你稱之爲控制器,只要添加公共$ uses = array(' Model1','Model2','Model3');在控制器的頂部 – user2710382
而不是將許多模型加載到控制器中(即使它們被延遲加載),最好通過該控制器的主模型的關聯來訪問其他模型。 – burzum