alphasion 2012-08-08 15:45:48 3831次浏览 2条回复 0 0 0
class UserRole extends CActiveRecord
{
    public function relations()
    {
        return array(
            'menu' => array(self::BELONGS_TO, 'Menu', 'menuId'),
            'user' => array(self::BELONGS_TO, 'User', 'userId'),
        );
    }
    public function attributeLabels()
    {
        return array(
            'userId' => 'User',
            'menuId' => 'Menu',
            'sel' => 'Sel',
            'ins' => 'Ins',
            'del' => 'Del',
            'upd' => 'Upd',
        );
    }
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria=new CDbCriteria;
        $criteria->compare('userId','='.Yii::app()->user->Id ,true,$operator='AND');
        $criteria->compare('sel','=1',true, $operator='OR');
        $criteria->compare('ins','=1',true, $operator='OR');
        $criteria->compare('del','=1',true, $operator='OR');
        $criteria->compare('upd','=1',true, $operator='OR');
        return new CActiveDataProvider($this->with('menu'), array(
            'criteria'=>$criteria,
            'pagination'=>FALSE,
        ));
    }
}

menu是菜单表.从LOG里面看生成的语句是

SELECT `t`.`userId` AS `t0_c0`, `t`.`menuId` AS `t0_c1`,
`t`.`sel` AS `t0_c2`, `t`.`ins` AS `t0_c3`, `t`.`del` AS `t0_c4`, `t`.`upd`
AS `t0_c5`, `menu`.`id` AS `t1_c0`, `menu`.`text` AS `t1_c1`, `menu`.`img`
AS `t1_c2`, `menu`.`controller` AS `t1_c3`, `menu`.`action` AS `t1_c4`,
`menu`.`order` AS `t1_c5`, `menu`.`enabled` AS `t1_c6`, `menu`.`rootid` AS
`t1_c7` FROM `vud_UserRole` `t`  LEFT OUTER JOIN `vud_Menu` `menu` ON
(`t`.`menuId`=`menu`.`id`)  WHERE (((((userId=:ycp0) OR (sel=:ycp1)) OR
(ins=:ycp2)) OR (del=:ycp3)) OR (upd=:ycp4))

直接带进参数执行,是可以得出正确结果的.但是在PHP代码里面就会报出下面的错误

Invalid argument supplied for foreach()

D:\Portal\Frameworks\framework\db\ar\CActiveFinder.php(841)

829     {
830         // determine the primary key value
831         if(is_string($this->_pkAlias))  // single key
832         {
833             if(isset($row[$this->_pkAlias]))
834                 $pk=$row[$this->_pkAlias];
835             else    // no matching related objects
836                 return null;
837         }
838         else // is_array, composite key
839         {
840             $pk=array();
841             foreach($this->_pkAlias as $name=>$alias)
842             {
843                 if(isset($row[$alias]))
844                     $pk[$name]=$row[$alias];
845                 else    // no matching related objects
846                     return null;
847             }
848             $pk=serialize($pk);
849         }
850 
851         // retrieve or populate the record according to the primary key value
852         if(isset($this->records[$pk]))
853             $record=$this->records[$pk];
Stack Trace
#0	
+  D:\Portal\Frameworks\framework\db\ar\CActiveFinder.php(819): CJoinElement->populateRecord(CJoinQuery, array("t0_c0" => "1", "t0_c1" => "1", "t0_c2" => "1", "t0_c3" => "1", ...))
#1	
+  D:\Portal\Frameworks\framework\db\ar\CActiveFinder.php(452): CJoinElement->runQuery(CJoinQuery)
#2	
+  D:\Portal\Frameworks\framework\db\ar\CActiveFinder.php(70): CJoinElement->find(CDbCriteria)
#3	
+  D:\Portal\Frameworks\framework\db\ar\CActiveRecord.php(1292): CActiveFinder->query(CDbCriteria, true)
#4	
+  D:\Portal\Frameworks\framework\db\ar\CActiveRecord.php(1406): CActiveRecord->query(CDbCriteria, true)
#5	
+  D:\Portal\Frameworks\framework\web\CActiveDataProvider.php(150): CActiveRecord->findAll(CDbCriteria)
#6	
+  D:\Portal\Frameworks\framework\web\CDataProvider.php(137): CActiveDataProvider->fetchData()
#7	
–  D:\Portal\php_work\voodooHVN\protected\views\layouts\main.php(31): CDataProvider->getData()
26         <?php echo $link=CHtml::link(CHtml::encode('Sign Out'),array('site/logout')) ?></div>
27         <?php
28             //$menu=Yii::app()->cache->get($m);
29             //if($menu===FALSE)
30             //{
31                 $model=UserRole::model()->search()->getData();
32                 echo count($model);
33             //}
34         ?>
您需要登录后才可以回复。登录 | 立即注册