CakePHP - 调用另一个导致“SQLSTATE[42000]:语法错误或访问冲突”的模型


CakePHP - Calling another model causing "SQLSTATE[42000]: Syntax error or access violation"

我是cakePHP的新手,我到处寻找答案,但找不到原因。我正在尝试在远程模型中调用一个函数,如果我直接运行模型,它工作正常,但如果我从另一个模型运行它,它会导致"语法错误或访问冲突"错误。

代码如下:

面板控制器

class CpanelController extends AppController {
    var $uses = array('Client');
    public function index() {
        $this->Client->index();
    }
}

客户端模型

class Client extends AppModel {
    public $useTable = 'users';
}

客户端控制器

class ClientController extends AppController {
    public function index() {
        echo "running";
    }
}

当我从我的网站/客户端运行它时,它运行良好。但是如果我尝试从 mysite/Cpanel 加载它,它会抛出:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index' at line 1

谢谢

您的User模型中没有index函数。

也许你打算做的是

public function index() {
        $this->Client->find('all');
    }

否则,您必须在模型内创建一个索引函数

class Client extends AppModel {
    public $useTable = 'users';
    public function index() {
       // Do Something;
    }
}

如果你想在CpanelController中使用UserController,这里是代码

App::uses('UserController', 'Controller');
$UserController= ClassRegistry::init('UserController');
$UserController->index();