Search code examples
phparrayscodeigniterdoctrine-ormcodeigniter-2

Doctrine join give me different different array instead of one array in codeigniter


Hello guy's i am new in doctrine. I just try join query in doctrine like this

 $query = $this->em->createQuery('SELECT sub, cat FROM Entity\Zi_subcategory sub JOIN Entity\Zi_category cat WHERE sub.cat_id = cat.id');
 $arrData['subcategories'] = $query->execute();
 echo "<pre>"; print_r($arrData['subcategories']); exit;

but it's give me different different array result instead of one array my output is

 Array
(
    [0] => Entity\Zi_subcategory Object
        (
            [id:protected] => 1
            [name:protected] => cricket
            [cat_id:protected] => 11
            [description:protected] => testing
            [created_at:protected] => DateTime Object
                (
                    [date] => 2014-10-02 00:00:00.000000
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

            [updated_at:protected] => DateTime Object
                (
                    [date] => 2014-10-02 00:00:00.000000
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

        )

    [1] => Entity\Zi_category Object
        (
            [id:protected] => 11
            [name:protected] => testing2
            [description:protected] => testing
            [created_at:protected] => DateTime Object
                (
                    [date] => 2014-10-06 17:54:15.000000
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

            [updated_at:protected] => DateTime Object
                (
                    [date] => 2014-10-06 17:54:15.000000
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

        )

)

My problem is if its correct then how we will show data in table because when i try to run foreach its run 2 time because of 2 array.


Solution

  • I changed my code i used this code for array

    $arrData['subcategories'] = $this->qb->select(array('sub.id', 'sub.name as subcat_name','cat.name as category_name'))
                                        ->from('Entity\Zi_subcategory','sub')
                                        ->join('Entity\Zi_category', 'cat')
                                        ->where('sub.cat_id = cat.id')
                                        ->orderBy('sub.id','DESC')
                                        ->setFirstResult( $offset )
                                        ->setMaxResults( $config['per_page'] )
                                        ->getQuery()
                                        ->getresult();
     echo "<pre>"; print_r($arrData['subcategories']); exit;
    

    its works for me....thax all of u...