我在other thread上找到了答案:实际上这个错误只是一个臭虫气体orm类的副作用。 我做了什么:
application/third_party/gas/classes/orm.php
之前:
if (empty($this->primary_key))
后:
if (! empty($this->primary_key))
注意:要使此修补程序正常工作,您必须始终在模型中指定$primary_key
。
我正在尝试使用Gas ORM 2.1和CodeIgniter 3.1.4(PHP 7)并且我在使用关系时遇到问题:似乎SQL请求中未指定外键:
A Database Error Occurred
Error Number: 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 ')' at line 1
SELECT * FROM `Client` WHERE `Client`.`id_cli` IN ()
Filename: third_party/gas/classes/core.php
Line Number: 850
关系如下:
表格按以下方式填写:
mysql> select * from Contact;
+-------+-----------+--------+--------+
| id_ct | prenom_ct | nom_ct | id_cli |
+-------+-----------+--------+--------+
| 1 | bonbeur | jean | 1 |
| 2 | lapraline | toto | 1 |
| 3 | cover | harry | 2 |
+-------+-----------+--------+--------+
3 rows in set (0,00 sec)
mysql> select * from Client;
+--------+-------------+------------+
| id_cli | nom_cli | nomasa_cli |
+--------+-------------+------------+
| 1 | My Client 1 | MC1 |
| 2 | My Client 2 | MC2 |
+--------+-------------+------------+
2 rows in set (0,00 sec)
联系模型如下:
<?php
namespace Model;
use \Gas\Core;
use \Gas\ORM;
class Contact extends ORM {
public $table = "Contact";
public $primary_key = 'id_ct';
function _init()
{
self::$relationships = array (
'id_cli' => ORM::belongs_to('\\Model\\Client')
);
self::$fields = array(
'id_ct' => ORM::field('auto[10]'),
'prenom_ct' => ORM::field('char[100]', array('required','max_length[100]')),
'nom_ct' => ORM::field('char[100]', array('required','max_length[100]')),
);
}
}
客户端模型如下:
<?php
namespace Model;
use \Gas\Core;
use \Gas\ORM;
class Client extends ORM {
public $primary_key = 'id_cli';
public $table = "Client";
function _init()
{
self::$relationships = array (
'Contact' => ORM::has_many('\\Model\\Contact')
);
self::$fields = array(
'id_cli' => ORM::field('auto[10]'),
'nom_cli' => ORM::field('char[250]', array('required','max_length[250]')),
'nomasa_cli' => ORM::field('char[25]', array('required','max_length[25]')),
);
}
}
控制器如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
/**
* Index Page for this controller.
*/
public function index() {
return $this->listing();
}
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function listing() {
$data["contacts"] = Model\Contact::all();
foreach($data['contacts'] as $c) {
$cli = $c->id_cli();
/*XXX*/ echo "<pre>";var_dump($cli);echo "</pre>";/*XXX*/
}
$this->render->show('contact/listing', $data);
}
}
我想要的是获取与该联系人相关的客户。
错误发生在var_dump
之前(因此除了错误消息之外没有显示任何内容)。
我的数据库表不遵守Gas ORM命名约定:
$table
)id_<suffix>
,其中<suffix>
与表格相关(参见模型中的$primary_key
)基于此,我不确定我是否正确使用Gas。
参考文献:
Gas ORM文件:gasorm-doc.taufanaditya.com
Code igniter网站:codeigniter.com
PS:抱歉,没有足够的声誉来包含图片或插入真实链接。
答案 0 :(得分:0)
解决:请参阅上面的编辑部分。