CakePHP模型“Member”与模特“Member”无关(CakePHP Model “Member” is not associated with model “Member”)

编程入门 行业动态 更新时间:2024-10-27 14:32:35
CakePHP模型“Member”与模特“Member”无关(CakePHP Model “Member” is not associated with model “Member”)

我正在使用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.

更多推荐

本文发布于:2023-07-28 08:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1303059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模特   模型   CakePHP   Member   model

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!