我正在使用CakePHP创建一个PHP论坛我遇到麻烦,获取所有成员的数组,然后在视图中回显它们,这是我的代码。
<?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时,我得到一个错误,说模型“Member”与模型“Member”无关[CORE / Cake / Model / Behavior / ContainableBehavior.php,第342行]
在你问我之前我已经让AppModels actas Containable
class AppModel extends Model { public $actsAs = array('Containable'); }I am making a PHP forum with CakePHP I am troubles with getting an array of all the members then echoing them in a view, here is my code.
<?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' => '' ) ); }That is my member model here is my 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()); } }And here is my Index view
<div class="row"> <?php foreach ($members as $member): ?> <?php echo $user[name]; ?> <?php endforeach; ?> </div>When I access example.com/members I get an error saying Model "Member" is not associated with model "Member" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]
Before you ask I have made AppModels actas Containable
class AppModel extends Model { public $actsAs = array('Containable'); }最满意答案
删除该行:
$this->Paginator->settings['contain'] = array('Member');您通过使用此方法导致自联接,并且您尚未在成员模型中定义它,并且您不希望这样。
这是错误的,加上你的php有一个语法错误 - 缺少名称。
<?php foreach ($members as $member): ?> <?php echo $user[name]; ?> <?php endforeach; ?>它应该是:
<?php foreach ($members as $member): ?> <?php echo $member['Member']['name']; ?> <?php endforeach; ?>你正在努力学习基础知识,我建议你做博客教程或者至少阅读一下如何将数据传递给视图以及模型关联如何工作。 你可以在书中找到所有内容 。
Remove that line:
$this->Paginator->settings['contain'] = array('Member');You're causing a self-join by using this and you haven't defined that in your members model and you dont want that.
This is wrong as well plus your php has a syntax error - missing ' around the name.
<?php foreach ($members as $member): ?> <?php echo $user[name]; ?> <?php endforeach; ?>It should be:
<?php foreach ($members as $member): ?> <?php echo $member['Member']['name']; ?> <?php endforeach; ?>You're struggling with the very basics, I would recommend you to do the blog tutorial or at least read about how data is passed to the view and how model associations work. You can find everything in the book.
更多推荐
发布评论