Join tables for get information

Hi i want to find Company’s vehicle in my application for this reason i want to use nested query or join between company table and vehicle table.

I create AR for my tables and write relations for them but i can’t get information with below commands.




1)

$companyVehicles=Company::model()->with(array(

            'hasVehicle'=>array(

            'select'=>array('hasVehicle.vehicleid','hasVehicle.vehicleName'),

            'joinType'=>'INNER JOIN',

            'condition'=>'hasVehicle.companyid=1',

            'params'=>array(':companyId=>$this->companyid'),

        ),

        ))->findAll();


2)

$criteria = new CDbCriteria();

        $criteria->with = 'isVehicle';

        $criteria->select=array('vehicleId','vehicleName');

        $criteria->condition='isVehicle.companyId=:companyId';

        $criteria->params=array(':companyId'=>$this->companyid);

        $companyVehicles = Vehicle::model()->with('hasVehicle')->findAll($criteria);



for get information i use below commmand but i can’t use above commmand to get information.

how can i fix problem of 1 and 2 commands to get information?




$companyVehicles=Company::model()->findByPk(1);

$vehicles=$companyVehicles->hasVehicle;



My models class are :




class Company extends CActiveRecord

{

	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(

                    'hasEmployee'=>array(self::HAS_MANY,'Person','companyid'),

                    'hasVehicle'=>array(self::HAS_MANY,'Vehicle','companyid'),

		);

	}

}


class Vehicle extends CActiveRecord

{

	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(

                    'isVehicle'=>array(self::BELONGS_TO, 'Company', 'companyid'),

		);

	}

}



And ER model for these entities are:

CompanyEntity------1------RelationShip----N------VehicleEntity

I use companyid as foreign key in vehicle table.

Is my relationships in yii code true?

why i can’t get information with 1 and 2 codes?

please help me

thanks

No one can help me?

I don’t know how can i perform Join queries in YII AR!

my query is




$criteria = new CDbCriteria();

        $criteria->with = 'isVehicle';

        $criteria->select=array('vehicleid','vehiclename');

        $criteria->condition='isVehicle.companyid=:companyId';

        $criteria->params=array(':companyId'=>$this->companyid);

        $companyVehicles = Vehicle::model()->with('isVehicle')->findAll($criteria);



and get this error for above query!




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

LINE 1: ...("t"."companyid"="isVehicle"."companyid") WHERE (isVehicle....

^. The SQL statement executed was: SELECT "t"."vehicleid" AS "t0_c0", "t"."vehiclename" AS "t0_c1", "isVehicle"."companyid" AS "t1_c0", "isVehicle"."companyaddress" AS "t1_c1", "isVehicle"."companyphone" AS "t1_c2", "isVehicle"."companyname" AS "t1_c3" FROM "vehicletbl" "t" LEFT OUTER JOIN "companytbl" "isVehicle" ON ("t"."companyid"="isVehicle"."companyid") WHERE (isVehicle.companyid=:companyId)



Although i read AR documentation but i don’t know how can i use below query in AR!

tbl1 inner join tbl2 on condition1 inner join tbl3 on condition2;

please help me to do above sql command with AR!

Can i use SQL Commands with relational tables?

I see Yii support sql command only for one table but i don’t know how can i perform sql commands for relational tables