doujuncuo9339 2015-05-08 08:03
浏览 33
已采纳

注意:未定义的偏移量:9 [重复]

This question already has an answer here:

I'm working with symfony and php and I'm new in this. I want to echo all the user's cars.

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));
    }

Now my code looks like this (before making any changes) but i'm having an error:

Notice: Undefined property: OBCarsTest1Bundle\Entity\cars::$getName

I searched for the problem and tried to fix it and change in the code but i got the same result.

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));
    }
</div>
  • 写回答

3条回答 默认 最新

  • drqja5919276 2015-05-08 08:10
    关注

    There are two problems here:

    1. Array indices are 0-based and you use $a = 1; so you are probably missing out on the first element;

    2. You should not loop over an array using a while loop like that as you will always run into problems / errors / warnings when there are no more elements, this loop ends with a warning by definition:

      while($UserCars[$a]->getId() != Null)

      Instead you should use a foreach loop to loop over every element:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵