【问题标题】:Join issue: Unknown record property / related component加入问题:未知的记录属性/相关组件
【发布时间】:2011-09-21 06:03:16
【问题描述】:

我正在尝试加入两个表,但出现错误:

“项目”上的未知记录属性/相关组件“价格”

这是我的 DQL:

    $q = Doctrine_Query::create()
        ->select('i.id, i.product_id, p.price')
        ->from('Item i')
        ->innerJoin('i.priceItem p')
        ->where('i.id = ?', $request->getParameter('id'))
        ->andWhere('p.currency_code = ?', $request->getParameter('currency_code'));

    $this->item = $q->execute();

    foreach($this->item as $item) {

        var_dump($item->getId());
        var_dump($item->getProductId());
        var_dump($item->getPrice()); // This line is causing the error

    }

和架构:

物品: 充当: 时间戳:~ 列: 名称:{类型:字符串(255),notnull:真} product_id:{类型:字符串(255),notnull:真} 价格: 充当: 时间戳:~ 列: 货币代码:{类型:字符串(3),notnull:真} item_id:{类型:整数,notnull:真} 价格:{类型:浮动,notnull:真} 关系: 项目:{ onDelete:级联,本地:item_id,外国:id,foreignAlias:priceItem }

请告诉我我做错了什么?

【问题讨论】:

    标签: php symfony1 symfony-1.4


    【解决方案1】:

    尝试如下var_dump($item->Price->getPrice());

    getPrice() 是 Price 学说基类的属性,而不是 Item Class

    【讨论】:

    • “项目”上的未知记录属性/相关组件“价格”
    • @RomanNewaza var_dump($item->priceItem->getPrice());试试这个
    • $item->priceItem 是一个数组。我可以打印一些引用它的索引的东西:$item->priceItem[0]->getPrice()。如果我循环它,则为一行 Item 打印的所有值。由于 WHERE 中的 p.currency_code 应该只有一行,对吧?
    • 不要使用 Innerjoin 尝试在您的价格表中使用 leftJoin
    • INNER JOIN 只返回匹配的行,所以应该使用它。另外,尝试了 leftJoin - 同样的事情。如果它返回一条记录,我会感到惊讶
    【解决方案2】:

    pricePrice 对象的属性/字段,而不是 ItemItem 通过别名 priceItemPrice 相关。那么:

    $item->priceItem['price']
    

    更多信息可以在here找到。

    【讨论】:

    • 它现在返回多个价格,因为它是正确的连接 - 应该是一行
    • 尝试添加 typeforeignType 属性您的架构。关系:Item: { onDelete: CASCADE, type: one, foreignType: one, local: item_id, foreign: id, foreignAlias: priceItem }
    • 现在我有一个价格行,但错误的行 - 无论选择什么货币代码,它总是给我带来第一行。 SQL 是正确的 - 我监视 mysql.log 的查询。
    • 尝试手动设置主键:item_id: { type: integer, notnull: true, primary: true } currency_code: { type: string(3), notnull: true, primary: true }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多