IndexAction.class.php:<?php
/**
 * 首页
 *
 */
 
class IndexAction extends Action {
public function index() {
header("Content-Type:text/html; charset=utf-8");

$featured = D('Products')->relation(true)->select();
var_dump($featured);
$this->assign('featured', $featured);
$this->display();
}
}
?>
IndexModel.class.php:<?php
/**
 * 首页
 *
 */
 
class IndexModel extends RelationModel {
protected $_link = array(
'auto_product_details' => array(
'mapping_type' => HAS_ONE,
'class_name' => 'auto_products',
'foreign_key' => 'productID'
)
);
}
?>
我的数据表:
auto_product_details:
auto_products:
提示错误信息:

解决方案 »

  1.   

    不好意思第二张图上传错了应该是这图:
    auto_products表:
      

  2.   

    就算IndexModel.class.php改成ProductsModel.class.php:<?php
    /**
     * 首页
     *
     */
     
    class ProductsModel extends RelationModel {
    protected $_link = array(
    'details' => array(
    'mapping_type' => HAS_ONE,
    'class_name' => 'auto_product_details',
    'mapping_name' => 'Details',
    'mapping_fields' => 'id,name,price,summary,isFeatured',
    'foreign_key' => 'productID'
    )
    );
    }
    ?>
    都不行
      

  3.   

    class NewsModel extends RelationModel{
    //关联分类表
    public $_link = array(
    ’profile’ => array(
    ’mapping_type’ => BELONGS_TO,
    ’class_name’ => ‘Ncat’,
    ’foreign_key’ => ‘cid’
    ),
    );
    }相关说明:首先要明确两者之间的关系,即每篇新闻都有一个分类,因此为BELONGS_TO;其次,有一点比较重要的是,这里的模型类的父类应该是 RelationModel 而不是 Model ,否则不会成功的;接下来,还有一点要注意,在新闻模块(NewsAction)中,注意查询时的实例化要用方法 D() 而不是 M()以上属于网上引用内容,希望对你有帮助