PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How can i get cakephp current model's foreign key column name?

 April 22, 2022     cakephp, cakephp-2.3     No comments   

Issue

Is there way to get current model's foreign key name in other model something like this

echo $this->Category->whatIsMyForeignKeyName();
// expected output 'category_id'

Edit: Version

strtolower($this->Category->alias).'_id'

somehow seems not in spirit of cake.


Solution

What you did with strtolower will work in almost all (or maybe all?) cases, though a slightly more thorough way would be something like:

$fkey = Inflector::singularize(Inflector::tableize($this->Category->alias)).'_id';

But the question I'd be asking is why do you want to do that? Why not just hard-code it as 'category_id'?



Answered By - joshua.paling
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing