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 4322222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:50:45+00:00 2026-05-21T08:50:45+00:00

I have 3 tables: users, images (foreign key to user_id), comments (foreign keys: user_id

  • 0

I have 3 tables: users, images (foreign key to user_id), comments (foreign keys: user_id and image_id)

Can somebody explain me or send me link to good tutorial how to work with database?
I mean e.g.:
1. I wants to take all Images which have any comment in Comments table. In SQL: select i from Images i, Commets c where i.id=c.image_id???

Can You explain me how can I access from one controller to another Model?
I was trying in Images controller:
$data = $this->Image->query(‘Select * from Images i, Comments c where i.id=c.image_id’);
And in view: $image[‘id’] but I have error:Undefined index: id [APP\views\images\commented.ctp,

SO it does not work:/

  1. How can I delete Image and all its comments? I used Zend and I get used to it and I have no idea how to do the same thinks in cakePHP :/

Can somebody explain me how to work with database in this cakePHP I know there are find, findAll etc functions but all of them are from a controller level ex. $this->Image->find() etc…

I need better tutorial than the basic cakePHP cookbook :/. I am open for any sugestions.
Regards,

  • 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-05-21T08:50:46+00:00Added an answer on May 21, 2026 at 8:50 am

    You reference models in the controller in one of many ways. The two common ways are with the uses variable at the top of the controller:

    var $uses = array('User','Image');
    

    Or you can put it inline in your functions:

    $this->loadModel('Image');
    $this->Image->find('all'); ...
    

    In addition, if you have the models linked by a foriegn key, you can even call it like:

    $this->User->Image->find('all');
    

    Once you get the hang of how CakePHP structures things, it makes it very clear.

    So for your sql statement in the question, you could do something like the following from the users controller:

    class UsersController extends AppController {
       var $name = 'Users';
       var $uses = array('User','Image','Comments');
    
       function {my_function_name}() {
           $this->Images->recursive = 1;
           $images = $this->Image->find('all');
       }
    }
    

    Now, as long as your relationships are correct in the model, the recursive function will build the images array with all of the comments attached to it.

    array(
       [Image] => array(
            image data,
            [comments] => array(
                [0] comment data,
                [1] comment data,
                [2] ...
            )
        )
    )
    

    I hope that at least pushes you in the right direction. Happy coding!

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

Sidebar

Related Questions

I have a pretty simple profile page where users can upload images and videos.
For example I have 2 tables, Users and UserRelations , and it is a
Let's say you have two tables, Users and UserRoles. Here's how the two tables
I have a simple database with two tables. Users and Configurations. A user has
I have a table of users which has a username column consisting of a
I have a table of Users that includes a bitmask of roles that the
I have table with 50 entries (users with such details like Name Surname Location
I have dbml with single table users i want add partial class for User
I have a DataGridView binded to this table: [Users] ID Name -- ---- 11
I have a table that saves some account limits like users. For most rows

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.