ThinkPHP关联查询

qq561887370 2011-04-20 07:34:18
SELECT a.bulletinId,a.title,a.content,b.userName,a.createTime FROM sp_Bulletin a left outer join sp_user b on a.userid=b.userId
ThinkPHP里面怎么写?
(普通查询然后分页)$list=$Bulletin->limit($p->firstRow.','.$p->listRows)->order('bulletinId asc')->findAll();
得到的是
protected 'queryStr' => string 'SELECT * FROM `sp_bulletin` ORDER BY bulletinId asc LIMIT 30,5 ' (length=63)
有人推荐用realtion做,在自己尝试的写了一个
但是不成功
我发一下我的Model类
BulletinModel.class.php
<?php
class BulletinModel extends Model
{
protected $_validate =array(
array('title','require','标题必须!',1),
array('content','require','内容必须'),
array('title','','标题已经存在',0,'unique',self::MODEL_INSERT),
);
protected $_auto=array(
array('userId','1',self::MODEL_INSERT),
array('createTime','time',1,'function'),
);
}
?>
UserModel.class.php
<?php
class UserModel extends Model{
}
?>

求帮助,
...全文
716 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
宰相秋水 2011-05-03
  • 打赏
  • 举报
回复
或者到我的博客看一下吧
宰相秋水 2011-05-03
  • 打赏
  • 举报
回复
三个数据表的关联查询

$M_shopping = M('Shops');

$M_product = M('Product');

$M_proimg = M('Product_image');

$list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id left join hr_product_image as productimg on productimg.p_id = product.p_id')->field('productimg.pi_url,product.p_id,product.p_name,shops.product_amount,shops.product_id,product.am_id,product.p_procolor,product.p_price,product_amount*p_price as totalone')->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'")->group('shops.id')->select();

数据表的查询条件

1:下面的是直接吧查询的条件放到了where中,这样就方便了条件的书写

$m_test = M("Product");

$productmeaage = $m_test->where("p_id='$proid'")->select();

2:除了上面的方法还有一种是以数组的方式

$M_product = M('Product');

$map['pid'] = $proid;

$p_result = $M_product->where($map)->select();
jlokys 2011-04-20
  • 打赏
  • 举报
回复
$model = new Model();

$model->query($sql);

可以用query()执行原生态的sql 语句

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧