Doctrine CodeIgniter MySQL CRUD错误

时间:2010-04-23 22:02:08

标签: mysql codeigniter doctrine crud mysql-error-1054

编辑:错误的原因(见下文):我在doctrine_pi.php中的默认主键是“id”,因此我将其更改为“book_id”(我的数据库的主键)并且它有效。谢谢Marc B。

您好, 我正在使用CI + Doctrine + MySQL,我收到以下CRUD错误:

(1)尝试使用以下代码在数据库中创建新记录时:

  

$ book_title ='伯罗奔尼撒战争';
     $ b =新书();
    $ b-> title = $ book_title;
    $ b-> price = 10.50;
    $ B->保存();

我收到此错误:

  

致命错误:未捕获的exeption'Doctrine_Connection_Mysql_Exception',消息'SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'title'...

(2)尝试从数据库中获取记录并使用以下代码显示在我的视图页面时:

  

$ book_title ='伯罗奔尼撒战争';
  $ title = $ book_title;
  $ search_results = Doctrine :: getTable('Book') - > findOneByTitle($ title);
  echo $ search_results-> title; //(在视图文件中)

我收到此错误:

  

致命错误:未捕获异常'Doctrine_Connection_Mysql_Exception',消息'SQLSTATE [45S22]:未找到列:1054'字段列表中的未知列'b.id'...

最后,当我尝试按如下方式更新记录时:

  

$ book_title ='伯罗奔尼撒战争';
  $ title = $ book_title;
  $ u = Doctrine :: getTable('Book') - > find($ title);
  $ u-> title = $ title;
  $ U->保存();

我得到了类似的错误:

  

致命错误:未捕获的异常   'Doctrine_Connection_Mysql_Exception'   消息'SQLSTATE [42S22]:列   找不到:1054未知栏'b.id'   在“字段列表”中...

这是我的Doctrine_Record模型:

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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@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','k@w.com','2010-04-21 02:32:52','2010-04-21 02:32:52');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

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

我们非常感谢您的帮助。提前致谢。

1 个答案:

答案 0 :(得分:4)

MySQL的数据库,表和字段名称区分大小写。该表在MySQL中称为“book”,但您的Doctrine定义为$this->setTableName('Book');且大写为B