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

Tuesday, February 22, 2022

[FIXED] How to create a lookup in CakePHP

 February 22, 2022     cakephp, cakephp-3.0, dropdown, lookup, menu     No comments   

Issue

I'm in the process of converting a 10 year old PHP application. After my boss hired a php consultant, he has set up a CakePHP application environment and we are learning as we go. (fun, I know). Also, I come from a javascript/sharepoint background and have not had a lot of php experience.

As a test, I created a basic address table with these fields: firstname, lastname, state, phonenumber. I've been using justice league members as names and other test data to populate my table. Baked it just fine, default bootstrap pages are working.

I decided I wanted to add a dropdown field called current status, and for now just to keep it simple I wanted the choices: alive, dead.I created the column in my address table.

I created a second table called statuses and pointed the status column in my first table to the status table, using the status id as the foriegn key.

Baked my new table and rebaked my old one.

The status drop down does not give my choices of dead or alive, If I click in the field I get an up or down arrow, and based on which one you click, it either increments or decrements by 1. So the first time I click it inserts a 0. If I go up or down, it adds or takes away one.

I'm not sure what I'm doing wrong, I'm guessing there is some additional code I need to add to the MVC?


Solution

ok, if this works, then a lot is working :-). Now to the following: set in the Status Model a query like this:

public function getStatus()
{
    $opt = $this->Status->find('list', array(...));
    return $opt;
}

Then get the list over to the Adress Controller like this:

$this->loadModel('Status');
$opt => $this->Status->getStatus();
$this->set('opt', $opt);

Now you are able to access the $opt in the view file. Just delete this line in the view:

$opts = array('0' => __('dead'), '1' => __('alive'));

And it should work.



Answered By - FishWave
  • 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