0

Here is my module controller in which one foreign is working fine while failed to get result from Foreign key....

<?php

    class UserCourseController extends Controller
    {
        public function actionIndex()
        {
        $this->render('index');
        } 
         public function actionGetUserCourse()
                {
                    if(isset($_POST['user_id']) && !empty($_POST['user_id']))
                    {
                         $response = array();
                         $userId = $_POST['user_id'];

                        $courseRecord = UserCourseModelNew::model()->findAll('user_id=:userId',array(':userId'=>$userId));

                        if($courseRecord){
                        foreach($courseRecord as $rec){
                           $rec->user_id=$rec->user->user_name;
                          $rec->course_id=$rec->course->course_name;   
                          }
                          $response['status'] = true;
                          $response['message']="Record Found";
                          $response['user_course']=$courseRecord;

                  }
                  else {
                         $response['status'] = false;
                         $response['message']="Record not Found";

                  }
                         echo CJSON::encode($response);

                    }
         else {
             echo "error found";
         }
                }

        }

There are two same lines but one is working fine while another one is throwing errors:

    $rec->user_id=$rec->user->user_name;
    $rec->course_id=$rec->course->course_name;  

This line is creating problem and giving me error when i comment first line then no error i testing in form here is the error list while there is no Users.php class in my whole project so how this error coming*ERROR:*

 PHP warning

include(Users.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

C:\wamp\www\yii-1.1.8.r3324\framework\YiiBase.php(421)

409                         {
410                             include($classFile);
411                             if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
412                                 throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
413                                     '{class}'=>$className,
414                                     '{file}'=>$classFile,
415                                 )));
416                             break;
417                         }
418                     }
419                 }
420                 else
421                     include($className.'.php');
422             }
423             else  // class name with namespace in PHP 5.3
424             {
425                 $namespace=str_replace('\\','.',ltrim($className,'\\'));
426                 if(($path=self::getPathOfAlias($namespace))!==false)
427                     include($path.'.php');
428                 else
429                     return false;
430             }
431             return class_exists($className,false) || interface_exists($className,false);
432         }
433         return true;
Stack Trace
#0  
+  C:\wamp\www\yii-1.1.8.r3324\framework\YiiBase.php(421): YiiBase::autoload()
#1  
 unknown(0): YiiBase::autoload("Users")
#2  
+  C:\wamp\www\yii-1.1.8.r3324\framework\db\ar\CActiveRecord.php(386): spl_autoload_call("Users")
#3  
+  C:\wamp\www\yii-1.1.8.r3324\framework\db\ar\CActiveFinder.php(215): CActiveRecord::model("Users")
#4  
+  C:\wamp\www\yii-1.1.8.r3324\framework\db\ar\CActiveFinder.php(48): CActiveFinder->buildJoinTree(CJoinElement, "user")
#5  
+  C:\wamp\www\yii-1.1.8.r3324\framework\db\ar\CActiveRecord.php(271): CActiveFinder->__construct(UserCourseModelNew, "user")
#6  
–  C:\wamp\www\yii-1.1.8.r3324\framework\db\ar\CActiveRecord.php(141): CActiveRecord->getRelated("user")
136         elseif(isset($this->getMetaData()->columns[$name]))
137             return null;
138         elseif(isset($this->_related[$name]))
139             return $this->_related[$name];
140         elseif(isset($this->getMetaData()->relations[$name]))
141             return $this->getRelated($name);
142         else
143             return parent::__get($name);
144     }
145 
146     /**
#7  
–  C:\wamp\www\examapp\protected\modules\services\controllers\UserCourseController.php(52): CActiveRecord->__get("user")
47      echo "<PRE>";
48     // echo $rec->user->id; exit();
49 print_r($courseRecord);                    
50 //print_r("$rec:"+$rec->course->course_name);      
51                    
52                   $rec->user_id=$rec->user->id;
53                   $rec->course_id=$rec->course->course_name;   
54                   
55                   
56                 }
57                   $response['status'] = true;
#8  
–  C:\wamp\www\yii-1.1.8.r3324\framework\web\actions\CInlineAction.php(49): UserCourseController->actionGetUserCourse()
44         $controller=$this->getController();
45         $method=new ReflectionMethod($controller, $methodName);
46         if($method->getNumberOfParameters()>0)
47             return $this->runWithParamsInternal($controller, $method, $params);
48         else
49             return $controller->$methodName();
50     }
51 
52 }
#9  
–  C:\wamp\www\yii-1.1.8.r3324\framework\web\CController.php(308): CInlineAction->runWithParams(array())
303     {
304         $priorAction=$this->_action;
305         $this->_action=$action;
306         if($this->beforeAction($action))
307         {
308             if($action->runWithParams($this->getActionParams())===false)
309                 $this->invalidActionParams($action);
310             else
311                 $this->afterAction($action);
312         }
313         $this->_action=$priorAction;
#10 
+  C:\wamp\www\yii-1.1.8.r3324\framework\web\CController.php(286): CController->runAction(CInlineAction)
#11 
+  C:\wamp\www\yii-1.1.8.r3324\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())
#12 
+  C:\wamp\www\yii-1.1.8.r3324\framework\web\CWebApplication.php(282): CController->run("GetUserCourse")
#13 
+  C:\wamp\www\yii-1.1.8.r3324\framework\web\CWebApplication.php(141): CWebApplication->runController("services/UserCourse/GetUserCourse")
#14 
+  C:\wamp\www\yii-1.1.8.r3324\framework\base\CApplication.php(169): CWebApplication->processRequest()
#15 
–  C:\wamp\www\examapp\index.php(13): CApplication->run()
08 defined('YII_DEBUG') or define('YII_DEBUG',true);
09 // specify how many levels of call stack should be shown in each log message
10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
11 
12 require_once($yii);
13 Yii::createWebApplication($config)->run();

UsersCourseModelNew.php:

 <?php

/**
 * This is the model class for table "user_course".
 *
 * The followings are the available columns in table 'user_course':
 * @property integer $id
 * @property integer $user_id
 * @property integer $course_id
 * @property string $status
 *
 * The followings are the available model relations:
 * @property Course $course
 * @property Users $user
 */

    class UserCourseModelNew extends CActiveRecord
    {
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }
        public function tableName()
        {
            return 'user_course';
        }
        public function rules()
        {

            return array(
                array('user_id, course_id', 'required'),
                array('user_id, course_id', 'numerical', 'integerOnly'=>true),
                array('status', 'length', 'max'=>8),
                array('id, user_id, course_id, status', 'safe', 'on'=>'search'),
            );
        }
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'course' => array(self::BELONGS_TO, 'Course', 'course_id'),
                'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
            );
        }
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'user_id' => 'User',
                'course_id' => 'Course',
                'status' => 'Status',
            );
        }
        public function search()
        {
            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id);
            $criteria->compare('user_id',$this->user_id);
            $criteria->compare('course_id',$this->course_id);
            $criteria->compare('status',$this->status,true);

            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }
    }

Edit: UsersModel.php

    <?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $user_name
 * @property string $password
 * @property string $date_created
 * @property string $date_modified
 * @property string $user_type
 * @property string $mobile_no
 * @property integer $required
 * @property integer $role_id
 * @property string $status
 * @property string $first_name
 * @property string $last_name
 * @property string $gender
 * @property string $religon
 * @property string $phone_no
 * @property string $email
 *
 * The followings are the available model relations:
 * @property QuestionBank[] $questionBanks
 * @property QuestionReplyAnswer[] $questionReplyAnswers
 * @property UserCourse[] $userCourses
 * @property UserSchedule[] $userSchedules
 * @property Role $role
 */
class UsersModel extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return UsersModel the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_name, password, date_created, date_modified, user_type, mobile_no', 'required'),
            array('required, role_id', 'numerical', 'integerOnly'=>true),
            array('user_type', 'length', 'max'=>7),
            array('status', 'length', 'max'=>8),
            array('first_name, last_name', 'length', 'max'=>255),
            array('gender', 'length', 'max'=>6),
            array('religon', 'length', 'max'=>10),
            array('phone_no, email', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, user_name, password, date_created, date_modified, user_type, mobile_no, required, role_id, status, first_name, last_name, gender, religon, phone_no, email', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'questionBanks' => array(self::HAS_MANY, 'QuestionBank', 'user_id'),
            'questionReplyAnswers' => array(self::HAS_MANY, 'QuestionReplyAnswer', 'user_id'),
            'userCourses' => array(self::HAS_MANY, 'UserCourse', 'user_id'),
            'userSchedules' => array(self::HAS_MANY, 'UserSchedule', 'user_id'),
            'role' => array(self::BELONGS_TO, 'Role', 'role_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'user_name' => 'User Name',
            'password' => 'Password',
            'date_created' => 'Date Created',
            'date_modified' => 'Date Modified',
            'user_type' => 'User Type',
            'mobile_no' => 'Mobile No',
            'required' => 'Required',
            'role_id' => 'Role',
            'status' => 'Status',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'gender' => 'Gender',
            'religon' => 'Religon',
            'phone_no' => 'Phone No',
            'email' => 'Email',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('user_name',$this->user_name,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('date_created',$this->date_created,true);
        $criteria->compare('date_modified',$this->date_modified,true);
        $criteria->compare('user_type',$this->user_type,true);
        $criteria->compare('mobile_no',$this->mobile_no,true);
        $criteria->compare('required',$this->required);
        $criteria->compare('role_id',$this->role_id);
        $criteria->compare('status',$this->status,true);
        $criteria->compare('first_name',$this->first_name,true);
        $criteria->compare('last_name',$this->last_name,true);
        $criteria->compare('gender',$this->gender,true);
        $criteria->compare('religon',$this->religon,true);
        $criteria->compare('phone_no',$this->phone_no,true);
        $criteria->compare('email',$this->email,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}
10
  • check the relations of the UserCourseModelNew activerecord class... I'm sure you'd find a reference to a 'Users' model in there... Feb 20, 2014 at 10:31
  • can you add your model code also
    – Kumar V
    Feb 20, 2014 at 10:40
  • @ragingprodigy public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'course' => array(self::BELONGS_TO, 'Course', 'course_id'), 'user' => array(self::BELONGS_TO, 'Users', 'user_id'), ); } Feb 20, 2014 at 10:54
  • this is the relation there is no Users model there Feb 20, 2014 at 10:55
  • @kumar_v can u please check now i have added model code also its wasted my whole day and still i am failed to correct this pls check it Feb 20, 2014 at 11:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.