Hi,
Yes I have read the documentation, but somethings are not clear for me. I could not joining models.
Maybe I use in the bad way. Sure. So I need your helps.
I need this joining (this works fine):
SELECT `dt_tickets` . * , `dt_bettings` . *
FROM `dt_tickets`
LEFT JOIN `dt_betteds` ON `dt_betteds`.`ticket_id` = `dt_tickets`.`id`
LEFT JOIN `dt_bettings` ON `dt_betteds`.`betting_id` = `dt_bettings`.`id`
This is my SQL:
--
-- Table structure for table `dt_betteds`
--
CREATE TABLE IF NOT EXISTS `dt_betteds` (
`ticket_id` int(11) NOT NULL,
`betting_id` int(11) NOT NULL,
KEY `ticket_id` (`ticket_id`),
KEY `betting_id` (`betting_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Table structure for table `dt_tickets`
--
CREATE TABLE IF NOT EXISTS `dt_tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`betting_sum` decimal(20,0) unsigned NOT NULL,
`betting_type` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
--
-- Table structure for table `dt_bettings`
--
CREATE TABLE IF NOT EXISTS `dt_bettings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
-- .....
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1012 ;
I have tried this, but this is not working for me:
$ticket = Ticket::find()->leftJoin('dt_betteds', 'dt_betteds.ticket_id=dt_tickets.id')->leftJoin('dt_bettings', 'dt_betteds.betting_id=dt_bettings.id')->all();
Here I got back just the ticket, but the bettings and the betteds not…
This is the generated Ticket model relation:
/**
* @return \yii\db\ActiveRelation
*/
public function getDtBetteds()
{
return $this->hasMany(Betteds::className(), ['ticket_id' => 'id']);
}
And this the Betteds model relations:
/**
* @return \yii\db\ActiveRelation
*/
public function getBetting()
{
return $this->hasOne(Bettings::className(), ['id' => 'betting_id']);
}
/**
* @return \yii\db\ActiveRelation
*/
public function getTicket()
{
return $this->hasOne(Tickets::className(), ['id' => 'ticket_id']);
}
So How can I get the Tickets and Bettings models’s data with the Betteds (pivot table)?