开发者

Is the method Doctrine_Table::find() deprecated?

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 ,
  `countr开发者_运维技巧y_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)!


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)


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜