Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP友谊_Php_Cakephp - Fatal编程技术网

CakePHP友谊

CakePHP友谊,php,cakephp,Php,Cakephp,我有两个表用户和友谊,我创建了模型和友谊控制器,但一旦我尝试“添加”朋友,就会出现一个错误,我缺少“添加朋友”视图 下面是FriendsController中的add_friend函数 function add_friend ( $id ){ if(!empty($this->data)){ $this->Friendship->Create(); if($this->Friendship->save($this->da

我有两个表用户和友谊,我创建了模型和友谊控制器,但一旦我尝试“添加”朋友,就会出现一个错误,我缺少“添加朋友”视图

下面是FriendsController中的add_friend函数

function add_friend ( $id ){
    if(!empty($this->data)){
        $this->Friendship->Create();
        if($this->Friendship->save($this->data)){
            $this->Session->setFlash('Friendship Requested');
            $this->redirect(array('controller'=>'users','action'=>'profile'));
        }
    }
在我看来,这就是我提到它的方式

<?php echo 
$this->Html->link('Add as Friend', array('controller' => 'Friendships', 'action'=>'add_friend', h($user['User']['id']))); 

方法中的第一行:

if(!empty($this->data)){
由于$this->data中没有数据,因此被评估为“false”。因此,在该方法上不会执行任何其他操作,标准的cakePHP流将继续。这意味着,渲染视图

显然,由于$this->data是空的,所以方法的大部分代码都是“错误的”。谁和谁之间的友谊? 假设您已经在处理Auth,以及友谊和用户之间的关系,类似这样的事情会起作用:

function add_friend ( $id ){
if(!empty($id) && $this->Friendship->User->exists($id)){
    $this->Friendship->create();
    if($this->Friendship->save(array('user_id' => $id, 'requesteduser_id' => $this->Auth->user('id')))){
        $this->Session->setFlash('Friendship Requested');
        $this->redirect(array('controller'=>'users','action'=>'profile'));
    }
}
}


希望有帮助

错误:对非对象的成员函数exists()的调用。我以前使用过exists(),没有问题。可能是我在这里写错了什么吗?你的错误(可能)是因为你没有设置友谊和用户之间的关系。当我给你答案时,我的假设是:1。您使用的是cakephp v2.x2。方法add_friend在FriendsController 3中。友谊和用户(belongsTo)4之间建立了关系。您已经在使用Auth进行身份验证。如果您不清楚上述任何一点,我建议您深入阅读cakephp文档及其教程: