0

我目前正在开发一个小型企业管理工具。我只是将其称为客户经理。我为客户、发票和付款创建了类。我希望能够在包含左连接字段的结果上使用 PDO::fetchClass 获取支付类对象。我试过了:

$sql = "SELECT * FROM payments LEFT OUTER JOIN ON payments.invoiceno = invoices.id";
    $table = $this->getTableList("Payment", $sql);

public function getDbObjects($class, $sql = false)
{

    if(!$sql) {
        $table = strtolower($class).'s';
        $sql = "SELECT * FROM $table";
    }

    $stmnt = $this->_databaseConnection->prepare($sql);
    if ($stmnt->execute()) {
        $objects = array();
        while ($object = $stmnt->fetchObject($class)) {
            var_dump($object);
            $objects[] = $object;
        }

        return $objects;
    }
    return FALSE;
}

我得到一个包含所有值的结果集,因此我将发票中的字段添加到付款中,但是当我对结果调用 fetchclass 时,付款中的字段未在返回的对象中设置。我已经确认它们在结果集中。这可能吗?我发现了相关的问题,但只是从连接中创建 2 个对象,而不是从连接中创建 1 个。

4

1 回答 1

0

我通过将我的功能代码直接复制到我的控制器类中来使它工作

 $sql = "SELECT * FROM payments LEFT OUTER JOIN invoices ON payments.invoiceno = invoices.id";
    //$table = $this->getTableList("Payment", $sql);
    $stmt = $this->_databaseConnection->query($sql);
    if ($stmt->execute()) {
        $objects = array();
        while ($object = $stmt->fetchObject('Payment')) {

            $objects[] = $object;
        }


    }
    $table = $this->getTableList($objects);


 public function getTableList($results)
{
// Fetch all payments from database and store in array
 $results = $this->getDbObjects($objects);

    // Initialize variables
    $colNames = array(); $head = ""; $body = "";

    // Loop over results to create table data (td) string
    foreach ($results as $result) {

        // Fetch associative array from results
        $row = $result->getListRow();

        // Initialize table row string
        $tableRow = "<tr>";

        // Gather column names for table header
        if( empty($colNames) ) $colNames = array_keys($row);

        //loop over columns to create html table data string
        foreach ($row as $td) {

            // Populate table data
            $tableRow .= "<td>$td</td>";

        }

        // Close table row
        $tableRow .= "</tr>";

        // Add row to table body
        $body .= $tableRow;
    }

    // Gather table header column names
    foreach ($colNames as $colName) {
        $head .= "<th>$colName</th>";
    }

    // Construct table
    return "<table><thead><tr>".$head."</tr></thead><tbody>".$body."</tbody></table>";
}
于 2013-06-13T03:02:36.073 回答