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