Home > Backend Development > PHP Tutorial > ThinkPHP关联查询有关问题

ThinkPHP关联查询有关问题

WBOY
Release: 2016-06-13 13:00:08
Original
823 people have browsed it

ThinkPHP关联查询问题
我用的是最新的2.0版 在MODEL中写好了关联定义
现在要在控制器里查数据
用手册里说的relation(true)方法但是提示错误:Model:relation您所请求的方法不存在

怎么回事呢?请帮我解答,谢谢大家啦
源码

关联定义(MODEL中)
public $_link =array(
    'lyType'=>array(
        'maping_type'=>HAS_ONE,
        'class_name'=>'lyType',
        'foregin_key'=>'typeId',
    ),


关联查询(控制器中)
$ly=new lyModel();
     $list=$ly->relation(true)->find(1);


错误提示:
Model:relation您所请求的方法不存在
------解决方案--------------------
$ly=new lyModel();
new LyModel()
------解决方案--------------------
class UserModel extends RelationModel

继承 RelationModel 类

<br />
/**<br />
     +----------------------<br />
     * 进行关联查询<br />
     +----------------------<br />
     * @access public<br />
     +----------------------<br />
     * @param mixed $name 关联名称<br />
     +----------------------<br />
     * @return Model <br />
     +----------------------<br />
     */<br />
    public function relation($name) {<br />
        $this->options['link']  =   $name;<br />
        return $this;<br />
    }<br />
<br />
Copy after login

------解决方案--------------------
1. 声明类

class NewsModel extends RelationModel
{

}

2. 查询

$News = D('News');
$data = $News->Relation(true)->find();
dump($data);
------解决方案--------------------
用D('News')或者m('New',‘RalationModel’)实例化

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template