Symfony1/Doctrine:SQLSTATE[HY093]:参数编号无效:绑定变量的数量与标记的数量不匹配

Symfony1/Doctrine : SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

实际上 Doctrine_Query 我遇到了问题。 它是一个 Symfony 1 项目 (1.4.20)。

这是最后的错误消息:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

这是构建查询的代码:

public function search ($keywords_article, $lang, $keywords_produit = '', $limit = 0, $offset = 0)
{
    $q = $this->createQuery('a')
        ->innerJoin('a.FlatSearchArticles fa')
        ->innerJoin('a.Translation t')
        ->where('t.lang = ?', $lang)
        ->addWhere('fa.lang = ?', $lang)
        ->addWhere('fa.reference LIKE "%?%" Or fa.nom LIKE "%?%"', array($keywords_article, $keywords_article));

    if ('' != $keywords_produit)
        $q->innerJoin('fa.Produits fp')
            ->addWhere('fp.lang = ?', $lang)
            ->addWhere('fp.reference LIKE "%?%" Or fp.nom LIKE "%?%"', array($keywords_produit, $keywords_produit));

    if (0 != $limit)
        $q->limit($limit);

    if (0 != $offset)
        $q->offset($offset);

    return $q->execute();
}

堆栈跟踪:

1. at ()
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php line 1082 ...
2. at Doctrine_Connection->rethrowException(object('PDOException'), object('Doctrine_Connection_Statement'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/Statement.php line 269 ...
3. at Doctrine_Connection_Statement->execute(array('fr', 'fr', 'ALLUM', 'ALLUM', 'fr', 'IDRA', 'IDRA'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php line 1006 ...
4. at Doctrine_Connection->execute('SELECT i.id AS i__id, i.reference AS i__reference, i.art_code AS i__art_code, i.commentaire AS i__commentaire, i.image AS i__image, i.marque_id AS i__marque_id, i.article_type_id AS i__article_type_id, i.reference_fournisseur AS i__reference_fournisseur, i.historique AS i__historique, i.article_id AS i__article_id, i.reference_client AS i__reference_client, i.domaine_id AS i__domaine_id, i.migration_id AS i__migration_id, i.date_validite AS i__date_validite, i.list_pays AS i__list_pays, i.type AS i__type, i.created_at AS i__created_at, i.updated_at AS i__updated_at, f.id AS f__id, f.reference AS f__reference, f.nom AS f__nom, f.lang AS f__lang, f.article_id AS f__article_id, i2.id AS i2__id, i2.nom AS i2__nom, i2.lang AS i2__lang, f2.id AS f2__id, f2.reference AS f2__reference, f2.nom AS f2__nom, f2.lang AS f2__lang, f2.produit_id AS f2__produit_id, f2.domaine_id AS f2__domaine_id, f2.marque_id AS f2__marque_id FROM item i INNER JOIN flat_search_article f ON i.id = f.article_id INNER JOIN item_translation i2 ON i.id = i2.id INNER JOIN flat_search_article_produit f3 ON (f.id = f3.article_id) INNER JOIN flat_search_produit f2 ON f2.id = f3.produit_id WHERE (i2.lang = ? AND f.lang = ? AND (f.reference LIKE "%?%" OR f.nom LIKE "%?%") AND f2.lang = ? AND (f2.reference LIKE "%?%" OR f2.nom LIKE "%?%") AND (i.type = 1))', array('fr', 'fr', 'ALLUM', 'ALLUM', 'fr', 'IDRA', 'IDRA'))
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Abstract.php line 983 ...
5. at Doctrine_Query_Abstract->_execute(array())
in SF_SYMFONY_LIB_DIR/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Abstract.php line 1033 ...
6. at Doctrine_Query_Abstract->execute()
in SF_ROOT_DIR/lib/model/doctrine/ArticleTable.class.php line 90 ...

当我在 mysql 控制台客户端中尝试相同的查询时,它有效:

mysql> SELECT i.id AS i__id, i.reference AS i__reference, i.art_code AS i__art_code, i.commentaire AS i__commentaire, i.image AS i__image, i.marque_id AS i__marque_id, i.article_type_id AS i__article_type_id, i.reference_fournisseur AS i__reference_fournisseur, i.historique AS i__historique, i.article_id AS i__article_id, i.reference_client AS i__reference_client, i.domaine_id AS i__domaine_id, i.migration_id AS i__migration_id, i.date_validite AS i__date_validite, i.list_pays AS i__list_pays, i.type AS i__type, i.created_at AS i__created_at, i.updated_at AS i__updated_at, f.id AS f__id, f.reference AS f__reference, f.nom AS f__nom, f.lang AS f__lang, f.article_id AS f__article_id, i2.id AS i2__id, i2.nom AS i2__nom, i2.lang AS i2__lang, f2.id AS f2__id, f2.reference AS f2__reference, f2.nom AS f2__nom, f2.lang AS f2__lang, f2.produit_id AS f2__produit_id, f2.domaine_id AS f2__domaine_id, f2.marque_id AS f2__marque_id FROM item i INNER JOIN flat_search_article f ON i.id = f.article_id INNER JOIN item_translation i2 ON i.id = i2.id INNER JOIN flat_search_article_produit f3 ON (f.id = f3.article_id) INNER JOIN flat_search_produit f2 ON f2.id = f3.produit_id WHERE (i2.lang = 'fr' AND f.lang = 'fr' AND (f.reference LIKE "%ALLUM%" OR f.nom LIKE "%ALLUM%") AND f2.lang = 'fr' AND (f2.reference LIKE "%IDRA%" OR f2.nom LIKE "%IDRA%") AND (i.type = 1));
***/***
35 rows in set (0.00 sec)

我不明白为什么 Symfony1/Doctrine/PHP Pdo 没有 "like" 我的查询。

感谢您的支持, 大卫.

? 不是此中的标记:

->addWhere('fa.reference LIKE "%?%" Or fa.nom LIKE "%?%"', array($keywords_article, $keywords_article));

而是这样传递:

->addWhere('fa.reference LIKE ? Or fa.nom LIKE ?', array('%' . $keywords_article . '%', '%' . $keywords_article . '%'));

我身边找到的另一个解决方案:

public function search ($keywords_article, $lang, $keywords_produit = '', $limit = 0, $offset = 0)
{
    $q = $this->createQuery('a')
        ->innerJoin('a.FlatSearchArticles fa')
        ->innerJoin('a.Translation t')
        ->where('t.lang = :lang')
        ->addWhere('fa.lang = :lang')
        ->addWhere('fa.reference LIKE "%:kwa%" OR fa.nom LIKE "%:kwa%"');

    if ('' != $keywords_produit)
        $q->innerJoin('fa.FlatSearchProduits fp')
            ->addWhere('fp.lang = :lang')
            ->addWhere('fp.reference LIKE "%:kwp%" OR fp.nom LIKE "%:kwp%"');

    if (0 != $limit)
        $q->limit($limit);

    if (0 != $offset)
        $q->offset($offset);

    return $q->execute(array(
        ':lang' => $lang,
        ':kwa' => $keywords_article,
        ':kwp' => $keywords_produit,
    ));
}

我最终更喜欢使用“:key”。

感谢 Marek 的回答,这个回答也很好用。

再见, 大卫.