Notice: Undefined index: checkdate in /var/www/html/wp-content/plugins/whmcs-bridge-sso/includes/license.class.php on line 57
How to model association in CakePHP – skyX

How to model association in CakePHP


Database relationship is difficult to keep up notwithstanding for a moderate sized PHP/MySQL application, especially, when different levels of connections are included on the grounds that confounded SQL questions are required. CakePHP offers a straightforward yet capable element called ‘question social mapping’ or ORM to deal with database associations with ease.In CakePHP, relations between the database tables are characterized through affiliation—an approach to speak to the database table relationship inside CakePHP. Once the affiliations are characterized in models as indicated by the table connections, we are prepared to utilize its great functionalities. Utilizing CakePHP’s ORM, we can spare, recover, and erase related information into and from various database tables with straightforwardness, betterly—no compelling reason to compose complex SQL questions with different JOINs any longer!

In this article by Ahsanul Bari and Anupom Syam, we will have a profound take a gander at different sorts of affiliations and their employments. Specifically, the reason for this article is to learn:

  • How to figure out association types from database table relations
  • How to define different types of associations in CakePHP models
  • How to utilize the association for fetching related model data
  • How to relate associated data while saving

There are basically 3 types of relationship that can take place between database tables:

  • one-to-one
  • one-to-many
  • many-to-many

The initial two of them are straightforward as they don’t require any extra table to relate the tables in relationship. In this article, we will first perceive how to characterize relationship in models for coordinated and one-to-numerous relations. At that point we will take a gander at how to recover and erase related information from, and spare information into, database tables utilizing model relationship for these straightforward affiliations.

Defining One-To-Many Relationship in Models

To perceive how to characterize a one-to-numerous relationship in models, we will think about a circumstance where we have to store data about a few writers and their books and the connection amongst writers and books is one-to-many. This implies a writer can have numerous books yet a book has a place with just a single writer (which is fairly silly, as, in actuality, situation a book can likewise have various writers). We are currently going to characterize relationship in models for this one-to-numerous connection, so our models perceive their relations and can manage them as needs be.

Time for Action: Defining One-To-Many Relation
  1. Create a new database and put a fresh copy of CakePHP inside the web root. Name the database whatever you like but rename the cake folder to relationship. Configure the database in the new Cake installation.
  2. Execute the following SQL statements in the database to create a table named authors.
  3. Create a books table in our database by executing the following SQL commands:
  4. Create the Author model using the following code (/app/models/authors.php):
  5. Use the following code to create the Book model (/app/models/books.php):
  6. Create a controller for the Author model with the following code: (/app/controllers/authors_controller.php):
  7. Use the following code to create a controller for the Book model (/app/controllers/books_controller.php):
  8. Now, go to the following URLs and add some test data:

http://localhost/relationship/authors/ and



Leave a Reply

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