I am new to YII and am not getting a particular solution.
I have generated 2 Model classes(people and supplier) with gii code generator
people table will contain the records for customers, employees, suppliers etc
I have a form which is saving the data in 2 tables. In one table, its primary and the same is foreign key in other table. I am having 2 tables, phppos_people and phppos_suppliers. When I am adding a supplier, data is getting saved in phppos_people where a Primary Key is getting generated. Few values are going to phppos_suppliers table with the auto generated id of phppos_people as one column in phppos_suppliers and its a foreign key.
Relations function of my People model class is as below :
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(
'phpposSuppliers' => array(self::HAS_MANY, 'PhpposSuppliers', 'person_id','index'=>'person_id','together'=>true ),
);
}
My suppliers listing function in the controller is as below :
Its showing all the records from people table no matter whether they are available in suppliers table or not. How can I use the relations in Model class to get the data which are available in both the tables.
Its not working for me. I am getting the below error :
Trying to get property of non-object
Can you get me a working example of it.
My project basically have many roles like employees, customers, suppliers etc. Common fields like first name, last name, email, mobile etc are stored in people table and the specific fields related to each role are stored in the respective tables with a foreign key which is primary in people table.
Now to show a list of suppliers, I need to join the suppliers and people table and show only the matching records and likewise for employees, customers etc.
Currently, for suppliers list, all the records from the people table are showing. I need to show only the matching records.
Controller class code
public function actionCreate()
{
$model=new PhpposPeople;
$model1=new PhpposSuppliers;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['PhpposPeople']))
{
$transaction=Yii::app()->db->beginTransaction();
try{
$model->attributes=$_POST['PhpposPeople'];
$model1->attributes=$_POST['PhpposSuppliers'];
if($model->save())
{
$model1->person_id=$model->person_id;
if($model1->save())
{
$transaction->commit();
$this->redirect(array('view','id'=>$model->person_id));
}
}
$transaction->rollBack();
}
catch(Exception $e) { // an exception is raised if a query fails
//something was really wrong - exception!
$transaction->rollBack();
//you should do sth with this exception (at least log it or show on page)
Yii::log( 'Exception when saving data: ' . $e->getMessage(), CLogger::LEVEL_ERROR );
}
}
$this->render('create',array(
'model'=>$model,
'model1'=>$model1,
));
}