Answer the question
In order to leave comments, you need to log in
Yii2 fetching data with FindBySql?
Hello
Task, to select partners from the database
, the problem is the following
, judging by the screen, ID is not transmitted to query the database.
in the model:
public function getPartners(){
$sql = 'SELECT dam as partner_id FROM pedigrees WHERE (sire=id and dam<>0) group by dam union SELECT sire as partner_id FROM pedigrees WHERE (dam=id and sire<>0) group by sire order by (select name from pedigrees a where id=partner_id)';
$partner = Pedigrees::findBySql($sql)->all();
return $partner;
}
<?php $partner = $model->getPartners();
foreach($partner as $partn) {
echo Html::a( Html::encode($partn->name, $partn->id), Url::to(['pedigrees/view', 'id' => $partn->id])) . '<br>';
}
?>
Answer the question
In order to leave comments, you need to log in
public function getPartners(){
$sql = 'SELECT dam as partner_id FROM pedigrees WHERE (sire=:id and dam<>0) group by dam union SELECT sire as partner_id FROM pedigrees WHERE (dam=:id and sire<>0) group by sire order by (select name from pedigrees a where id=:partner_id)';
$partner = Pedigrees::findBySql($sql, [':id' => $id, 'partner_id' => $partner_id])->all();
return $partner;
}
Apparently this is a new model. And the most likely solution is:
if(!$model->isNewRecord){
//тут ваша логика, тут точно есть id
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question