views:

420

answers:

2

Hi,

I had a problem with the method Doctrine_Table::find(), since it's thorowing an exception of SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

I solved the problem by using Doctrine::getTable('City')->findOneById($id); instead and it works fine.

When I tried to invistigate about the problem I was surprised since no documentation about the method Doctrine_Table::find() in the official website.

Any one knows what's the problem? is it deprecated? BTW it's exists on the actual code! of the version (1.2.1).

more info about the database:

CREATE  TABLE IF NOT EXISTS `country` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(64) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;


CREATE  TABLE IF NOT EXISTS `city` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(64) NOT NULL ,
  `country_id` INT NOT NULL ,
  PRIMARY KEY (`id`, `country_id`) ,
  INDEX `fk_city_country` (`country_id` ASC) ,
  CONSTRAINT `fk_city_country`
    FOREIGN KEY (`country_id` )
    REFERENCES `country` (`id` )
    ON DELETE CASCADE
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;

What's weird is that both Doctrine_Table::find(), and Doctrine_Table::findOneById() works fine on Country table!.

PS: I realize that Doctrine_Table::findOneById() is a __call() generated method. And that make me confused more, why the actual find() method can't behave as expected (is my expectation wrong or what)!

A: 

As of v 1.2.1, Doctrine_Table::find() is NOT deprecated You can check the official documentation on http://www.doctrine-project.org/documentation/manual/1_2/en/component-overview#table:finder-methods

As for the "invalid parameter number" error, it means you query has more or fewer parameters than expected, most often you used a token (?) and forgot to add the parameter to it

Doctrine_Query::create()
->from('User u')
->where('u.name = ?', 'Jonh')
->andWhere('u.is_active = ?')

The example i used have two tokens '?', but only one parameter 'jonh', it would throw the same error: "Invalid parameter number: number of bound variables does not match number of tokens"

Vitor Mello
In my case I'm passing one parameter which is the identified `id` of the `City`, so does it requires passing every primary key or what??
Omar Dolaimy
can you post the query youre using? it would help greatly :)
Vitor Mello
The problem-query is this one: `Doctrine::getTable('City')->find(1);` and it's written in my question.
Omar Dolaimy
A: 

Oh my bad. I didnt see it earlier, shame on me =p

Your table has two primary keys (id and country_id), so the find method requires you to pass both parameters to the find method.

You could instead use the magic methods:

Doctrine::getTable('City')->findOneById(1)
Vitor Mello
Thanks :), I thought this is the reason but it did not make sense to me... even now!.Anyway it's a design thing. So I can use the `Doctrine::getTable('City')->findOneById(1)` with no problem :).
Omar Dolaimy
I got it!, I made the mistake in designing the database in the first place!. Sorry a DB nope. I shouldn't set the foreign key as a primary...
Omar Dolaimy