-2

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));
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
daroum
  • 59
  • 14

3 Answers3

2

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) { ...

jeroen
  • 91,079
  • 21
  • 114
  • 132
1

Refactor your code to use a foreach. If all you want to do is iterate over the car array and print the name of each car then this should do the job. I've stripped out the bits that aren't necessary to illustrate the point.

class UserController extends Controller
{   
    public function indexAction()
    {
        $UserCars = array(...);

        foreach ($UserCars as $Car) {
            if (is_a($Car, 'UserCar')) {
                echo ('Created user :  ' . $Car->getName());    
            }
        }
    }
}

What you may also want to do as a precaution is verify the identity of each $UserCar you iterate over in order to make sure it is what you think it is before calling methods on it. This is what the is_a method does (I've guessed at your class name).

Drumbeg
  • 1,914
  • 1
  • 15
  • 22
1

If you really want the while loop :

   $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++;
    }

There is no need to check if the car have an Id since you get it from your repository obviously it will have one. But foreach is easier.. no need for extra variables( $i, $count_cars)

Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22