【问题标题】:Notice: Undefined offset: 9 [duplicate]注意:未定义的偏移量:9 [重复]
【发布时间】:2015-05-08 08:03:18
【问题描述】:

我正在使用 symfony 和 php,我是这方面的新手。我想呼应所有用户的汽车。

class UserController extends Controller
{   
    public function indexAction($uname)
    {
        $cars = new cars();
        $user = new user();
        $models = new models();
        $brands = new brands();
        $a = 1;

        $em = $this -> getDoctrine() -> getEntityManager();
        $id = $em -> getRepository("OBCarsTest1Bundle:user") ->findOneByuname($uname);
        array ($UserCars = $em -> getRepository("OBCarsTest1Bundle:cars") ->findByidUser($id));
        //$ModelId = $em -> getRepository("OBCarsTest1Bundle:models")->findById($UserCars);
        //$BrandsId = $em -> getRepository("OBCarsTest1Bundle:brands")->findOneById($ModelId);

        while($UserCars[$a]->getId() != Null)
        {
            if(! isset($UserCars[$a]))
            {
                $UserCars[$a] = Null;
            }
            //i want to see all the cars of the users
            echo ('Created user :  '.$UserCars[$a]->getName());
            //return new Response('Created user :  '.$UserCars[$a]->getId());
            $a++;
        }

        return new Response('ma akal');

        //return $this->render('OBCarsTest1Bundle:Default:index.html.twig', array('UserCars' => $UserCars));
    }

现在我的代码看起来像这样(在进行任何更改之前)但我遇到了一个错误:

注意:未定义属性:OBCarsTest1Bundle\Entity\cars::$getName

我搜索了问题并尝试修复它并更改代码,但我得到了相同的结果。

class UserController extends Controller
{   
    public function indexAction($uname)
    {
        $cars = new cars();
        $user = new user();
        $models = new models();
        $brands = new brands();
        $a = 0;

        $em = $this -> getDoctrine();
        $id = $em -> getRepository("OBCarsTest1Bundle:user") ->findOneByuname($uname);
        $UserCars = $em -> getRepository("OBCarsTest1Bundle:cars") ->findByidUser($id);
        //$ModelId = $em -> getRepository("OBCarsTest1Bundle:models")->findById($UserCars);
        //$BrandsId = $em -> getRepository("OBCarsTest1Bundle:brands")->findOneById($ModelId);

        foreach($UserCars as $car)
        {
            echo "$car->getName() <br>";
        }


        return new Response('ma akal');

        //return $this->render('OBCarsTest1Bundle:Default:index.html.twig', array('UserCars' => $UserCars));
    }

【问题讨论】:

  • 更好的选择是使用foreach 循环,但在循环之前检查$UserCars 是否不是情感

标签: php symfony doctrine-orm


【解决方案1】:

这里有两个问题:

  1. 数组索引是从 0 开始的,并且您使用 $a = 1;,因此您可能会错过第一个元素;

  2. 您不应该像这样使用 while 循环遍历数组,因为当没有更多元素时,您总是会遇到问题/错误/警告,此循环根据定义以警告结束:

    while($UserCars[$a]-&gt;getId() != Null)

    相反,您应该使用 foreach 循环来遍历每个元素:

    foreach ($UserCars as $UserCar) { // not sure if you need this, I would guess you don't: if ($UserCar->getId() != Null) { ...

【讨论】:

    【解决方案2】:

    重构您的代码以使用foreach。如果您只想遍历汽车数组并打印每辆车的名称,那么这应该可以完成工作。我已经删除了不需要说明这一点的部分。

    class UserController extends Controller
    {   
        public function indexAction()
        {
            $UserCars = array(...);
    
            foreach ($UserCars as $Car) {
                if (is_a($Car, 'UserCar')) {
                    echo ('Created user :  ' . $Car->getName());    
                }
            }
        }
    }
    

    作为预防措施,您可能还想做的是验证您迭代的每个 $UserCar 的身份,以确保在调用方法之前它是您认为的那样。这就是is_a 方法的作用(我猜到了你的类名)。

    【讨论】:

      【解决方案3】:

      如果你真的想要 while 循环:

         $count_cars = count($UserCars);
         $i = 0;
         while($count_cars > 0)
         {
             echo ('Created user :  '.$UserCars[$i]->getName());
             //return new Response('Created user :  '.$UserCars[$a]->getId());
             $count_cars--;
             $i++;
          }
      

      没有必要检查汽车是否有 ID,因为您从存储库中获取了它,显然它会有一个。 但是 foreach 更容易......不需要额外的变量($i,$count_cars)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-22
        • 1970-01-01
        • 2015-08-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2012-02-08
        相关资源
        最近更新 更多