views:

580

answers:

1

EDIT: The cause of the errors (see below): my default primary key in doctrine_pi.php was "id", so I changed it to "book_id" (my database's primary key) and it worked. Thanks Marc B.

Hi, I am using CI + Doctrine + MySQL and I am getting the following CRUD errors:

(1) When trying to create a new record in the database with this code:

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

I get this error:

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

(2) When trying to fetch a record from the database and display on my view page with this code:

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

I get this error:

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

And finally, when I try to update a record as follows:

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

I get a similar error:

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

Here is my Doctrine_Record model:

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

And finally, here is my 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','[email protected]','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

Any assistance will be really appreciated. Thanks in advance.

+3  A: 

MySQL's database, table, and field names are case-sensitive. The table is called "book" inside MySQL, but your Doctrine definition has $this->setTableName('Book'); with a capital B

Marc B
+1 thanks for your reply Marc B. I made the change you suggested and my first query worked! However, I'm getting the 1054 Unknown column 'b.id' in 'field list'' error still for the second and third queries so something seams to be wrong with those queries....
01010011
Without seeing the rest of the SQL error, it's hard to say, but since you don't have a table named 'b' with an 'id' field, perhaps Doctrine's expecting your book table to have an 'id' field, but you've got 'book_id' instead.
Marc B
+1. You are correct Marc B. The default primary key in my doctrine_pi.php file is "id" so I changed it to "book_id" and deleted the line: $this->hasColumn('book_id'); from my Doctrine model and it worked. Thanks
01010011