php - Pulling data from another table while I have the index keys from my table -


i have 3 mysql tables:

brands ( brand names , brand ids)  cars ( car names,car ids, brand ids)  statuss ( car ids, each car status). 

i trying pull brand names brand table have cars ids. how can see code below.


controller.ctp

$this->loadmodel('car');         $cars = $this->car>find('all',array('limit' => 6, 'order' => array('car.id' => 'asc')));         $this->set('cars', $cars);          $this->loadmodel('status');         $cars = $this->status>find('all',array('limit' => 6, 'order' => array('status.id' => 'asc')));         $this->set('statuss', $statuss); 


view.ctp

app::import('controller', 'cars'); $carscont = new carscontroller;

app::import('controller', 'brands'); $brandcont = new brandscontroller;

foreach($statuss $status)

{

$car_info = $carscont->get_car_info($status['status']['car_id']);

            $car_name = $car_info['car_name'];              $car_id = $status['status']['car_id'];      $brand_list = $brandcont -> get_brand_name($status['car']['brand_id']); <---- not working     echo $brand_list['brand']['brand']; echo $car_name;  

}

cakephp controllers not designed or meant instantiated directly doing.

rather, should making use of associations cakephp gives you.

based on you've said, you've got statuses belongsto cars belongsto brands. set relations in model files (as documented in book: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html)

if use containable behavior (http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html) you'd able (in status controller)

$statuses = $this->statuses->find('all', [     'contain' => ['cars' => 'brands'] ]); 

and should have data need.

edit: based on additions, change cars query to

$cars = $this->car>find('all',array('limit' => 6, 'order' => array('car.id' => 'asc'), 'contain' => ['brands'])); 

but, make sure cars belongsto brands, , you've attached containable behaviour.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -