我想在CodeIgniter上构建我的第一个应用程序。这也是我第一次尽可能地坚持OOP和MVC。到目前为止一切都很顺利,但现在我正在尝试编写我的第一个模型,我遇到了一些麻烦。下面是我得到的错误:CodeIgniter ActiveRecord问题
数据库出错
错误编号:1064
您的SQL语法错误;检查对应于你的MySQL服务器版本在线路附近使用 'Castledine' 3
SELECT * FROM(
authors
)正确的语法手册WHEREauthor
=厄尔Castledine
其中,因为你会见下面,涉及我的模型中的以下行:
$this->db->get_where('authors', array('author' => $author));
我不太清楚为什么它会抛出错误。这是否因为厄尔卡斯汀没有被引用?如果是这样,为什么CI没有把它们放在那里呢?我怀疑这是问题所在,而不是认为这是我的错,但我不确定。
我还有另一个问题。标签和作者都没有插入到它们各自的表格中。他们的insert语句被包装在一个条件中,应该确保它们不存在,但它似乎失败了,并且插入从不发生。我认为它是失败的,因为标签没有被放入数据库中,并且在它抛出错误之前它已经在作者部分被关闭了。我知道如何用纯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';
}
}
任何人都可以帮上什么,我做错了棚灯?非常感谢。
马库斯
我有这个想通了。在24小时之前我无法回答,但是如果你不喜欢,你不需要再努力。也就是说,我有兴趣看看我是否现在正在做这件事(我的意思是,它可行,但是谁知道这是否正确),所以请随时放下你的想法。 – Marcus 2010-09-26 15:42:45