CakePHP 2.x Accessing Another Model

BrotherBarnabas

Noob to cakePHP here.

Bicycle belongsTo Stand
Stand hasMany Bicycle
Stand belongsTo Station
Station hasMany Stand

I've baked the CRUDs and now I want to display the station name when I view a bicycle. How should I define the bicycle - station relationship? Or what do I need to do to include the station model in the bicycles controller?

What I've tried so far. Which throws a "Notice (8): Undefined index: Station": BicyclesController.php:

public function index() {
        $this->Bicycle->recursive = 2;
        $this->set('bicycles', $this->Paginator->paginate());
    }

index.ctp:

    <?php echo $this->Html->link($bicycle['Station']['name'].$bicycle['Stand']['stand_no'], array('controller' => 'stands', 'action' => 'view', $bicycle['Stand']['id'])); ?>

Help!

Colonel Mustard

Since you have no direct relationship from Bicycle to Station, its not going to be available within $bicycle['Station'] Its more likely to be available in $bicycle['Stand']['Station]['name'].

In order to see if its actually being returned with recursive set as it is, I would run the query again and call $debug($bicycle);die; in the following order.

public function index() {
        $this->Bicycle->recursive = 2;
        $bicycles = $this->Paginator->paginate();
        $debug($bicycle);die;
        $this->set(compact('bicycles'));
    }

This will kill the model at this point and display an array of results returned. You will then see where the information you want is, and how to access it. Just remove the $debug line when finished.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

cakePHP accessing table without model

From Dev

CakePHP 2.x: How to manually set validationErrors without a model?

From Dev

rename Model Name from find('all') with cakePHP 2.X

From Dev

Why is my model not being saved? (CakePHP 2.x)

From Dev

CakePHP 2.x hasMany that belongsTo another table

From Dev

Check if a model is associated with another in cakephp?

From Dev

set condition on another model in cakephp

From Dev

accessing model that belongs_to another model

From Dev

Link the same model twice to another model in CakePhp

From Dev

Cakephp Is there a way make a Model that is based on another Model?

From Dev

Link the same model twice to another model in CakePhp

From Dev

Rails 4: Accessing model through another

From Dev

Accessing user name from another model

From Dev

Accessing the variable from another model in my controller

From Dev

Rails 4: Accessing model through another

From Dev

multiple model validation cakephp 2

From Dev

CakePHP 3 - Model - Accessing data from third layer of array

From Dev

Order 'Contain' Model in CakePHP 3.x

From Dev

CakePHP 3.X Multiple Model association

From Dev

hasAndBelongsToMany in CakePHP 2.x

From Dev

join in cakephp 2.x

From Dev

CakePHP 2.x Acl

From Dev

CakePHP 2 Model relations multiple foreign Keys

From Dev

cakephp 2 name variable in model and controller

From Dev

Accessing a JSON object in Bash - associative array / list / another model

From Dev

Accessing objects from another model using foreign key

From Dev

Accessing a JSON object in Bash - associative array / list / another model

From Dev

Cakephp 2 use task in another task

From Dev

accessing subfolder of cakephp site

Related Related

HotTag

Archive