CakePHP Basics Tutorial – Part 4

Overview

In this tutorial I will show you the power of associations in cakePHP.  With associations you can link up your models and get related data when you fetch results from one model.

In this example I’ll show you how to create an association between a User and a Product.

In this tutorial I’ll be covering

  1. Explain and create associations between models
  2. Create an add form for the Products
  3. Fetch data from one model and get the associated data for free

This part is the follow-up on what we have seen in CakePHP Basics Turorial – Part 3.

Let me know if you enjoyed it! Happy coding!

Share this post

12 Responses

  1. Hi.. nice tutorial.
    Could you help me on this I am new in cakephp.
    When I am using association gives, Error: Call to a member function find() on a non-object File: /cakeapp/app/Controller/QuestionsController.php
    Where my association as follows,
    Questions belongsTo Test
    Test hasMany Questions.
    Same as explained in tutorial.
    Following my app informaion,
    Database
    – tests
    – questions
    Models
    – Questions.php
    – Test.php
    Controllers
    – QuestionsController.php
    – TestsController.php
    View Folders
    – Questions
    – Tests
    Rather Everything works fine like view questions, view tests etc..
    Please help me.
    Thanks in advance

  2. Can you also cover, master detail forms ?
    ie. invoice page having multiple line items, and a form allowing to enter main invoice information like invoice #, po #, date, and multiple line items on the same form ?

    Thanks

    Kadri

  3. very nice tutorial,
    Can u plz explain how to retrieve data and display it in view using associations?
    I have two association arrays same as in above tutorial,but i am stuck in getting data from those association arrays and displaying it in views.
    Thank u

  4. These tutorial series really helped me a lot in understand CakePHP.

    Thank you very much for all the effort you have put in these.

    Hope to see more of your great tutorials!

  5. Hello Frederik .
    Thanks a lot for this tutorial , its clear and nice .
    Well done, keep up the good work , and good luck for exams!

  6. Ik heb nog een praktische vraag.

    Ik heb een ingelogde gebruiker ($this->Auth->User) en dus ook een model User. Ik heb ook een model Purchase met een belongsTo-relatie naar User. Dus de user_id kolom staat in mijn tabel purchases.

    Hoe kan ik nu een nieuw object/rij opslaan zonder een form te gebruiken?

    $this->Purchase->create(array(
    ‘user’ => $this->Auth->User()
    ));

    $this->log($this->Purchase); // De user lijkt mij correct te zijn toegevoegd.

    $this->Purchase->save($this->Purchase->data, array(
    ‘fieldList’ => array(‘user’)
    ));

    Dit doet niets in mijn database en geeft geen foutmeldingen. De DebugKit geeft aan dat er geen SQL queries uitgevoerd zijn.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts