madcorp / zfdatagrid

Automatically exported from code.google.com/p/zfdatagrid
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

doctrine2: need virtual column with count() of relations #771

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

i use doctrine2 as source.

i have a column "prota_bilder" - that defines a OneToMany relation to a picture 
table.

    /**
     * @OneToMany(targetEntity="Modulename\Models\Picture", mappedBy="protagonist")
     * @OrderBy({"name" = "ASC"})
     */
    protected $prota_picture;

Now i need a Grid column to show the relation count, for example "has 2 
picture(s)"

i have tried something, but all with doctrine Exceptions.

#       $selectColumnsDBLArr[] = 'p.prota_picture';
#       $grid->updateColumn('prota_picture',array('escape'=>'count'));
#       $selectColumnsDBLArr[] = 'count(p.prota_picture) prota_picture';

What is the right way to show a count ?

PS:
$prota_picture is of type Doctrine\Common\Collections\ArrayCollection or NULL.

if (isnull($prota_picture))
      echo "0";
else
      echo $prota_picture->count();

best regards,
Daniel.

Original issue reported on code.google.com by phpact...@gmail.com on 4 Oct 2011 at 8:09

GoogleCodeExporter commented 9 years ago
I can't see a problem here.

Have you tried the SIZE function 
(http://www.doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-la
nguage.html#dql-functions)?

Following example works:
$qb = $em->getRepository('\My\Entity\Account')->createQueryBuilder('c');
$qb->select('c.name', 'SIZE(c.bugsToReview)');

You can find this example in the Doctrine2Controller#accountAction in the 
current trunk.

Original comment by martin.p...@gmail.com on 19 Oct 2011 at 2:21

GoogleCodeExporter commented 9 years ago

Original comment by martin.p...@gmail.com on 19 Oct 2011 at 2:21

GoogleCodeExporter commented 9 years ago
confiremed: this works

//NOT WORK:     $selectColumnsDBLArr[] = 'count(p.prota_changelogs)';   # Message: 
[Semantical Error] line 0, col 149 near 'prota_changelogs)': Error: Invalid 
PathExpression. StateFieldPathExpression or SingleValuedAssociationField 
expected.

//DOES WORK:        $selectColumnsDBLArr[] = 'size(p.prota_changelogs)';

thanks, you can close this

Original comment by phpact...@gmail.com on 20 Oct 2011 at 3:06

GoogleCodeExporter commented 9 years ago

Original comment by martin.p...@gmail.com on 21 Oct 2011 at 10:11