CodeIgniter ActiveRecord问题
我正在尝试在CodeIgniter上构建我的第一个应用程序。这也是我第一次尝试尽可能地坚持OOP和MVC。到目前为止一切正常,但现在我正试图写我的第一个模型,我遇到了一些麻烦。下面是我得到的错误: 发生数据库错误 错误号码:1064 您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解第3行“Castledine”附近使用的正确语法 从(CodeIgniter ActiveRecord问题,activerecord,codeigniter,Activerecord,Codeigniter,我正在尝试在CodeIgniter上构建我的第一个应用程序。这也是我第一次尝试尽可能地坚持OOP和MVC。到目前为止一切正常,但现在我正试图写我的第一个模型,我遇到了一些麻烦。下面是我得到的错误: 发生数据库错误 错误号码:1064 您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解第3行“Castledine”附近使用的正确语法 从(authors)中选择*其中author=Earle Castledine 如下所示,这与我的模型中的以下行有关: $this->db-&g
authors
)中选择*其中author
=Earle Castledine
如下所示,这与我的模型中的以下行有关:
$this->db->get_where('authors', array('author' => $author));
我不太清楚它为什么会抛出错误。是因为Earle Castledine没有被引用吗?如果是,为什么CI不把它们放在那里?我怀疑这是个问题,宁愿认为这是我的错,但我不确定
我还有另一个问题。标记和作者都不会插入到各自的表中。他们的insert语句被包装在一个条件中,该条件应该确保它们不存在,但它似乎失败了,并且insert永远不会发生。我假设它失败了,因为标记没有被放入数据库中,在抛出错误之前,它已经在author部分中了。我知道如何使用纯PHP实现这一点,但我正在尝试使用纯CI ActiveRecord方式
以下是我使用的声明:
if ($this->db->count_all_results() == 0)
我用它来代替我通常使用的:
if (mysql_num_rows() == 0)
我做错了吗
下面是我的模型和控制器(只有重要的功能),尽我所能对其进行了评论
型号:
function new_book($book, $tags, $authors, $read) {
// Write book details to books table
$this->db->insert('books', $book);
// Write tags to tag table and set junction
foreach ($tags as $tag) {
// Check to see if the tag is already in the 'tags' table
$this->db->get_where('tags', array('tag' => $tag));
// trying to use this like mysql_num_rows()
if ($this->db->count_all_results() == 0) {
// Put it there
$this->db->insert('tags', $tag);
}
// Set the junction
// I only need the id, so...
$this->db->select('id');
// SELECT id FROM tags WHERE tag = $tag
$query = $this->db->get_where('tags', array('tag' => $tag));
// INSERT INTO books_tags (book_id, tag_id) VALUES ($book['isbn'], $query->id)
$this->db->insert('books_tags', array('book_id' => $book['isbn'], 'tag_id' => $query->id));
}
// Write authors to author table and set junction
// Same internal comments apply from tags above
foreach ($authors as $author) {
$this->db->get_where('authors', array('author' => $author));
if ($this->db->count_all_results() == 0) {
$this->db->insert('authors', $author);
}
$this->db->select('id');
$query = $this->db->get_where('authors', array('author' => $author));
$this->db->insert('authors_books', array('book_id' => $book['isbn'], 'author_id' => $query));
}
// If the user checked that they've read the book
if (!empty($read)) {
// Get their user id
$user = $this->ion_auth->get_user();
// INSERT INTO books_users (book_id, tag_id) VALUES ($book['isbn'], $user->id)
$this->db->insert('books_users', array('book_id' => $book['isbn'], 'user_id' => $user->id));
}
}
控制器:
function confirm() {
// Make sure they got here by form result, send 'em packing if not
$submit = $this->input->post('details');
if (empty($submit)) {
redirect('add');
}
// Set up form validation
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<h3 class="error">', ' Also, you’ll need to choose your file again.</h3>');
$this->form_validation->set_rules('isbn','ISBN-10','trim|required|exact_length[10]|alpha_numeric|unique[books.isbn]');
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('tags','tags','required');
// Set up upload
$config['upload_path'] = './books/';
$config['allowed_types'] = 'pdf|chm';
$this->load->library('upload', $config);
// If they failed validation or couldn't upload the file
if ($this->form_validation->run() == FALSE || $this->upload->do_upload('file') == FALSE) {
// Get the book from Amazon
$bookSearch = new Amazon();
try {
$amazon = $bookSearch->getItemByAsin($this->input->post('isbn'));
} catch (Exception $e) {
echo $e->getMessage();
}
// Send them back to the form
$data['image'] = $amazon->Items->Item->LargeImage->URL;
$data['content'] = 'add/details';
$data['error'] = $this->upload->display_errors('<h3 class="error">','</h3>');
$this->load->view('global/template', $data);
// If they did everything right
} else {
// Get the book from Amazon
$bookSearch = new Amazon();
try {
$amazon = $bookSearch->getItemByAsin($this->input->post('isbn'));
} catch (Exception $e) {
echo $e->getMessage();
}
// Grab the file info
$file = $this->upload->data();
// Prep the data for the books table
$book = array(
'isbn' => $this->input->post('isbn'),
'title' => mysql_real_escape_string($this->input->post('title')),
'date' => $amazon->Items->Item->ItemAttributes->PublicationDate,
'publisher' => mysql_real_escape_string($amazon->Items->Item->ItemAttributes->Publisher),
'pages' => $amazon->Items->Item->ItemAttributes->NumberOfPages,
'review' => mysql_real_escape_string($amazon->Items->Item->EditorialReviews->EditorialReview->Content),
'image' => mysql_real_escape_string($amazon->Items->Item->LargeImage->URL),
'thumb' => mysql_real_escape_string($amazon->Items->Item->SmallImage->URL),
'filename' => $file['file_name']
);
// Get the tags, explode by comma or space
$tags = preg_split("/[\s,]+/", $this->input->post('tags'));
// Get the authors
$authors = array();
foreach ($amazon->Items->Item->ItemAttributes->Author as $author) {
array_push($authors, $author);
}
// Find out whether they've read it
$read = $this->input->post('read');
// Send it up to the database
$this->load->model('add_model', 'add');
$this->add->new_book($book, $tags, $authors, $read);
// For now... Later I'll load a view
echo 'Success';
}
}
函数确认(){
//确保他们是根据表格结果来的,否则就打包
$submit=$this->input->post('details');
如果(空($提交)){
重定向(“添加”);
}
//设置表单验证
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('',您还需要再次选择文件');
$this->form_validation->set_rules('isbn','isbn-10','trim | required |精确的|长度[10]|字母|数字|唯一的[books.isbn]);
$this->form_validation->set_规则('title','title','required');
$this->form_validation->set_规则('tags','tags','required');
//设置上载
$config['upload_path']='./books/';
$config['allowed_types']='pdf | chm';
$this->load->library('upload',$config);
//如果验证失败或无法上载文件
如果($this->form_validation->run()==FALSE | |$this->upload->do_upload('file')==FALSE){
//从亚马逊获得这本书
$bookSearch=新亚马逊();
试一试{
$amazon=$bookSearch->getItemByAsin($this->input->post('isbn');
}捕获(例外$e){
echo$e->getMessage();
}
//把他们送回表格
$data['image']=$amazon->Items->Item->LargeImage->URL;
$data['content']='add/details';
$data['error']=$this->upload->display_errors(“”,”);
$this->load->view('global/template',$data);
//如果他们一切都做对了
}否则{
//从亚马逊获得这本书
$bookSearch=新亚马逊();
试一试{
$amazon=$bookSearch->getItemByAsin($this->input->post('isbn');
}捕获(例外$e){
echo$e->getMessage();
}
//抓取文件信息
$file=$this->upload->data();
//为books表准备数据
$book=数组(
'isbn'=>this->input->post('isbn'),
'title'=>mysql\u real\u escape\u字符串($this->input->post('title'),
“日期”=>$amazon->Items->Item->ItemAttributes->PublicationDate,
'publisher'=>mysql\u real\u escape\u string($amazon->Items->Item->itemtattributes->publisher),
“页面”=>$amazon->Items->Item->ItemAttributes->NumberOfPages,
“评论”=>mysql\u real\u escape\u字符串($amazon->Items->Item->EditorialReviews->EditorialReview->Content),
'image'=>mysql\u real\u escape\u字符串($amazon->Items->Item->LargeImage->URL),
'thumb'=>mysql\u real\u escape\u字符串($amazon->Items->Item->SmallImage->URL),
'filename'=>$file['file\u name']
);
//获取标记,按逗号或空格分解
$tags=preg_split(“/[\s,]+/”,$this->input->post('tags');
//找到作者
$authors=array();
foreach($amazon->Items->Item->ItemAttributes->Author as$Author){
数组推送($authors,$author);
}
//看看他们是否读过
$read=$this->input->post('read');
//将其发送到数据库
$this->load->model('add_model','add');
$this->add->new_book($book、$tags、$authors、$read);
//现在…稍后我将加载一个视图
呼应"成功",;
}
}
谁能帮我弄清楚我做错了什么?多谢
Marcus在使用count\u all\u results()时,我想您的意思是使用num\u rows()。count\u all\u results()将实际创建一个SELECT count(*)查询 用于调试您的问题…
如果要测试insert()是否有效,请使用,例如:
var_dump($this->db->affected_rows());
var_dump($this->db->last_query());
在任何时候,您都可以输出上一次查询的内容,例如:
var_dump($this->db->affected_rows());
var_dump($this->db->last_query());
通过在控制器中添加以下内容,还可以打开,以便查看正在运行的所有查询:
$this->output->enable_profiler(TRUE);
我自己设法解决了这个问题。控制器没有真正改变,但新型号如下:
function new_book($book, $tags, $authors, $read) {
// Write book details to books table
$this->db->insert('books', $book);
// Write tags to tag table and set junction
foreach ($tags as $tag) {
// Check to see if the tag is already in the 'tags' table
$query = $this->db->get_where('tags', array('tag' => $tag));
// trying to use this like mysql_num_rows()
if ($query->num_rows() == 0) {
// Put it there
$this->db->insert('tags', array('tag' => $tag));
}
// Set the junction
// I only need the id, so...
$this->db->select('id');
// SELECT id FROM tags WHERE tag = $tag
$query = $this->db->get_where('tags', array('tag' => $tag));
$result = $query->row();
// INSERT INTO books_tags (book_id, tag_id) VALUES ($book['isbn'], $query->id)
$this->db->insert('books_tags', array('book_id' => $book['isbn'], 'tag_id' => $result->id));
}
// Write authors to author table and set junction
// Same internal comments apply from tags above
foreach ($authors as $author) {
$query = $this->db->get_where('authors', array('author' => mysql_real_escape_string($author)));
if ($query->num_rows() == 0) {
$this->db->insert('authors', array('author' => mysql_real_escape_string($author)));
}
$this->db->select('id');
$query = $this->db->get_where('authors', array('author' => mysql_real_escape_string($author)));
$result = $query->row();
$this->db->insert('authors_books', array('book_id' => $book['isbn'], 'author_id' => $result->id));
}
// If the user checked that they've read the book
if (!empty($read)) {
// Get their user id
$user = $this->ion_auth->get_user();
// INSERT INTO books_users (book_id, tag_id) VALUES ($book['isbn'], $user->id)
$this->db->insert('books_users', array('book_id' => $book['isbn'], 'user_id' => $user->id));
}
}
我已经弄明白了。直到24小时前我都无法回答,但如果你不喜欢,也不要觉得你必须去做。也就是说,我会