this question has answer here:
- reference - error mean in php? 30 answers
i'm working symfony , php , i'm new in this. want echo 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 see cars of 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 code looks (before making changes) i'm having error:
notice: undefined property: obcarstest1bundle\entity\cars::$getname
i searched problem , tried fix , change in code got 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 $car) { echo "$car->getname() <br>"; } return new response('ma akal'); //return $this->render('obcarstest1bundle:default:index.html.twig', array('usercars' => $usercars)); }
there 2 problems here:
array indices 0-based , use
$a = 1;
missing out on first element;you should not loop on array using
while
loop run problems / errors / warnings when there no more elements, loop ends warning definition:while($usercars[$a]->getid() != null)
instead should useforeach
loop loop on every element:foreach ($usercars $usercar) { // not sure if need this, guess don't: if ($usercar->getid() != null) { ...
Comments
Post a Comment