pinsn 2012-07-25 10:52:40 4093次浏览 19条回复 0 0 0

控制器中admin方法怎么做联合查询

A表 id productname author 1 test haha@haha.com 2 test1 haha1@haha1.com 3 test1 haha1@haha1.com B表 id name author 1 name1 haha@haha.com 2 name2 haha1@haha1.com

当 haha@haha.com 登录时 得到表A.author = B.author (haha@haha.com) 的 两个表所有信息?

  • 回复于 2012-07-25 13:22 举报

    消灭0回复。。嘎嘎。

  • 回复于 2012-07-25 13:46 举报
    $criteria = new CDbCriteria;
    $criteria->condition = "where 条件";
    
  • 回复于 2012-07-25 15:07 举报

    呵呵,再追问一下,where 条件怎么写? 比如 A.id = B.eid and author= Yii->app()->user->username

  • 回复于 2012-07-25 15:20 举报

    就这样写,注意语句的符号规范就可以了

  • 回复于 2012-07-25 15:38 举报

    $criteria->condition = "where name='事件名称'";

    我在admin方法中加上where 条件 不过滤

  • 回复于 2012-07-25 16:06 举报
    $criteria->condition = "where name='事件名称'";
    应该改为$criteria->condition = "`name`='事件名称'";
    
  • 回复于 2012-07-25 20:00 举报

    也可以在A表中添加relation,
    用has_manay

  • 回复于 2012-07-26 09:18 举报

    话说这个关系型怎么用?多对多?

  • 回复于 2012-07-26 09:25 举报

    我也不明白,昨天的问题还没有解决,根椐你的方法还没有解决呢,愁死

  • 回复于 2012-07-26 10:19 举报

    你的需求不是想添加一个where条件么?

  • 回复于 2012-07-26 12:01 举报

    这两个表有关系么?

  • 回复于 2012-07-26 14:38 举报

    是的就添加上面的where 条件 ,我也加上了可是不起作用

  • 回复于 2012-07-26 14:38 举报

    有哇 你看我贴子中列出的where 条件

  • 回复于 2012-07-26 15:05 举报

    贴代码,或错误信息。我不知道你表之间是否有定义关系,或者你用 $criteria->addCondition("t.字段 = ".数据);

  • 回复于 2012-07-26 16:00 举报
    public function actionProcessAdmin()
    {
    	//$dataObject=A::model()->getAll(Yii::app()->user->username);
    	$model = new A();
    	$criteria = new CDbCriteria;
    	//$criteria->condition = "id = 1";
    
    	if(isset($_GET['A']))
    		$model->attributes=$_GET['Event'];
    	$this->render('admin',array(
    			'model'=>$model,
    	));
    }
    

    没有错误信息,全部读出来了

  • 回复于 2012-07-26 16:04 举报

    你还是说说你的具体需求吧。第一个表,大概可以看出是用户购买物品的数据表,第二个表是啥意思?用户的个人数据?第二个表中的name指啥?如果是用户名的话,就表1BELONGS_TO表2,或者表2has_manay表1...
    还有,你直接写sql语句执行下,看能否得到你想要的,然后再在yii中修改where语句。。。
    我总觉得你的这两个表没啥关系。。。囧

  • 回复于 2012-07-26 16:16 举报
    $dataObject=Event::model()->findBySql("SELECT * FROM A,B where A.author =B.author and B.author='haha@haha.com'");
    

    这样能得到我想要的信息 ,但有一个问题,当B表中没有记录了 就返回NULL 页页空白

  • 回复于 2012-07-26 17:06 举报

    where语句写不出你需要的这个效果,至少我写不出。MANY_MANY,我木用过,还不会用。。。等待高手。

    另外。你写的那个sql,A B中任何一个表为空,得到的数据都是空。笛卡尔积。。。

    建议你说说你的需求,让高手给你指点一个方法来做。。

  • 回复于 2012-07-26 19:58 举报

    实际上我觉得你没说清楚你的条件,照你上面给出的例子看,A表为产品表,里面的author为用户id,一个用户可以有多个productname;B表你没说清楚是什么,因为你只列了2个,并且都不同,我可以认为你是用户表,一个用户只有一行记录,所以这样的话关系就很明显了,A与B的关系为一对多(我上面没分析清楚),那么你应该在B表中添加relation,用has_manay;在A表中添加relation,用belongs_to

您需要登录后才可以回复。登录 | 立即注册