2010-04-23 58 views
1

編輯:錯誤的原因(見下文):我在doctrine_pi.php中的默認主鍵是「ID」,所以我將它改爲「book_id」(我的數據庫的主鍵)和它工作。感謝馬克·B.Doctrine CodeIgniter MySQL的CRUD錯誤

嗨, 我使用CI +學說+ MySQL和我得到以下CRUD錯誤:

(1)當試圖與這個代碼在數據庫中創建一個新的記錄:

$book_title = 'The Peloponnesian War';
$b = new Book();
$b->title = $book_title;
$b->price = 10.50;
$b->save();

我得到這個錯誤:

Fatal error: Uncaught exeption 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'title' in 'field list' in ...

(2)當試圖對我的看法PA從數據庫中獲取和顯示記錄GE與此代碼:

$book_title = 'The Peloponnesian War';
$title = $book_title;
$search_results = Doctrine::getTable('Book')->findOneByTitle($title);
echo $search_results->title; //(in view file)

我得到這個錯誤:

Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[45S22]: Column not found: 1054 Unknown column 'b.id' in 'field list" in ...

最後,當我嘗試如下更新記錄:

$book_title = 'The Peloponnesian War';
$title = $book_title;
$u = Doctrine::getTable('Book')->find($title);
$u->title = $title;
$u->save();

,我收到了類似的錯誤:

Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'b.id' in 'field list''in ...

這是我的Doctrine_Record m奧德爾:

class Book extends Doctrine_Record{ 
    public function setTableDefinition() 
    { 
     $this->hasColumn('book_id'); 
     $this->hasColumn('isbn10','varchar',20); 
     $this->hasColumn('isbn13','varchar',20); 
     $this->hasColumn('title','varchar',100); 
     $this->hasColumn('edition','varchar',20); 
     $this->hasColumn('author_f_name','varchar',20); 
     $this->hasColumn('author_m_name','varchar',20); 
     $this->hasColumn('author_l_name','varchar',20); 
     $this->hasColumn('cond','enum',null, array('values' => array('as new','very  good','good','fair','poor'))); 
     $this->hasColumn('price','decimal',8, array('scale' =>2)); 
     $this->hasColumn('genre','varchar',20); 
    } 
    public function setUp() 
    { 
     $this->setTableName('Book'); 
     //$this->actAs('Timestampable'); 
    } 

最後,這裏是我的mysqldump:

-- MySQL dump 10.13 Distrib 5.1.41, for Win32 (ia32) 
-- 
-- Host: localhost Database: books 
-- ------------------------------------------------------ 
-- Server version 5.1.41 

/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 
/*!40103 SET @[email protected]@TIME_ZONE */; 
/*!40103 SET TIME_ZONE='+00:00' */; 
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; 

-- 
-- Table structure for table `book` 
-- 
CREATE DATABASE books; 
USE books; 

DROP TABLE IF EXISTS `book`; 
/*!40101 SET @saved_cs_client  = @@character_set_client */; 
/*!40101 SET character_set_client = utf8 */; 
CREATE TABLE `book` (
    `book_id` int(11) NOT NULL AUTO_INCREMENT, 
    `isbn10` char(20) DEFAULT NULL, 
    `isbn13` char(20) DEFAULT NULL, 
    `title` char(100) DEFAULT NULL, 
    `edition` char(20) DEFAULT NULL, 
    `author_f_name` char(20) DEFAULT NULL, 
    `author_m_name` char(20) DEFAULT NULL, 
    `author_l_name` char(20) DEFAULT NULL, 
    `cond` enum('as new','very good','good','fair','poor') DEFAULT NULL, 
    `price` decimal(8,2) DEFAULT NULL, 
    `genre` char(20) DEFAULT NULL, 
    PRIMARY KEY (`book_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 

-- 
-- Dumping data for table `book` 
-- 

LOCK TABLES `book` WRITE; 
/*!40000 ALTER TABLE `book` DISABLE KEYS */; 
INSERT INTO `book` VALUES (1,'0136061699','978-0136061694','Software Engineering: Theory and Practice','4','Shari','Lawrence','Pfleeger','very good','50.00','Computing'),(2,'0538469684','978-0538469685','Database Systems Design, Implementation, and Management','9','Peter','','Rob','as new','150.00','Computing'),(3,'1418835404','978-1418835408','Java Programming Program Design Including Data Structures','1','D','S','Malik','as new','150.00','Computing'),(4,'0201609215','978-0201609219','Introduction to Computer Graphics','1','James','D','Foley','good','100.00','Computing'),(5,'0534490964','978-0534490966','Discrete Mathematics with Applications','1','Susanna','','Epp','as new','150.00','Mathematics'),(6,'0321616999','978-0321616999','Brief Calculus and its Applications','12','Larry','J','Goldstein','fair','40.00','Mathematics'),(7,'0136154344','978-0136154341','College Algebra Essentials','8','Michael','','Sullivan','as new','150.00','Mathematics'),(8,'0495108359','978-0495108351','Trigonometry','6','Charles','P','McKeague','good','100.00','Mathematics'),(9,'0310291097','978-0310291091','Moral Choices: An Introduction to Ethics','3','Scott','B','Rae','good','100.00','Ethics'),(10,'1883925541','978-1883925543','Philosophy and Education: An Introduction in Christian Perspective','4','George','R','Knight','good','100.00','Philosophy'),(11,'000715447X','978-0007154470','Sociology Themes and Perspectives','6','Michael','','Haralambos','good','100.00','Sociology'),(12,'0273651404','978-0273651406','Economics: A Students Guide','5','John','','Beardshaw','good','100.00','Economics'),(13,'1883925169','978-1883925161','Reign of God','2','Richard','','Rice','good','100.00','Religion'); 
/*!40000 ALTER TABLE `book` ENABLE KEYS */; 
UNLOCK TABLES; 

-- 
-- Table structure for table `copy` 
-- 

DROP TABLE IF EXISTS `copy`; 
/*!40101 SET @saved_cs_client  = @@character_set_client */; 
/*!40101 SET character_set_client = utf8 */; 
CREATE TABLE `copy` (
    `copy_id` int(11) NOT NULL AUTO_INCREMENT, 
    `book_id` int(11) DEFAULT NULL, 
    `rental_date` date DEFAULT NULL, 
    `quantity` int(11) DEFAULT NULL, 
    `cus_id` int(11) DEFAULT NULL, 
    `emp_id` int(11) DEFAULT NULL, 
    `man_id` int(11) DEFAULT NULL, 
    PRIMARY KEY (`copy_id`), 
    KEY `book_id` (`book_id`), 
    KEY `cus_id` (`cus_id`), 
    KEY `emp_id` (`emp_id`), 
    KEY `man_id` (`man_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 

-- 
-- Dumping data for table `copy` 
-- 

LOCK TABLES `copy` WRITE; 
/*!40000 ALTER TABLE `copy` DISABLE KEYS */; 
/*!40000 ALTER TABLE `copy` ENABLE KEYS */; 
UNLOCK TABLES; 

-- 
-- Table structure for table `user` 
-- 

DROP TABLE IF EXISTS `user`; 
/*!40101 SET @saved_cs_client  = @@character_set_client */; 
/*!40101 SET character_set_client = utf8 */; 
CREATE TABLE `user` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `username` varchar(255) NOT NULL, 
    `password` varchar(255) NOT NULL, 
    `email` varchar(255) NOT NULL, 
    `created_at` datetime NOT NULL, 
    `updated_at` datetime NOT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `username` (`username`), 
    UNIQUE KEY `email` (`email`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 

-- 
-- Dumping data for table `user` 
-- 

LOCK TABLES `user` WRITE; 
/*!40000 ALTER TABLE `user` DISABLE KEYS */; 
INSERT INTO `user` VALUES (1,'mmmmmm','8476817631fd5ca37ebca97bb438c472','[email protected]','2010-04-21 02:32:52','2010-04-21 02:32:52'); 
/*!40000 ALTER TABLE `user` ENABLE KEYS */; 
UNLOCK TABLES; 
/*!40103 SET [email protected]_TIME_ZONE */; 

/*!40101 SET [email protected]_SQL_MODE */; 
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; 
/*!40014 SET [email protected]_UNIQUE_CHECKS */; 
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 
/*!40111 SET [email protected]_SQL_NOTES */; 

-- Dump completed on 2010-04-21 1:39:04 

任何援助將非常感激。提前致謝。

回答

4

MySQL的數據庫,表和字段名稱區分大小寫。該表在MySQL中稱爲「book」,但您的Doctrine定義有$this->setTableName('Book');,大寫B

+0

+1感謝您的回覆Marc B.我做出了您所建議的更改並且我的第一個查詢正常運行!但是,我在'字段列表'的錯誤仍然爲第二個和第三個查詢得到1054未知列'b.id',所以一些接縫與這些查詢錯誤... – Anthony 2010-04-24 06:16:43

+0

沒有看到其餘的SQL錯誤,很難說,但由於你沒有一個名爲'b'的'id'字段的表,也許Doctrine期望你的書桌有一個'id'字段,但是你有'book_id' 。 – 2010-04-24 16:25:40

+0

+1。你是正確的Marc B.我的doctrine_pi.php文件中的默認主鍵是「id」,所以我將它改爲「book_id」並刪除了這行:$ this-> hasColumn('book_id');從我的教條模式,它的工作。謝謝 – Anthony 2010-04-25 16:07:31