FinbByPk Trouble with alias

This problem has small description here

http://code.google.com/p/yii/issues/detail?id=1207

But after erase all assets problem do not repeat.

But i spend 2 hours for find where is bug and i hope yii team can say whats up?

My full description with examples:

Models




class Project extends CActiveRecord{

  public function relations(){

    return array('seo'=> array(self::BELONGS_TO, 'User', 'seoid', 'alias'=>'seo'),)

  }

}

class User extends CActiveRecord{

//Simple  class for example


}


class TestController extends CController{

  public function actionShow(){

     //Work fine

//     $prj=Project::model()->findByPk(intval($_GET['projectid']) );

     //Doesnot work 

     $prj=Project::model()->with('seo')->findByPk(intval($_GET['projectid']) );

  }

}



And full stack trace




Stack trace:

#0 C:\xampp\htdocs\yii\framework\db\CDbCommand.php(265): CDbCommand->queryInternal('fetchAll', 2, Array)

#1 C:\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php(739): CDbCommand->queryAll()

#2 C:\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php(413): CJoinElement->runQuery(Object(CJoinQuery))

#3 C:\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php(85): CJoinElement->find(Object(CDbCriteria))

#4 C:\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php(143): CActiveFinder->query(Object(CDbCriteria))

#5 C:\xampp\htdocs\sp\protected\controllers\TodoController.php(425): CActiveFinder->findByAttributes(Array)

#6 C:\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(32): TodoController->actionAutoCompleteLookup()

#7 C:\xampp\htdocs\yii\framework\web\CController.php(300): CInlineAction->run()

#8 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(129): CController->runAction(Object(CInlineAction))

#9 C:\xampp\htdocs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#10 C:\xampp\htdocs\yii\framework\web\CController.php(999): CFilter->filter(Object(CFilterChain))

#11 C:\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#12 C:\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))

#13 C:\xampp\htdocs\yii\framework\web\CController.php(283): CFilterChain->run()

#14 C:\xampp\htdocs\yii\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#15 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(320): CController->run('AutoCompleteLoo...')

#16 C:\xampp\htdocs\yii\framework\web\CWebApplication.php(120): CWebApplication->runController('todo/AutoComple...')

#17 C:\xampp\htdocs\yii\framework\base\CApplication.php(135): CWebApplication->processRequest()

#18 C:\xampp\htdocs\sp\index.php(15): CApplication->run()

#19 {main} REQUEST_URI=/index.php?r=test/show&q=aasdfds&limit=10&timestamp=1273656577867&type=worker&projectid=262

2010/05/12 16:50:09 [error] [system.db.CDbCommand] Error in querying SQL: SELECT "project"."title" AS "t0_c0", "project"."budget_max" AS "t0_c1", "project"."budget_current" AS "t0_c2", "project"."cost" AS "t0_c3", "project"."site" AS "t0_c4", "project"."salesid" AS "t0_c5", "project"."seoid" AS "t0_c6", "project"."analyticid" AS "t0_c7", "project"."stausdescription" AS "t0_c8", "project"."status" AS "t0_c9", "project"."created" AS "t0_c10", "project"."id" AS "t0_c11", "project"."clientid" AS "t0_c12", "project"."intelsiblink" AS "t0_c13", "project"."paytime" AS "t0_c14", "project"."starttime" AS "t0_c15", "project"."debt" AS "t0_c16", "project"."pointsnow" AS "t0_c17", "project"."pointslast" AS "t0_c18", "project"."host_pwd" AS "t0_c19", "project"."host_usr" AS "t0_c20", "project"."ssh_pwd" AS "t0_c21", "project"."ssh_usr" AS "t0_c22", "project"."bd_pwd" AS "t0_c23", "project"."bd_usr" AS "t0_c24", "project"."adm_pwd" AS "t0_c25", "project"."adm_usr" AS "t0_c26", "project"."ftp_pwd" AS "t0_c27", "project"."ftp_usr" AS "t0_c28", "project"."host_server" AS "t0_c29", "project"."ssh_server" AS "t0_c30", "project"."ftp_server" AS "t0_c31", "project"."adm_server" AS "t0_c32", "project"."type" AS "t0_c33", "project"."top10" AS "t0_c34", "project"."top5" AS "t0_c35", "project"."top3" AS "t0_c36", "project"."state" AS "t0_c37", "project"."prompt_days" AS "t0_c38", "project"."prompt_status" AS "t0_c39", "project"."prompt_viewed" AS "t0_c40", "project"."show_cost" AS "t0_c41", "project"."partnerid" AS "t0_c42", "project"."copyrighterid" AS "t0_c43", "seo"."id" AS "t1_c0", "seo"."username" AS "t1_c1", "seo"."password" AS "t1_c2", "seo"."status" AS "t1_c3" FROM "Project" "project"  LEFT OUTER JOIN "User" "seo" ON ("project"."seoid"="seo"."id") WHERE ("t"."id"=:yp0). Bind with parameter :yp0=262

2010/05/12 16:50:09 [error] [exception.CDbException] exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[42P01]: Undefined table: 7 ERROR:  missing FROM-clause entry for table "t"

LINE 1: ...r" "seo" ON ("project"."seoid"="seo"."id") WHERE ("t"."id"=$...

                                                             ^' in C:\xampp\htdocs\yii\framework\db\CDbCommand.php:375



Are you using 1.1.2? Are you able to reproduce it?

What yii version do you use? Alias t was introduced in 1.1.0.

Yes, yii 1.1.2 (lastest)

And Yes i can reproduce it.

My way to reproduce:

After removal of temporary files bug disappeared.

I returned to the construction of the query.

My dev version was




$prj=Project::model()->with(array('author'=>array('with'=>'profile'),'seo'=>array('with'=>'profile')))->findByPk(intval($_GET['projectid']));



It generate the bug with aliase ‘profile’ for related model Profile.

Update to:




$prj=Project::model()->with(array('author'=>array('with'=>'profile'),'seo'))->findByPk(intval($_GET['projectid']));



And bug return.

Now code look like


$prj=Project::model()->findByPk(intval($_GET['projectid']) );

$auth_fio=$prj->author->profile->fio;

$seo_fio=$prj->seo->profile->fio;



Tomorrow will write down the video and posted on a forum

Could you create reduced test case so it will include minimal reproduceable code? So 2—3 standard models with relations, one controller.

Video is not necessary.

All test with comments in a BugController code.

Check ‘test 5’ – its our problem. read comment.

Also comments on ‘test 4’ welcome, too.




class BugController extends Controller

{

    public function actionIndex()

    {

        $id=262;

        //test 1:  normal

        //$prj=Project::model()->with()->findByPk($id);

//***********************************************************        

        //test 2: normal

        //$prj=Project::model()->with(array('analytic','seo'))->findByPk($id);

//***********************************************************         

        //test 3: normal

        //$prj=Project::model()->with(array('analytic'=>array('with'=>'profile'),'seo'=>array()))->findByPk($id);

//***********************************************************        

        //test 4: Bug with 'profile'

        //$prj=Project::model()->with(array('analytic'=>array('with'=>'profile'),'seo'=>array('with'=>'profile')))->findByPk($id);

        

/*        

        CDbCommand failed to execute the SQL statement: SQLSTATE[42712]: Duplicate alias: 7 ERROR: table name "profile" specified more than once

  

        Error in querying SQL: SELECT "t"."title" AS "t0_c0", "t"."budget_max" AS

        "t0_c1", "t"."budget_current" AS "t0_c2", "t"."cost" AS "t0_c3", "t"."site"

        AS "t0_c4", "t"."salesid" AS "t0_c5", "t"."seoid" AS "t0_c6",

        "t"."analyticid" AS "t0_c7", "t"."stausdescription" AS "t0_c8",

        "t"."status" AS "t0_c9", "t"."created" AS "t0_c10", "t"."id" AS "t0_c11",

        "t"."clientid" AS "t0_c12", "t"."intelsiblink" AS "t0_c13", "t"."paytime"

        AS "t0_c14", "t"."starttime" AS "t0_c15", "t"."debt" AS "t0_c16",

        "t"."pointsnow" AS "t0_c17", "t"."pointslast" AS "t0_c18", "t"."host_pwd"

        AS "t0_c19", "t"."host_usr" AS "t0_c20", "t"."ssh_pwd" AS "t0_c21",

        "t"."ssh_usr" AS "t0_c22", "t"."bd_pwd" AS "t0_c23", "t"."bd_usr" AS

        "t0_c24", "t"."adm_pwd" AS "t0_c25", "t"."adm_usr" AS "t0_c26",

        "t"."ftp_pwd" AS "t0_c27", "t"."ftp_usr" AS "t0_c28", "t"."host_server" AS

        "t0_c29", "t"."ssh_server" AS "t0_c30", "t"."ftp_server" AS "t0_c31",

        "t"."adm_server" AS "t0_c32", "t"."type" AS "t0_c33", "t"."top10" AS

        "t0_c34", "t"."top5" AS "t0_c35", "t"."top3" AS "t0_c36", "t"."state" AS

        "t0_c37", "t"."prompt_days" AS "t0_c38", "t"."prompt_status" AS "t0_c39",

        "t"."prompt_viewed" AS "t0_c40", "t"."show_cost" AS "t0_c41",

        "t"."partnerid" AS "t0_c42", "t"."copyrighterid" AS "t0_c43",

        "analytic"."id" AS "t1_c0", "analytic"."username" AS "t1_c1",

        "analytic"."password" AS "t1_c2", "analytic"."status" AS "t1_c3",

        "profile"."ownerid" AS "t2_c0", "profile"."firstname" AS "t2_c1",

        "profile"."lastname" AS "t2_c2", "profile"."email" AS "t2_c3",

        "profile"."createdate" AS "t2_c4", "profile"."sape_login" AS "t2_c5",

        "profile"."sape_password" AS "t2_c6", "profile"."sape_params" AS "t2_c7",

        "profile"."phone_msk" AS "t2_c8", "profile"."photo" AS "t2_c9",

        "profile"."phone_nsk" AS "t2_c10", "profile"."partner" AS "t2_c11",

        "seo"."id" AS "t3_c0", "seo"."username" AS "t3_c1", "seo"."password" AS

        "t3_c2", "seo"."status" AS "t3_c3", "profile"."ownerid" AS "t4_c0",

        "profile"."firstname" AS "t4_c1", "profile"."lastname" AS "t4_c2",

        "profile"."email" AS "t4_c3", "profile"."createdate" AS "t4_c4",

        "profile"."sape_login" AS "t4_c5", "profile"."sape_password" AS "t4_c6",

        "profile"."sape_params" AS "t4_c7", "profile"."phone_msk" AS "t4_c8",

        "profile"."photo" AS "t4_c9", "profile"."phone_nsk" AS "t4_c10",

        "profile"."partner" AS "t4_c11" FROM "Project" "t"  LEFT OUTER JOIN "User"

        "analytic" ON ("t"."analyticid"="analytic"."id") LEFT OUTER JOIN "Profile"

        "profile" ON ("profile"."ownerid"="analytic"."id") LEFT OUTER JOIN "User"

        "seo" ON ("t"."seoid"="seo"."id") LEFT OUTER JOIN "Profile" "profile" ON

        ("profile"."ownerid"="seo"."id") WHERE ("t"."id"=262)

        in C:\xampp\htdocs\test\protected\controllers\BugController.php (18)

        in C:\xampp\htdocs\test\index.php (12)

*/

//***********************************************************        

        //test 5: Bug with 'with()->findByPk', Bug not repeated  every time.


        //Look at Model Project code and check method: defaultScope()   

        // seted  alias for use 'project' in  any place instead 't'

        $prj=Project::model()->with(array('analytic'=>array('with'=>'profile'),'seo'=>array()))->findByPk(intval($id) );

        /*

         CDbCommand failed to execute the SQL statement: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "t"

         LINE 1: ...r" "seo" ON ("project"."seoid"="seo"."id") WHERE ("t"."id"=2...

         

         Error in querying SQL: SELECT "project"."title" AS "t0_c0",

        "project"."budget_max" AS "t0_c1", "project"."budget_current" AS "t0_c2",

        "project"."cost" AS "t0_c3", "project"."site" AS "t0_c4",

        "project"."salesid" AS "t0_c5", "project"."seoid" AS "t0_c6",

        "project"."analyticid" AS "t0_c7", "project"."stausdescription" AS "t0_c8",

        "project"."status" AS "t0_c9", "project"."created" AS "t0_c10",

        "project"."id" AS "t0_c11", "project"."clientid" AS "t0_c12",

        "project"."intelsiblink" AS "t0_c13", "project"."paytime" AS "t0_c14",

        "project"."starttime" AS "t0_c15", "project"."debt" AS "t0_c16",

        "project"."pointsnow" AS "t0_c17", "project"."pointslast" AS "t0_c18",

        "project"."host_pwd" AS "t0_c19", "project"."host_usr" AS "t0_c20",

        "project"."ssh_pwd" AS "t0_c21", "project"."ssh_usr" AS "t0_c22",

        "project"."bd_pwd" AS "t0_c23", "project"."bd_usr" AS "t0_c24",

        "project"."adm_pwd" AS "t0_c25", "project"."adm_usr" AS "t0_c26",

        "project"."ftp_pwd" AS "t0_c27", "project"."ftp_usr" AS "t0_c28",

        "project"."host_server" AS "t0_c29", "project"."ssh_server" AS "t0_c30",

        "project"."ftp_server" AS "t0_c31", "project"."adm_server" AS "t0_c32",

        "project"."type" AS "t0_c33", "project"."top10" AS "t0_c34",

        "project"."top5" AS "t0_c35", "project"."top3" AS "t0_c36",

        "project"."state" AS "t0_c37", "project"."prompt_days" AS "t0_c38",

        "project"."prompt_status" AS "t0_c39", "project"."prompt_viewed" AS

        "t0_c40", "project"."show_cost" AS "t0_c41", "project"."partnerid" AS

        "t0_c42", "project"."copyrighterid" AS "t0_c43", "analytic"."id" AS

        "t1_c0", "analytic"."username" AS "t1_c1", "analytic"."password" AS

        "t1_c2", "analytic"."status" AS "t1_c3", "profile"."ownerid" AS "t2_c0",

        "profile"."firstname" AS "t2_c1", "profile"."lastname" AS "t2_c2",

        "profile"."email" AS "t2_c3", "profile"."createdate" AS "t2_c4",

        "profile"."sape_login" AS "t2_c5", "profile"."sape_password" AS "t2_c6",

        "profile"."sape_params" AS "t2_c7", "profile"."phone_msk" AS "t2_c8",

        "profile"."photo" AS "t2_c9", "profile"."phone_nsk" AS "t2_c10",

        "profile"."partner" AS "t2_c11", "seo"."id" AS "t3_c0", "seo"."username" AS

        "t3_c1", "seo"."password" AS "t3_c2", "seo"."status" AS "t3_c3" FROM

        "Project" "project"  LEFT OUTER JOIN "User" "analytic" ON

        ("project"."analyticid"="analytic"."id") LEFT OUTER JOIN "Profile"

        "profile" ON ("profile"."ownerid"="analytic"."id") LEFT OUTER JOIN "User"

        "seo" ON ("project"."seoid"="seo"."id") WHERE ("t"."id"=262)

         */

        echo '<pre>';

        print_r($prj->attributes);

        echo '</pre>';

    }

}






class Project extends CActiveRecord

{

    public static function model($className=__CLASS__){ return parent::model($className);    }


    public function tableName(){ return 'Project';  }


    public function relations()

    {

        return array(

            'sales'  => array(self::BELONGS_TO, 'User', 'salesid', 'alias'=>'sales'),

            'seo'    => array(self::BELONGS_TO, 'User', 'seoid', 'alias'=>'seo'),

            'analytic'=> array(self::BELONGS_TO, 'User', 'analyticid', 'alias'=>'analytic'),

        );

    }  


    public function defaultScope() {

        return array(

            'alias' => 'project'

        );

    }    

}




class User extends CActiveRecord

{

	public static function model($className=__CLASS__){ return parent::model($className); }


	public function tableName()	{ return 'User'; }


	public function relations()

	{

		return array(

			'profile' => array(self::HAS_ONE, 'Profile', 'ownerid'),

		);

	}

}




class Profile extends CActiveRecord

{

	public static function model($className=__CLASS__){ return parent::model($className); }


	public function tableName(){ return 'Profile';}


	public function relations()

	{

		return array(

			'owner' => array(self::BELONGS_TO, 'User', 'ownerid'),

		);

	}

}

Perhaps it is important, my database is PostgreSQL 8.4

Ok, without video