I
I
idaho2017-07-12 13:16:24
Yii
idaho, 2017-07-12 13:16:24

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.
8d97159045f446a2a8bc19dd94118324.png
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;
           
        }

output in view:
<?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

2 answer(s)
D
davidnum95, 2017-07-12
@idaho

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;
 }

M
Maxim Timofeev, 2017-07-12
@webinar

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 question

Ask a Question

731 491 924 answers to any question