Answer the question
In order to leave comments, you need to log in
How to properly organize relationships between tables in Yii?
Hello! I have 2 tables:
CREATE TABLE `orders` (
`id` INT(255) NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `goods` (
`id` INT(255) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`price` DECIMAL(10,2) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `orders_goods_assigment` (
`id` INT(255) NOT NULL AUTO_INCREMENT,
`order_id` INT(255) NOT NULL,
`goods_id` INT(255) NOT NULL,
PRIMARY KEY (`id`)
)
public function relations() {
return [
'goods' => [
self::MANY_MANY,
'Goods',
'orders_goods_assigment(order_id, goods_id)'
]
];
}
INSERT INTO `orders_goods_assigment` (`order_id`, `goods_id`) VALUES (1, 2);
INSERT INTO `orders_goods_assigment` (`order_id`, `goods_id`) VALUES (1, 2);
INSERT INTO `orders_goods_assigment` (`order_id`, `goods_id`) VALUES (1, 2);
$order = Order::model()->findByPk(1);
print_r($order->goods); # Array([0] => Goods)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question