2010-09-26 76 views
0

我想在CodeIgniter上構建我的第一個應用程序。這也是我第一次儘可能地堅持OOP和MVC。到目前爲止一切都很順利,但現在我正在嘗試編寫我的第一個模型,我遇到了一些麻煩。下面是我得到的錯誤:CodeIgniter ActiveRecord問題

數據庫出錯

錯誤編號:1064

您的SQL語法錯誤;檢查對應於你的MySQL服務器版本在線路附近使用 'Castledine' 3

SELECT * FROM(authors)正確的語法手冊WHERE author =厄爾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&rsquo;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'; 

    } 

} 

任何人都可以幫上什麼,我做錯了棚燈?非常感謝。

馬庫斯

+0

我有這個想通了。在24小時之前我無法回答,但是如果你不喜歡,你不需要再努力。也就是說,我有興趣看看我是否現在正在做這件事(我的意思是,它可行,但是誰知道這是否正確),所以請隨時放下你的想法。 – Marcus 2010-09-26 15:42:45

回答

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 
     $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)); 
    } 

} 
0

如果您正在使用count_all_results(),我想你的意思是使用NUM_ROWS()。 count_all_results()實際上會創建一個SELECT COUNT(*)查詢。

用於調試您的問題...
如果你想測試是否插入()的工作,用affected_rows(),例如:

var_dump($this->db->affected_rows()); 

在任何時候,你可以輸出與last_query()最後的查詢,例如什麼:

var_dump($this->db->last_query()); 

你可以還打開Profiler,所以你可以看到所有的查詢正在運行,通過在控制器中加入:

$this->output->enable_profiler(TRUE);