0

Let's say I have 2 table.

One is floor, another is shop.

Inside floor table : id, floor_name

Inside shop table: id, name, floor_id.

Meanwhile, the association is: one floor hasMany shop, one shop hasOne floor.

ok , the question is ..

How can I get the floor_name data directly while I'm only able to get the floor_id in my view ?

Example: I display all of the shop data as
$shop['Shop']['name'], $shop['Shop']['floor_id'], which in my /shops/main

I want the $shop['Shop']['floor_id'] field display the $floor['Floor']['floor_name'] data ? possible ?

3
  • will u please add your find query here Commented May 14, 2013 at 4:08
  • There are many issues with this question including strange table naming conventions, incorrect (likely) associations, and referencing trying to get DB data from a view. Commented May 14, 2013 at 5:15
  • Aren't you forgetting belongsTo? Commented May 14, 2013 at 5:35

2 Answers 2

4

Kampung,

You should have below mentioned association in you tables:

Floor hasMany Shops Shop belongsTo Floor

Now in your controller when you will fetch data about your shop then you will get floor data automatically.

$shops=$this->Shop->read(null, $id);

Now $shops will contain two array ['Shop']=>array(.......) ['Floor']=>array(........)

so now to display the floor name, you need to use $shop['Floor']['floor_name'] instead of $floor['Floor']['floor_name']

Sign up to request clarification or add additional context in comments.

Comments

1

Define a hasMany Floor relationship in your Shop model. In your Floor model, define a belongsTorelationship to Shop. This will allow your associated data to be retrieved.

When retrieving your data via $this->Shop->find('first', $arg), where $arg is your search option array, define the following

$arg['contains'] = array('Floor.name');

Now, you should get what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.