Symfony 2对象与对象数组,如何访问它们?

时间:2014-04-26 18:47:53

标签: php mysql symfony object doctrine-orm

我有3个以下列方式相关的Symfony2实体:

  1. 具有板号,制造,型号的车辆。此外,车辆还有许多工作和燃料购买
  2. Job有一种方法可以给出作业的总距离(在开始时使用里程表,在工作结束时使用里程表。
  3. 燃料购买有升,燃料价格和成本方法。
  4. 想要根据一段时间呈现具有车牌号,总距离,总燃料,燃料成本,Km / L,总作业的表格报告。 还想对数据库做一个传统的查询,完成所有这些并发布了this问题。你可以在那里看到一些实际的物体。但是,如果我没有错,Symfony2的理念是使用具有所有这些对象的车辆对象来完成它。您认为最好的方法是什么? 只是我不能设法例如上下对象数组。我试过这个:

    class ReportsController extends Controller
    {
        public function renderAction(Request $request)
        {
            $vehicles = $this->getDoctrine()
                ->getRepository('TeamERPTransportBundle:Vehicle')->findAll();
            $countVehicles = 0;
            $countJobsXVehicle = array();
            foreach ($vehicles as $vehicle)
            {            
                $VehicleJobs = $vehicle->getJobs();
                $countJobsXVehicle[$countVehicles] = 0;
                $countVehicles++;
                //This piece of code gives the error
                foreach ($VehicleJobs as $VehicleJob)
                {
                   $countJobsXVehicle[$countVehicles]++;
                }
                //I am planing to do the same for Fuel purchase
                //$VehicleRefuels = $vehicle->getFuelPurchaces();
                //foreach ($VehicleRefuels)
            }
        }
    }
    

    有了这个我得到这个错误: ContextErrorException: Notice: Undefined index: vehicles in D:\Web\wamp\www\team\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 1753

    如何让它发挥作用? 谢谢你的帮助!

    编辑: 我也试过这个:

    class ReportsController extends Controller
    {
    public function renderAction(Request $request)
    {
        $vehicles = $this->getDoctrine()->getRepository('TeamERPTransportBundle:Vehicle')->findAll();
    
        $countVehicles = 0;
        $countJobsXVehicle[] = 0;
    
                foreach ($vehicles as $vehicle)
                {     
                        $VehicleJobs  = new ArrayCollection();
                        $VehicleJobs[]  = $vehicle->getJobs();
                        $countJobsXVehicle[$countVehicles] = $this->VehicleJobs($VehicleJobs);          
                        //$VehicleRefuels = $vehicle->getFuelPurchaces();
                        //foreach ($VehicleRefuels)
                        $countVehicles++;                        
                }
    
    }
        return $this->render('TeamERPTransportBundle:Transport:report.html.twig', array('vehicles' => $vehicles,
            'countJobsXVehicle' => $countJobsXVehicle,
            'countVehicles' => $countVehicles,
            'render' => $graph));
    }
    private function VehicleJobs (ArrayCollection $VehicleJobs){
        $countJobsXVehicle = 0;
        foreach ($VehicleJobs as $VehicleJob)
        {
            $countJobsXVehicle++;
            //var_dump($VehicleJob);
        }  
        return $countJobsXVehicle;
    }
    }
    

    并且它可以工作,但它的行为不符合预期。 Report Table 实体条款为here

0 个答案:

没有答案