I want to join two tables in mysql:
ticket_periods. I want to join the two tables with ticket_number field.
For the first when creating it I’ve set up a FK:
In the Tickets model i have relation:
‘ticketNumber0’=>array(self::BELONGS_TO, ‘TicketPeriods’, ‘ticket_number’),
,and in the TicketPeriods model i have relation:
‘tickets’=>array(self::HAS_MANY, ‘Ticket’, ‘ticket_number’),
In the TicketController i am trying this:
$criteria=new CDbCriteria; $criteria->with=array('ticketNumber0',); $criteria->condition = "t.id = $id"; $this->render('view',array( 'ticket_periods1'=>Tickets::model()->findAll($criteria), ));
But, when i am trying to create one ticket i get this error:
CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails
tk_num FOREIGN KEY (
, that would mean i have no records in the referenced table and i have "childs" without "parents".
if i manually populate the second table i would get the proper sql but i don’t have the columns from the referenced table displayed!
Can someone please give me some tips of how to join two tables in mysql?