Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:31:49+00:00 2026-06-10T06:31:49+00:00

i am having trouble with cakephp and writing an sql query, I have the

  • 0

i am having trouble with cakephp and writing an sql query, I have the sql code written out but am not sure on how to code it in cake.

`users`          - id, name, address
`accounts`       - id, companyname, abn
`accounts_users` - id, account_id, user_id
`templates`      - id, name, description, account_id
`fields`         - id, name, description, template_id
  • users habtm accounts
  • accounts habtm users
  • accounts hasMany templates
  • templates belongsTo accounts
  • templates hasMany fields
  • fields belongsTo templates

what I am trying to get my find statement in the fieldscontroller, to add the template_id field is to do is this

select t.id
from  template.t, account_users.au, user.u
where t.account_id=au.account_id AND
au.user_id=users.id;

this is the code I have so far in my fields in controller:

function add() {
    $this->Session->setFlash("Please create your required fields.");
    $templates = $this->Template->find('list', array(
        'fields' => array('id'),
        'conditions' => array('id'=> $this->Auth->user('id'))));
    $this->set('templates','$templates');

    if($this->request->is('post')) {
        $this->Field->create(); 

        if ($this->Field->save($this->request->data)) {     
            if ($this->request->data['submit'] == "type_1") { 
                $this->Session->setFlash('The field has been saved');  
                $this->redirect(array(
                    'controller' => 'fields',
                    'action' => 'add'));
            } 
            if ($this->request->data['submit'] == "type_2") {
                $this->Session->setFlash('The template has been saved'); 
                $this->redirect( array('controller' => 'templates','action' => 'index'));
            }
        } else {
            $this->Session->setFlash('The field could not be saved. Please, try again.'); 
        }
    }
}

the sql statement it currently is printing out is

SELECT `Template`.`id` FROM `pra`.`templates` AS `Template` WHERE `id` = 14

id is referring to the users table id

The error is i am trying to get the template_id in the the add form. the find function isn’t getting the template_id’s, it is instead grabbing the user – id

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:31:51+00:00Added an answer on June 10, 2026 at 6:31 am

    I think there’s something wrong with your field names. Are they in line with the code/name conventions?
    Please post the table layout of the models.

    if template_id is the primary key for the table templates you should use:

    public $primaryKey = 'template_id';
    

    in your templates model.
    http://book.cakephp.org/2.0/en/models/model-attributes.html#primarykey

    and offcourse your getting what your asking. The query should be more like:

    $templates = $this->Template->find('list', array(
        'fields' => array('Template.template_id'),
        'conditions' => array('User.id'=> $this->Auth->user('id'))));
    

    But that is dificult to say without your exact table layout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble saving multiple entries in CakePHP. I'm not sure whether saveAll()
Having trouble with this -- a couple of other related posts out there, but
So I'm having trouble with unit testing CakePHP models. Simple stuff like writing tests
Having trouble here and not quite sure how to do it. I've attempted jQuery
I'm having trouble with my HABTM relationship in CakePHP. I have two models like
I have this data spited out from my cake php query.. This is the
Having trouble with the binary_search function listed at the top. not sure where to
Having trouble with ClickOnce manifests and our build server. We have a .p12 code
Having trouble and not sure how to proceed with debug. Found a question that
I'm having trouble composing a CakePHP find() which returns the records I'm looking for.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.