dsa4d4789789 2017-01-24 10:47
浏览 46
已采纳

如何在ZF2中设置MySQL数据库表的特定属性的条件?

This is the code, and i want to set condition on obtainedMarks. The condition will check if the obtainedMarks have a value assigned or not.

 $qb->select(array(
       "academic.studentAcademicId as studentAcademicId",
       "academic.descriptions as descriptions",
       "student.name as studentName",
       "student.studentId as studentId",
       "class.name as className",
       "session.name as sessionName",
       ***"SUM(marks.marksObtained) as marksObtained",***
    ))
        ->from('Application\Entity\StudentAcademics','academic')
        ->leftJoin('academic.class','class')
        ->leftJoin('academic.marks','marks')
        ->leftJoin('academic.student','student')
        ->leftJoin('academic.session','session')
        ->where('academic.student = :student')
        ->setParameter('student',$studentId)->groupBy('studentAcademicId');

So how to put such a condition that checks out if obtainedMarks carry a value or not..!

  • 写回答

1条回答 默认 最新

  • dqrfdl5708 2017-01-26 10:26
    关注

    In Zendframework2, all query data is assigned to an array variable e.g $data

     $data = $qb->getQuery()->getScalarResult();
    

    and is returned to whoever call this controller i.e,

    return new ViewModel(array(
                    'data' => $data,
                )
    

    To check if a specific attribute in a query bears a value or not is to check it through foreach loop in the view that called this controller e.g

    <?php foreach ($this->data as $item): ?>
    <tr>
        <td>
            <?php if(isset( $item['marksObtained']) &&  $item['marksObtained'] > 0){?>
                <a href="<?=$this->url( 'marks/details', array('action'=>'index', 'id'=>$item['studentAcademicId']))?>">
                    <?php echo $item['marksObtained']; ?></a>
            <?php }else{ ?>
                <a href="<?=$this->url( 'marks/details', array('action'=>'add', 'id'=>$item['studentAcademicId']))?>">
                    Add Marks</a>
            <?php }?>
        </td>
    </tr>
    

    As seen, the if condition is checking out the obtainedMarks value from the data array variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。