one to many - Symfony Sonata OneToMany, Sum or Total of a field is not showing in the Admin List -
i have 2 entities, client , campaign.
client entity /** * @orm\onetomany(targetentity="campaign", mappedby="client") */ protected $campaign;
+++++++++++++++++++++++++++++
campaign entity /** * @var integer * * @orm\column(name="numberofbid", type="integer", nullable=true) */ protected $numberofbid; /** * @orm\manytoone(targetentity="clients", inversedby="campaign") * @orm\joincolumn(name="client_id", referencedcolumnname="client_id") */ protected $client; /* let's client has campaign a, numberofbid = 1 client has campaign b, numberofbid = 5 client has campaign c, numberofbid = 3 client has campaign d, numberofbid = 4 total numberofbid = 13 */
problem: how sum of numberofbid , show 1 column in client admin list board? on method configurelistfields, tried different ways using sonata_type_model, doctrine_orm_callback, query still didn't work.
client id | campaign totalbid | 13
hoping feedback.
thanks in advance.
first should rename
protected $campaign;
in
protected $campaigns;
cause it's collection. 1 client has many campaigns.
to problem: implement method on client entity this
class client{ ... public function gettotalnumberofbids() { $i = 0; foreach ($this->getcampaigns() $campaign) { $i += $campaign->getnumberofbid(); } return $i; } ... }
and add list view by
protected function configurelistfields(listmapper $list) { $list ... ->add('totalnumberofbids'); }
the "magic getter" automatic invoke gettotalnumberofbids method.
note: dependent on number of objects holding campaigns collection, summation of numberofbids slow due use of foreach loop.
Comments
Post a Comment