Problem s clone ActiveRow
- David Ďurika
- Člen | 328
mam kod:
<?php
$row = $this->selection->find($id);
$rowTemp = clone $row;
$row->// to sa vytahuju nejake zavislosti a potom sa to foreach-uje
$rowTamp->//chcem vytiahnut dalsie zavislosti ALE tu to uz zostavy zle query
?>
ked ten kod upravim takto:
<?php
$row = $this->selection->find($id);
$rowTemp = $this->selection->find($id);
$row->// to sa vytahuju nejake zavislosti a potom sa to foreach-uje
$rowTamp->//funguje spravne
?>
tak je setko OK!
je tam nejaky bug? alebo je to naschval? ci len robim nieco zle ?
- David Matějka
- Moderator | 6445
asi je tam zavislost na puvodnim selection, schvalne zkus tohle:
<?php
$row = $this->selection->find($id);
$rowTemp = clone $row;
$rowTemp->setTable(clone $row->getTable());
?>
- David Ďurika
- Člen | 328
@matej21 mal si pravdu, funguje to tak ako pises
@enumag hore pisem ze preco potrebujem kolonovat…
- David Ďurika
- Člen | 328
sam nechapem, tiez som cakal ze to pojde ale ked spravim nieco taketo:
<?php
$row = $this->selection->find($id);
$rowTemp = $row;
$row->// to sa vytahuju nejake zavislosti a potom sa to foreach-uje
$rowTamp->//chcem vytiahnut dalsie zavislosti ALE tu to uz zostavy zle query
?>
- David Ďurika
- Člen | 328
<?php
$restaurant = $this->restaurants->find($id);
$foodsList = $restaurant->related('food')->fetchPairs('id', 'name');
$menuCard = $restaurant->related('menu_card')->where('date > ?', 0);
foreach($menuCard as $value) {
$value->food->name; // tuto do hody chybu
}
?>
v premennej $foodsList a $menuCard su data OK len uz ked zacnem foreachovat $menuCard a v nom sa cez vstah pitat na dalsiu tabulku (food) a potom na stlpec (name) tak to hodi error. ALE nie pri prvej iteracii ta este zbehne OK, ale az na dalsej! lebo tam sa vykona query
<?php
SELECT `id`, `name` FROM `food` WHERE (`id` IN (5))
?>
cize ak mam v premennej $value->food_id = 5 tak po zavolani $value->food sa dostane pekne na tu dalsiu tabulku s jedlamy, ale ked je $value->food_id = 6 tak po zavolani $value->food dostanem NULL a nie activeRow ako predtym, kedze to spravilo zle SQL-ko…
ALE ked si tu premennu $restaurant vytahnem nanovo tak to ide ok!
<?php
$restaurant = $this->restaurants->find($id);
$foodsList = $restaurant->related('food')->fetchPairs('id', 'name');
$restaurant = $this->restaurants->find($id);
$menuCard = $restaurant->related('menu_card')->where('date > ?', 0);
...
?>
Editoval achtan (17. 11. 2012 12:56)
- enumag
- Člen | 2118
Aha, chápu. Tohle ale asi těžko někdo opraví aniž by tu chybu reprodukoval u sebe a důkladně otestoval. Leda @hrach by to možná dokázal.
Dej to jako issue na GitHub. S klonováním to nijak nesouvisí, to vůbec nezmiňuj. Bylo by dobré přidat SQL relevantních tabulek (ideálně test nebo sandbox se simulací tohoto problému).
- David Ďurika
- Člen | 328
SQL
CREATE TABLE `food` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`restaurant_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `restaurant_id` (`restaurant_id`),
CONSTRAINT `food_ibfk_1` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurant` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `food` (`id`, `name`, `restaurant_id`)
VALUES
(5,'Kura',1),
(6,'Prasa',1);
CREATE TABLE `menu_card` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`restaurant_id` int(11) unsigned DEFAULT NULL,
`food_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `restaurant_id` (`restaurant_id`),
KEY `food_id` (`food_id`),
CONSTRAINT `menu_card_ibfk_1` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurant` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `menu_card_ibfk_2` FOREIGN KEY (`food_id`) REFERENCES `food` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `menu_card` (`id`, `restaurant_id`, `food_id`)
VALUES
(43,1,5),
(44,1,6),
(45,1,6),
(46,1,5),
(47,1,5),
(48,1,6);
CREATE TABLE `restaurant` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `restaurant` (`id`, `name`)
VALUES
(1,'FastVega');
a tu je kod pri kt. to hodi chybu
$restaurant = $this->getService('database')->table('restaurant')->where('id', 1)->fetch();
$menuCards = $restaurant->related('menu_card')->limit(1);
foreach ($menuCards as $row) {
Debugger::barDump($row->food);
Debugger::barDump($row->food->name);
}
$menuCards = $restaurant->related('menu_card')->limit(2);
foreach ($menuCards as $row) {
Debugger::barDump($row->food);
Debugger::barDump($row->food->name);
}