【问题标题】:use joins in zend framework query在 zend 框架查询中使用连接
【发布时间】:2012-05-24 07:51:50
【问题描述】:

我想在 zend 中使用连接。以下是我的查询

 $select = $this->_db->select()
 ->from(array('evaluationcriteria' => 'procurement_tbltenderevaluationcriteria'),
 array('ScoringCriteriaID','ScoringCriteriaWeight'))
 ->join(array('scoringcriteria' => 'procurement_tbltenderscoringcriteria'),
                               'scoringcriteria.    TenderId=evaluationcriteria.TenderId')
 ->join(array('tenderapplications' => 'procurement_tbltenderapplications','tendersupplier' => 'tblsupplier'),
                               'tenderapplications. TenderInvitationContractorID=tendersupplier.UserID');

我在招标供应商表中有用户 ID。但它给出以下错误:-

找不到列:1054 'on 子句中的未知列 'tendersupplier.UserID'

【问题讨论】:

  • 您将tendersupplier 别名为tblsupplier

标签: zend-framework join


【解决方案1】:

我认为在同一个连接数组中包含多个故事不是正确的方法。 试试这样的代码..

->from(array('evaluationcriteria' => 'procurement_tbltenderevaluationcriteria'),
       array('ScoringCriteriaID','ScoringCriteriaWeight'))
->join(array('scoringcriteria' => 'procurement_tbltenderscoringcriteria'),
      'scoringcriteria.    TenderId=evaluationcriteria.TenderId')
      'scoringcriteria.    TenderId=evaluationcriteria.TenderId')
->join(array('tenderapplications' => 'procurement_tbltenderapplications'),
      'tenderapplications.TenderInvitationContractorID=tblsupplier.UserID');

我不确定您是否也打算从 tblsupplier 表中加入值。

【讨论】:

  • 给出错误 ---------- Unknown column 'tendersupplier.UserID'
  • @Navdeep:请重新检查您使用的是tblsupplier.UserID'还是tendersupplier.UserID。您必须使用 tblsupplier.UserID。
【解决方案2】:

where 条件的写法和你写的不一样。

我不认为 tblsupplier 是一个表,那么它应该在一个数组中

此代码未经测试!

   $select = $this->_db->select()
   ->from(array('evaluationcriteria' => 'procurement_tbltenderevaluationcriteria'),
    array('ScoringCriteriaID','ScoringCriteriaWeight'))
   ->join(array('scoringcriteria' => 'procurement_tbltenderscoringcriteria'),
                           'scoringcriteria.    TenderId=evaluationcriteria.TenderId')
    ->join(array('tenderapplications' =>  'procurement_tbltenderapplications'), array('tendersupplier' => 'tblsupplier'))
    ->where('tenderapplications. TenderInvitationContractorID=tendersupplier.UserID');

【讨论】:

  • 它给出错误 ---> Zend_Db_Select::join() 缺少参数 2。它在最后一次加入时给出错误
  • @Nacdeep,tblsupplier 是一张桌子吗?我怀疑这是一张桌子,我更新了答案
  • 是的,它是一张桌子。现在它给出错误--->'where子句'中的未知列'tendersupplier.UserID'
【解决方案3】:

看起来您正在尝试将 2 个表合二为一 -> 加入,我认为您不能这样做。

从 Zend_Db_Select() 加入代码

/**
     * Adds a JOIN table and columns to the query.
     *
     * The $name and $cols parameters follow the same logic
     * as described in the from() method.
     *
     * @param  array|string|Zend_Db_Expr $name The table name.
     * @param  string $cond Join on this condition.
     * @param  array|string $cols The columns to select from the joined table.
     * @param  string $schema The database name to specify, if any.
     * @return Zend_Db_Select This Zend_Db_Select object.
     */
    public function join($name, $cond, $cols = self::SQL_WILDCARD, $schema = null)
    {
        return $this->joinInner($name, $cond, $cols, $schema);
    }

这是 from() 的注释块

/**
     * Adds a FROM table and optional columns to the query.
     *
     * The first parameter $name can be a simple string, in which case the
     * correlation name is generated automatically.  If you want to specify
     * the correlation name, the first parameter must be an associative
     * array in which the key is the correlation name, and the value is
     * the physical table name.  For example, array('alias' => 'table').
     * The correlation name is prepended to all columns fetched for this
     * table.
     *
     * The second parameter can be a single string or Zend_Db_Expr object,
     * or else an array of strings or Zend_Db_Expr objects.
     *
     * The first parameter can be null or an empty string, in which case
     * no correlation name is generated or prepended to the columns named
     * in the second parameter.
     *
     * @param  array|string|Zend_Db_Expr $name The table name or an associative array
     *                                         relating correlation name to table name.
     * @param  array|string|Zend_Db_Expr $cols The columns to select from this table.
     * @param  string $schema The schema name to specify, if any.
     * @return Zend_Db_Select This Zend_Db_Select object.
     */

也许可以试试这样的方法:

 $select = $this->_db->select()
 //FROM table procurement_tbltenderevaluationcriteria AS evaluationcriteria, SELECT FROM
 //COLUMNS ScoringCriteriaID and ScoringCriteriaWeight
 ->from(array('evaluationcriteria' => 'procurement_tbltenderevaluationcriteria'),
 array('ScoringCriteriaID','ScoringCriteriaWeight'))
 //JOIN TABLE procurement_tbltenderscoringcriteria AS scoringcriteria WHERE 
 //TenderId FROM TABLE scoringcriteria == TenderId FROM TABLE evaluationcriteria
 ->join(array('scoringcriteria' => 'procurement_tbltenderscoringcriteria'),
                 'scoringcriteria.TenderId=evaluationcriteria.TenderId')
 //JOIN TABLE procurement_tbltenderapplications AS tenderapplications
 ->join(array('tenderapplications' => 'procurement_tbltenderapplications'))
 //JOIN TABLE tblsupplier AS tendersupplier WHERE TenderInvitationContractorID FROM TABLE 
 // tenderapplications == UserID FROM TABLE tendersupplier
 ->join(array('tendersupplier' => 'tblsupplier'),
   'tenderapplications.TenderInvitationContractorID=tendersupplier.UserID');

you may also need to alter your select() definition to allow joins:

//this will lock the tables to prevent data corruption
$this->_db->select(Zend_Db_Table::SELECT_WITHOUT_FROM_PART)->setIntegrityCheck(FALSE);

我希望我正确地阅读了您的意图,如果不是一直到那里,这应该会让您更接近。 (一个提示,使用较短的别名...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多