Warning: Illegal string offset '_imagedir' in /home/cakephpf/public_html/lofiversion/index.php on line 545

Warning: Illegal string offset '_emodir' in /home/cakephpf/public_html/lofiversion/index.php on line 546
CakePHP UnOfficial Community Forum > Reading name using city_id in the user (main) tabl
Help - Search - Member List - Calendar
Full Version: Reading name using city_id in the user (main) tabl
CakePHP UnOfficial Community Forum > General CakePHP Discussions

Must be a simple one. I want to use cakephp model behaviour to do this. Can you help?

I have a user table which stores id(s) for city, secretquestion etc., so my 'user' table has city_id and secretquestion_id and the actual names/texts for these are stored in City & Secretquestion tables respectively.

Now, I am coding UsersController. When I read my User table, I get these IDs obviously. But I need to retrieve these names (City Name, Secretquestion Text) for subsequent use.

How can I do that? Can someone shed light on the steps involved?

I can simply write a SQL and do that but I want to utilize CakePHP Model functionality and handle this the CakePHP way.

You need to look at associations...

Basically, from your post, it sounds like your associations will be like.

User belongsTo City
User belongsTo SecretQuestion

Then in your model, when you

It will also return the matched results for City / Secret.

I haven't tested the code, but you do need associations.
Yes, it works.

I added this to my model. That's all it took.

I thought this works only when my primary table's id is placed in the secondary table as foreign key unlike in my case where I have seconday table id is in the primary table.

var $belongsTo = array(
'Secretquestion' => array(
'className' => 'Secretquestion',
'foreignKey' => 'secretquestion_id',
'conditions' => '',
'fields' => '',
'order' => ''
Glad to of helped :-)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2015 Invision Power Services, Inc.
Lo-Fi 1.1 iDS Beta, Originally written by Matt,
re-written by Shaun Harrison, Layer 04.com, for pre IPB2.0 versions.