yii2 Gridview ไม่แสดงคอลัมน์ที่มีแอตทริบิวต์ที่เข้าร่วม

ฉันมีใบรับรองรุ่นซึ่งมี application_id คีย์ต่างประเทศของรุ่นอื่นที่เรียกว่า Application ดังนั้นใบรับรองแต่ละใบจึงเป็นของแอปพลิเคชันเดียว

ขณะนี้มีสถานการณ์ที่ฉันต้องการแสดงใบรับรองทั้งหมดของผู้ใช้ที่มีอยู่ รหัสผู้ใช้มีอยู่ในโมเดลแอปพลิเคชัน เช่น user_id

นี่คือแบบสอบถาม

SELECT * FROM `certificates` 
inner join applications b ON    
application_id = b.id where b.user_id = 7

จากบันทึกที่มาจากแบบสอบถามข้างต้น ฉันต้องการแสดงคอลัมน์ของใบรับรองบางส่วนและบางส่วนจากแอปพลิเคชันที่ใช้มุมมองตาราง แต่ด้วยเหตุผลบางประการ หากบันทึกมีมากกว่าหนึ่งรายการ ฉันจะไม่ได้รับข้อมูลคอลัมน์จากแอปพลิเคชัน

  <?php Pjax::begin(); ?>    <?= GridView::widget([
                    'dataProvider' => $dataProvider,
               //     'filterModel' => $searchModel,

                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],

                        'application_id',
                         'verified_application_file_path',
                         'certificate_name',
                        'application.name',
                        'application.user_id',


                        [
                            'attribute' => 'creation_date',
                            'format' => ['date', 'php:d/m/Y']
                        ],
                        [
                            'attribute' => 'expiry_date',
                            'format' => ['date', 'php:d/m/Y']
                        ],

                    ],
                ]); ?>
                <?php Pjax::end(); ?></div>

ตารางด้านบนแสดงชื่อและรหัสผู้ใช้หากบันทึกเดียวได้รับการส่งคืน มิฉะนั้นจะแสดงเป็น "ไม่ได้ตั้งค่า" ฉันไม่แน่ใจว่าทำไม 'application.name' และ 'application.user_id' ไม่ทำงานเมื่อได้รับมากกว่าหนึ่งบันทึก

นี่คือคำถามของฉันโดยใช้ yii2

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search_vendor_certificates($user_id)
{
    $query = ApplicationCertificates::find()->joinWith('application b',true , 'INNER JOIN')->where(["b.user_id"=>$user_id]);

    // add conditions that should always apply here


    $dataProvider = new \yii\data\ActiveDataProvider([
        'query' => $query,
    ]);

return $dataProvider; }

ฉันจะขอบคุณถ้ามีคนบอกฉันว่าฉันกำลังทำอะไรผิดพลาดในการแสดงคุณลักษณะที่ถูกต้องของโมเดลแอปพลิเคชัน


person wolvorinePk    schedule 21.01.2017    source แหล่งที่มา


คำตอบ (1)


ก่อนอื่น (อย่าใช้สิ่งนี้ ฉันจะแสดงให้คุณเห็นข้อผิดพลาดเชิงตรรกะ):

->joinWith('application b',true , 'INNER JOIN')

คุณตั้งค่านามแฝงสำหรับ application b จากนั้นใน GridView ให้ใช้ application อย่างไรก็ตาม มันยังคงแย่อยู่หากคุณเปลี่ยนชื่อเป็น b ใน GridView


จากคำตอบนี้:

โมเดล TableTwo:

public function getTableOneRecord()
{
    return $this->hasOne(TableOne::className(), ['id' => 't1_id']);
}

public function getTableThreeRecord()
{
    return $this->hasOne(TableThree::className(), ['id' => 't3_id']);
}

public function getTableFourRecord()
{
    return $this->hasOne(TableFour::className(), ['id' => 't4_id'])
        ->via('tableThreeRecord');
}

และไฟล์มุมมอง:

echo GridView::widget([
   'dataProvider' => $dataProvider,
   'columns' => [
       'id',
       't1_id',
       'tableOneRecord.id',
       't3_id',
       'tableThreeRecord.id',
       'tableThreeRecord.t4_id',
       'tableFourRecord.id',
   ],
]);

วิธีที่ง่ายที่สุดที่จะพูดคือ ความสัมพันธ์ของคุณจากการค้นหาจะไม่ทำงานใน GridView คุณต้องกำหนดความสัมพันธ์ใน Model แล้วใช้ความสัมพันธ์ ;)

ดูโค้ดบน GitHub

person Yupik    schedule 23.01.2017